Social Media

Category Archives for Java

Java9 JShell Examples : Collections Static Factory Methods

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

Collections Static Factory Methods

Java9 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

Note the inference as a List object

To static List of (E e1, E e2, E e3) –

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

Note the inference as a List object

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

static List of (E… elements)

Map

Similarly Map defines –

  • 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

Characteristics of Collections Static Factory Methods

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

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

jshell> immutableSet.add("four")
| java.lang.UnsupportedOperationException thrown:
  • No Nulls – NullPointerException thrown
jshell> List 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 notNullList = List.of(null);
| ^--^
| java.lang.NullPointerException thrown:
| at List.of (List.java:1030)
| at (#10:1)
  • Serialized – Serialized if elements Serializable

List Specific Characteristics

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

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 Keus – The Map will reject duplicate keys with IllegalArgumentException –
jshell> Map.of("key1", "value1", "key1", "value2")
| java.lang.IllegalArgumentException thrown: duplicate key: key1
| at ImmutableCollections$MapN. (ImmutableCollections.java:680)
| at Map.of (Map.java:1326)
| at (#15:1)
  • Iteration is also not guaranteed

Conclusions

These are a useful and quick method for creating immutable collections, and jshell provides a good test ground to learn about the new methods and their associated characteristics

Copyright 2013-2019 by javabullets.com   -  Designed by Thrive Themes | Powered by WordPress

Connect With Me: