Lets Create Unmodifiable Collections with Java 9

Posted By :Hemant Samriya |26th October 2021

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.*/


About Author

Hemant Samriya

Hemant is an experienced backend developer, specializing in Java. He possesses proficiency in various skills, including Java (up to Java 9), MongoDB, MySQL and tools like Postman, Azure, AWS Dashboard, and Searchkit. He is well-versed in IDE tools such as IntelliJ (primary), Eclipse (STS), and VSCode. He is also experienced in web technologies like JavaScript, HTML, CSS, and JSON. In terms of frameworks, he has expertise in Spring Boot (JPA, DATA, MVC, Security) and Hibernate. Additionally, he has hands-on experience with various AWS services such as Lambda, EC2, S3, and CloudWatch. Hemant is also familiar with ElasticSearch/OpenSearch as a search engine and utilizes version control through GitHub. He has contributed to several projects, including Konfer, HP1T, KRB, and many others.

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us