DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Java 9: Collections Improvement

Java 9: Collections Improvement

As Java 9 draws closer, let's take a look at using the new REPL to work with the new utility methods you can expect to see for Collections.

Gowtham Girithar Srirangasamy user avatar by
Gowtham Girithar Srirangasamy
·
Jun. 29, 17 · Java Zone · Tutorial
Like (32)
Save
Tweet
20.59K Views

Join the DZone community and get the full member experience.

Join For Free

Java 9 has created factory methods for creating immutable Lists, Sets, Maps, and Map.Entry Objects. These utility methods are used to create empty or non-empty collection objects.

In Java 8 and earlier versions, we can use Collections class utility methods like unmodifiableXXX to create Immutable Collection objects for our requirements. If we want to create an Immutable List, then we can use the Collections.unmodifiableList method.

However, these Collections.unmodifiableXXX methods are very tedious and verbose. To overcome those shortcomings, coupleJava 9 a couple of utility methods to List, Set, and Map interfaces to achieve the same behavior.

These useful methods are used to create a new Non-Empty Immutable Map with 1-10 elements. The methods are designed in such a case that we can add only 10 elements to immutable List, Set, Map, and Map.Entry objects.

If we have any requirement for an immutable collection with the size of the collection at a max of 10 or below, then we use this feature.

Characteristics of These Utility Methods

  1. These methods are immutable. We cannot add or delete or update the elements in the collection. If we try to add or delete or update the elements, it throws an unsupportedOperationException.Image title

  2. It doesn't allow null values. If we try to add null values to any collection, then it throws a null pointer exception.Image title

  3. They are serializable if all the elements are serializable.

Examples

List: (with values and an empty List)

List<String> list= List.of("apple","bat");
List<String> list= List.of();


Image title


Set: (with values and an empty Set)

Set<String> set= Set.of("apple","bat");
Set<String> set= Set.of();


Image title


Map: (with values and an empty Map)

Map<Integer,String> emptyMap = Map.of()
Map<Integer,String> map = Map.of(1, "Apple", 2, "Bat", 3, "Cat")


Image title


Map.Entry: (with values and an empty Map Entry)

Map<Integer,String> emptyEntry = Map.ofEntries()
Map.Entry<Integer,String> mapEntry1 = Map.entry(1,"Apple")
Map.Entry<Integer,String> mapEntry2 = Map.entry(2,"Bat")
Map.Entry<Integer,String> mapEntry3 = Map.entry(3,"Cat")
Map<Integer,String> mapEntry = Map.ofEntries(mapEntry1,mapEntry2,mapEntry3)


Image title

Note: Java 9 has Jshell(REPL) to write the code and execute it without creating the classes and main methods. I have used Jshell to execute all these cases.

Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DZone's Article Submission Guidelines
  • Low Code and No Code: The Security Challenge
  • No-Code/Low-Code Use Cases in the Enterprise
  • Hard Things in Computer Science

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo