The Collections class is used as a data structure to manage the data. We can add, remove, fetch, and update the data in the List, Set, or Map Object. Collections class has default methods for these operations. We can use those methods easily. By default, when we create an object of the Collections class, it will be Both Readable and Writable.
Read-only Collection: To make the object of Collections to Read-Only, we need to restrict an object to add, remove, or update data from it. The only operation is to fetch the data.
Java has different methods for different Collection type like unmodifiableCollection(), unmodifiableMap(), ununmodifiableSet() e.t.c. All the methods are predefined in java.util.Collecitons class.The unmodifiableCollection() is a generic method to make Read-Only collection. We need to make the reference of Collections class for that. If we have an object of Set Interface, we can use ununmodifiableSet() to make Read-Only.
Example 1: Below code shows how to make a List unmodifiable.
12345678910 After Performing Some Operations 123446781011 Hence By default Collections object is Readable and Writable Exceptions is java.lang.UnsupportedOperationException 4 Now list is only Read-Only
Above is an example of how to make a list Read-Only. Before making Read-Only we can perform CRUD operations but after making Read-only list, set(), add(), and remove() methods will generate Exceptions. We can now only fetch the data from the list.
Example 2: Below code shows how to make a Set unmodifiable.
12345 After Performing Operation 1234 Set is also By Default Readable and Writable Exceptions is java.lang.UnsupportedOperationException true Now Set is Read-Only
In Above Example, We make Set as Read-Only. We can make Collections object Read-Only by using unmodifiableCollection() and to make Map Read-Only we can use unmodifiableMap() method.
Method Description static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c) This method accepts any of the collection objects and returns an unmodifiable view of the specified collection. static <T> List<T> unmodifiableList(List<? extends T> list) This method accepts an object of the List interface and returns an unmodifiable view of it. static <K,V> Map<K,V> unmodifiableMap(Map<? extends K,? extends V> m) This method accepts an object of the Map interface and returns an unmodifiable view of it. static <T> Set<T> unmodifiableSet(Set<? extends T> s) This method accepts an object of the Set interface and returns an unmodifiable view of it.. static <K,V> SortedMap<K,V> unmodifiableSortedMap(SortedMap<K,? extends V> m) This method accepts an object of the SortedMap interface and returns an unmodifiable view of it. static <T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> s) This method accepts an object of the SortedSet interface and returns an unmodifiable view of the specified sorted set.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.