There are some factory methods for creating unmodifiable Collections. before it,
in brief,
> List :
It is an indexed collection of elements where duplicate elements are allowed and insertion order is preserved.
> Set :
It is an unordered collection of elements where duplicate elements are not allowed and do not follow insertion order.
> Map :
This collection contains Key-Value pair which is called Entry (inner interface inside Map), Keys must be unique and Values can be duplicates.
It is very common to use immutable collection objects in Programming requirements to improve Memory Utilization and Performance.
In Java 9,
1. static <E> List<E> of() //factory method of unmodifiable List
2. static <E> Set<E> of() //factory method of unmodifiable Set
Up to 10 elements are allowed and the matched methods (12 methods [Method overloading] -> 1 without param method, 10 param methods and 1 var-arg param method) will be executed for more than ten elements, the internal var-arg method will be invoked.
3. static <E> Map<K,V> of() //factory method of unmodifiable Map
in the Map case, we need to pass the key-value pair instead of elements alone.
ex: Map<String, String> map = Map.of("1", "Java", "2", "Spring");
> we have another way in map to create unmodifiable map object:
Map.Entry<String, String> e = Map.entry("1", "Java", "2", "Spring");
The Entry object is immutable and cannot be modifiable, if we try to change the content then, will get UnsupportedOperationException. by using these Entry objects we can create an Unmodifiable Map object with Map.ofEntries() method.
Map<String, String> m = Map.ofEntries(e);
Short way,
import static java.util.Map.entry;
Map<String, String> map = Map.ofEntries(entry("1","Java"), entry("2", "Spring"));
In Map Collection we have to take care of that "Up to 10 elements, it is recommended to use of() methods and for more than 10 items we should use ofEntries() method".
There are some exceptions we can get within this feature:
1. NullPointerException
2. UnsupportedOperationException
3. IllegalArgumentsException
1. NullPointerException with Unmodifiable Collection Objects:
ex: List<String> lang = List.of("Java", "Python", "C", null);//NullPointerException
/*while using these factory methods, if any element is null then we will get this exception.*/
2. UnsupportedOperationException with Unmodifiable Collection Objects:
ex:
Set<String> lang = Set.of("Java", "Python", "C");
lang.add("Ruby"); //UnsupportedOperationException
lang.remove("Python"); //UnsupportedOperationException
/*after creating Unmodifiable objects if we try to change the content(add/remove) then we will face this exception.*/
3. IllegalArgumentsException with Unmodifiable Collection Objects:
ex-1:
Map<String, String> langMap = Map.of("Lang-1","Java", "Lang-2","Python", "Lang-3","C", "Lang-1","Ruby");//IllegalArgumentsException
/*while using these factory methods, if we are try to add duplicate keys then we will get this exception.*/
ex-2:
Set<String> lang = Set.of("Java", "Python", "C", "Java");//IllegalArgumentsException
/*While using these factory methods, if we are trying to add duplicate elements in the set, then we will get this exception.*/