Java 9 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<Object>
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<String>
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 –
- staticMapof ()
- staticMapof (K k1, V v1) to (K k10, V v10)
- staticMapofEntries (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 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 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 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.<init> (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
Published on Java Code Geeks with permission by Martin Farrell, partner at our JCG program. See the original article here: Java9 JShell Examples : Collections Static Factory Methods Opinions expressed by Java Code Geeks contributors are their own. |