Java 9 Illustration - Manufacturing Flora Methods For Collection - Creating Unmodifiable List, Set, As Well As Map

Advertisement

Masukkan script iklan 970x90px

Java 9 Illustration - Manufacturing Flora Methods For Collection - Creating Unmodifiable List, Set, As Well As Map

Jumat, 27 Maret 2020

Hello guys, this is my start article inward Java ix features on this spider web log together with today you'll acquire near my favorite characteristic "factory methods for collection", which is introduced every bit purpose of JEP 269. The JEP stands for JDK enhancement proposal. If you lot accept worked inward Groovy or Kotlin thus you lot know that how slow is to create the listing amongst elements using collection literals e.g. to create a listing of 1, 2, three you lot tin sack merely write val items = listOf(1, 2, 3). Unfortunately, Java doesn't back upwards that withal but things accept been improved amongst the manufactory methods for collection inward JDK ix together with it's almost similar that. JDK has added static manufactory methods similar of() on to basic Collection interfaces which you lot tin sack utilisation to create a listing of items.

Even though the Project Jigsaw or Java Module systems is the primary 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 sentiment e.g. procedure API enchantment, Stream API enhancements together with closed to useful methods on Optional class, but the API alter which I liked most is the manufactory methods for Collection.

It allows you lot to create a list, set, together with a map of values inward precisely 1 line, precisely similar you lot tin sack practise inward Kotlin, Scala, or Groovy:

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

But, exclusively grab is that you lot tin sack create an unmodifiable or immutable List, Set, or Map.

The List, Set or Map returned past times the of() static manufactory method are structurally immutable, which way you lot cannot add, remove, or alter elements 1 time added.

Calling whatsoever mutator method volition e'er drive UnsupportedOperationException to hold out thrown. However, if the contained elements are themselves mutable, this may drive the Collection to comport inconsistently or its contents to appear to change. If you lot are non familiar amongst the Java Collection framework then The Complete Java MasterClass course on Udemy is a proficient starting point. It non exclusively covers the Collections but likewise other essential Java topics similar threads, networking, together with IO.




How to create Immutable List, Set, together with Map inward Java 8

This is same every bit the unmodifiable list you lot create inward JDK vi or vii every bit 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 past times the unmodifiableList() method likewise doesn't back upwards add, remove, or gear upwards functioning together with throws UnsupportedOperationException if you lot telephone telephone them.

The exclusively deviation betwixt ii 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 1 time you lot tin sack practise that inward precisely 1 line.

There are likewise several overloaded version of List.of() is available on the List interface e.g. to allow you lot to create an immutable listing of 1 to 10 elements together with a variable declaration method which allows you lot to create the listing of whatsoever divulge of elements.

Same is truthful for Set.of() together with Map.of() method every bit well. Here is the illustration fo creating an immutable Set inward Java 9:

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

You tin sack run into that you lot tin sack create an immutable Set inward precisely 1 line. Similarly, to create immutable Map, JDK ix provides ii methods Map.of(K k1, V v1) together with Map.ofEntries() past times using these ii you lot tin sack 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, together with Map

This method is overloaded to create a map upwards to 10 key-value pairs but if you lot require a bigger map amongst to a greater extent than mapping thus you lot should utilisation Map.ofEntries() method.

Btw, practise you lot know how this characteristic is implemented? together with why it wasn't available before? If you lot hold off at JDK ix code or Javadoc thus you lot volition uncovering that this characteristic is implemented past times adding static manufactory method on telephone commutation interfaces of Java Collection framework similar List, Set, together with Map.

This wasn't possible until JDK 8 because adding a method on the interface way breaking all its clients together with static methods were non allowed on the interface. Things changed on Java 8 amongst the introduction of default together with 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 inward the hereafter which makes using JDK API fifty-fifty easier. If you lot are non familiar amongst JDK 8 changes yet, 10 Things Java Developers Should Learn inward 2019
  • 20 Essential libraries for Java Developers
  • 5 Spring Boot Features Java Developer Should Learn
  • 20 Books Java Programmers Can Read inward 2019
  • 10 Free Java Courses for Beginners together with Experienced Devs
  • Top 10 Java ix Tutorials together with Courses - Best of Lot
  • 5 Free Courses to Learn Spring together with Spring Boot Online
  • 10 Frameworks for Java together with Web Developers
  • Java 10 Features Programmer Should Know

  • Thanks for reading this article thus far. If you lot similar this Java ix characteristic thus delight portion amongst your friends together with colleagues. If you lot accept whatsoever questions for feedback thus delight drib a note.

    P. S. - If you lot novel to Java footing together with looking for a comprehensive course of report to start your journey, in that place is no meliorate course of report than The Complete Java MasterClass on Udemy. It is most up-to-date together with covers everything a Java programmer should know.