Java Ix Event - Mill Methods For Collection - Creating Unmodifiable List, Set, As Well As Map

Hello guys, this is my showtime article inwards Java nine features on this weblog too today you'll acquire nearly my favorite characteristic "factory methods for collection", which is introduced every bit move of JEP 269. The JEP stands for JDK enhancement proposal. If you lot bring worked inwards Groovy or Kotlin too then you lot know that how tardily is to create the listing amongst elements using collection literals e.g. to create a listing of 1, 2, iii you lot tin only write val items = listOf(1, 2, 3). Unfortunately, Java doesn't back upwards that nonetheless but things bring been improved amongst the mill methods for collection inwards JDK nine too it's almost similar that. JDK has added static mill methods similar of() on to basic Collection interfaces which you lot tin utilisation to create a listing of items.

Even though the Project Jigsaw or Java Module systems is the master copy highlight of Java nine 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 too around useful methods on Optional class, but the API modify which I liked most is the mill methods for Collection.

It allows you lot to create a list, set, too a map of values inwards exactly 1 line, exactly similar you lot tin exercise inwards Kotlin, Scala, or Groovy:

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

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

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

Calling whatever mutator method volition ever movement UnsupportedOperationException to hold out thrown. However, if the contained elements are themselves mutable, this may movement 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 too other essential Java topics similar threads, networking, too IO.




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

This is same every bit the unmodifiable list you lot create inwards JDK six or seven 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 too doesn't back upwards add, remove, or ready functioning too throws UnsupportedOperationException if you lot telephone telephone them.

The exclusively divergence betwixt 2 code is that before it required to a greater extent than than six lines of code to create an immutable Collection e.g. an immutable List, Set or Map but similar a shot you lot tin exercise that inwards exactly 1 line.

There are too 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 too a variable declaration method which allows you lot to create the listing of whatever lay out of elements.

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

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

You tin run into that you lot tin create an immutable Set inwards exactly 1 line. Similarly, to create immutable Map, JDK nine provides 2 methods Map.of(K k1, V v1) too Map.ofEntries() past times using these 2 you lot tin create a Map of immutable entries e.g.

 The JEP stands for JDK enhancement proposal Java nine 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 you lot bespeak a bigger map amongst to a greater extent than mapping too then you lot should utilisation Map.ofEntries() method.

Btw, exercise you lot know how this characteristic is implemented? too why it wasn't available before? If you lot expect at JDK nine code or Javadoc too then you lot volition honor that this characteristic is implemented past times adding static mill method on primal interfaces of Java Collection framework similar 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 amongst 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 the time to come 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 inwards 2019
  • 20 Essential libraries for Java Developers
  • 5 Spring Boot Features Java Developer Should Learn
  • 20 Books Java Programmers Can Read inwards 2019
  • 10 Free Java Courses for Beginners too Experienced Devs
  • Top 10 Java nine Tutorials too Courses - Best of Lot
  • 5 Free Courses to Learn Spring too Spring Boot Online
  • 10 Frameworks for Java too Web Developers
  • Java 10 Features Programmer Should Know

  • Thanks for reading this article hence far. If you lot similar this Java nine characteristic too then delight part amongst your friends too colleagues. If you lot bring whatever questions for feedback too then delight drib a note.

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

    Belum ada Komentar untuk "Java Ix Event - Mill Methods For Collection - Creating Unmodifiable List, Set, As Well As Map"

    Posting Komentar

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel