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 > JShell Examples: Collections Static Factory Methods

JShell Examples: Collections Static Factory Methods

Explore Java 9's JShell and how you can use it to create static factory methods for collections, particularly with Lists, Sets, and Maps.

Martin Farrell user avatar by
Martin Farrell
·
Oct. 25, 17 · Java Zone · Tutorial
Like (8)
Save
Tweet
8.48K Views

Join the DZone community and get the full member experience.

Join For Free

This post continues my exploration of Java 9 features from my My Top Java 9 Features blog post. Here, we are experimenting with Java 9 Collections Static Factory Methods in the List, Set, and Map interfaces.

Collections Static Factory Methods

Java 9 makes it easier to create immutable lists using its new static Factory Methods.

List and Set

There are 12 Set.of and List.of methods:

  • List.of() or Set.of()
  • List.of(E e1) or Set.of(E e1) to E e10
  • List.of(E… elements) or Set.of(E… elements)

Examples

jshell> Set.of()
$1 ==> []
| created scratch variable $1 : Set<Object>


Note the inference as a List  object.

For a static List of (E e1, E e2, E e3):

jshell> List.of("one","two","three")
$2 ==> [one, two, three]
| created scratch variable $2 : List<String>


Again, note the inference as a List object.

The number of arguments keeps increasing until E e10, at which point we can use vararg.

Map

Similarly, Map defines a:

  • static Map of ()
  • static Map of (K k1, V v1) to (K k10, V v10)
  • static Map ofEntries (Map.Entry … entries) – note the use of Map.Entry

Examples

jshell> Map.of()
$12 ==> {}

jshell> Map.of("key1", "value1", "key2", "value2")
$13 ==> {key1=value1, key2=value2}
| created scratch variable $13 : Map<String,String>


Characteristics of Collections Static Factory Methods

Common characteristics of these static Factory Methods for Lists, Sets, and Maps are:

  • Structurally Immutable: UnsupportedOperationException is thrown, although the elements themselves are immutable.
jshell> Set<String> immutableSet = Set.of("one","two","three")
immutableSet ==> [three, two, one]
| created variable immutableSet : Set<String>

jshell> immutableSet.add("four")
| java.lang.UnsupportedOperationException thrown:


  • No Nulls: NullPointerException thrown.
jshell> List<Object> notNullList = List.of(null)
| Warning:
| non-varargs call of varargs method with inexact argument type for last parameter;
| cast to java.lang.Object for a varargs call
| cast to java.lang.Object[] for a non-varargs call and to suppress this warning
| List<Object> notNullList = List.of(null);
| ^--^
| java.lang.NullPointerException thrown:
| at List.of (List.java:1030)
| at (#10:1)


  • Serialized – Serialized if elements are Serializable.

List-Specific Characteristics

  • Order – Order is maintained the same as elements input
jshell> List<String> immutableList = List.of("one","two","three")
immutableList ==> [one, two, three]
| created variable immutableList : List<String>


Set Specific Characteristics

  • Reject Duplicates – The Set will also reject duplicates at creation time with an IllegalArgumentException:
jshell> Set.of("one","one")
| java.lang.IllegalArgumentException thrown: duplicate element: one


Map Specific Characteristics

  • Reject Duplicate Keys – The Map will reject duplicate keys with IllegalArgumentException –
jshell> Map.of("KeyOne", "one", "KeyOne", "two")
|  java.lang.IllegalArgumentException thrown: duplicate key: KeyOne
|        at ImmutableCollections$MapN.<init> (ImmutableCollections.java:680)
|        at Map.of (Map.java:1326)
|        at (#1:1)


  • Iteration is also not guaranteed

These are useful and quick methods for creating immutable collections, and JShell provides a good testing ground to learn about the new methods and their associated characteristics.

Factory (object-oriented programming) JShell

Published at DZone with permission of Martin Farrell, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Choosing Between GraphQL Vs REST
  • Synchronization Methods for Many-To-Many Associations
  • Application Scalability — How To Do Efficient Scaling
  • Making Machine Learning More Accessible for Application Developers

Comments

Java Partner Resources

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