Java Ix Example - Manufacturing Works Life Methods For Collection - Creating Unmodifiable List, Set, As Well As Map

Hello guys, this is my start article inwards Java ix features on this weblog too today you'll larn close my favorite characteristic "factory methods for collection", which is introduced equally business office of JEP 269. The JEP stands for JDK enhancement proposal. If y'all accept worked inwards Groovy or Kotlin thence y'all know that how slow is to create the listing alongside elements using collection literals e.g. to create a listing of 1, 2, three y'all tin only write val items = listOf(1, 2, 3). Unfortunately, Java doesn't back upwards that even thence but things accept been improved alongside the mill methods for collection inwards JDK ix too it's almost similar that. JDK has added static mill methods similar of() on to basic Collection interfaces which y'all tin role to create a listing of items.

Even though the Project Jigsaw or Java Module systems is the original highlight of Java ix release, in that place are several other useful features which are to a greater extent than helpful from evolution indicate of thought e.g. procedure API enchantment, Stream API enhancements too precisely about useful methods on Optional class, but the API alter which I liked most is the mill methods for Collection.

It allows y'all to create a list, set, too a map of values inwards precisely 1 line, precisely similar y'all tin do inwards Kotlin, Scala, or Groovy:

List<String> listing = List.of("Java", "Kotlin", "Groovy");

But, entirely grab is that y'all tin create an unmodifiable or immutable List, Set, or Map.

The List, Set or Map returned yesteryear the of() static mill method are structurally immutable, which agency y'all cannot add, remove, or alter elements in 1 trial added.

Calling whatever mutator method volition ever movement UnsupportedOperationException to travel thrown. However, if the contained elements are themselves mutable, this may movement the Collection to deportment inconsistently or its contents to look to change.



How to create Immutable List, Set, too Map inwards Java 8

This is same equally the unmodifiable list y'all create inwards JDK vi or seven equally shown below:

List<String> listOfString = novel List<>();
listOfString.add("Java");
listOfString.add("Kotlin");
listOfString.add("Groovy");
listOfString.add("Scala");
listOfString = Collections.unmodifiableList(listOfString);

The listing returned yesteryear the unmodifiableList() method likewise doesn't back upwards add, remove, or fix functioning too throws UnsupportedOperationException if y'all telephone call upwards them.

The entirely divergence betwixt 2 code is that before it required to a greater extent than than vi lines of code to create an immutable Collection e.g. an immutable List, Set or Map but at in 1 trial y'all tin do that inwards precisely 1 line.

There are likewise several overloaded version of List.of() is available on the List interface e.g. to permit y'all to create an immutable listing of 1 to 10 elements too a variable declaration method which allows y'all to create the listing of whatever let on of elements.

Same is truthful for Set.of() too Map.of() method equally well. Here is the illustration fo creating an immutable Set inwards Java 9:

Set<Integer> primes = Set.of(2,3,5,7);

You tin run into that y'all tin create an immutable Set inwards precisely 1 line. Similarly, to create immutable Map, JDK ix provides 2 methods Map.of(K k1, V v1) too Map.ofEntries() yesteryear using these 2 y'all tin create a Map of immutable entries e.g.

 The JEP stands for JDK enhancement proposal Java ix Example - Factory Methods for Collection - Creating Unmodifiable List, Set, too Map

This method is overloaded to create a map upwards to 10 key-value pairs but if y'all take a bigger map alongside to a greater extent than mapping thence y'all should role Map.ofEntries() method.

Btw, do y'all know how this characteristic is implemented? too why it wasn't available before? If y'all facial expression at JDK ix code or Javadoc thence y'all volition notice that this characteristic is implemented yesteryear adding static mill method on telephone substitution interfaces of Java Collection framework e.g. List, Set, too Map.

This wasn't possible until JDK 8 because adding a method on the interface agency breaking all its clients too static methods were non allowed on the interface. Things changed on Java 8 alongside the introduction of default too static methods on the interface which pave the way for evaluating the JDK API.

I promise to a greater extent than similar enchantment volition come upwards inwards hereafter which makes using JDK API fifty-fifty easier.

If y'all are non familiar alongside JDK 8 changes yet, The Ultimate Java 8 Tutorial course on Udemy is a practiced starting point.


Also, the rules that apply to the role of the unlike collections likewise apply (as y'all would expect) when using these mill methods. So, y'all cannot transcend a duplicate chemical ingredient when y'all create a Set because Set doesn't permit duplicate.

Similarly, y'all cannot transcend duplicate keys when y'all create a Map because Map doesn't permit duplicate keys. If y'all do thence IllegalArgumentException volition travel thrown

Also, y'all can't transcend a cypher value to the collection mill method, if y'all do, travel cook for the form of Java exceptions, the NullPointerException.


That's all close how to create an immutable list, set, too map inwards Java 9. The static method on collections has actually made using Java collection API easier too at to the lowest degree it's at in 1 trial similar to what Kotlin or Groovy offer. JDK ix is amount of such useful characteristic too remain tuned for to a greater extent than of such articles on this blog. If y'all can't wait, cheque out What's New inwards Java ix - Modules too more, which provides a prissy overview of all JDK ix features.

Other Java too Programming Articles y'all may like
Java ix New Features In Simple Way - Overview (Free Course)

Thanks for reading this article thence far. If y'all similar this Java ix characteristic thence delight percentage alongside your friends too colleagues. If y'all accept whatever questions for feedback thence delight drib a note. 

Sumber https://javarevisited.blogspot.com/

0 Response to "Java Ix Example - Manufacturing Works Life Methods For Collection - Creating Unmodifiable List, Set, As Well As Map"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel