SEARCH BY

All

  • All
  • Java8
  • Spring

Creating immutable collections in java9

Post a Comment
Java9 has additional feature on Collections, based on requirement we need an immutable collections. To create immutable collections we can use of() method on Collection Class.
List<string> names = List.of("Jaya", "Kumar", "Reddy");
         
        //Preserve the elements order
        System.out.println(names);
         
        //names.add("Brian"); //UnsupportedOperationException occurs
         
        //java.lang.NullPointerException
        //List<string> names2 = List.of("Jaya", "Kumar", "Reddy", null);

Set do not allow duplicate elements, where as list has duplicate elements.
jaya
I love software designing, coding, writing and teaching...

Share this article

Related Posts

Post a Comment

Place your code in <em> </em> tags to send in comments | Do not add hyper links in commnet

Close

Subscribe our newsletter for the latest articles directly into your email inbox