Java 9 features – Collections Update

Finally, Java 9 is out and it’s high time to go through and discuss the issues of previous JDK releases and discuss the benefits of recently release JDK 9 features. Changing in Collections is one of the features that is of particular interest to developers and is discussed in this article.

Benefits of Collections

Widely used in java and helps to gather data in many ways in your applications

Provides various useful ways to manipulate the data

Plenty of collection interfaces that represent the abstract concepts of a List, Set and Map

Issues with Collections in Java 8

There is no simple way to create a collection with predefined data.

Some extra line of code is required if you want a collection to be immutable

Here are examples to make a collection immutable in Java 8.
1. List Example

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

packagesafevar;

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

publicclassImmutableExample{

publicstaticvoidmain(String[]args){

List list=newArrayList&lt;&gt;();

list.add("One");

list.add("Two");

list.add("Three");

list.add("Four");

list=Collections.unmodifiableList(list);

System.out.println("Java 8 list : "+list.toString());

//An error will occur. You can't add, update or delete

list.add("Five");

}

}

2. Map Example

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

packagesafevar;

import java.util.Collections;

import java.util.HashMap;

import java.util.Map;

publicclassImmutableExample{

publicstaticvoidmain(String[]args){

//Let's start with Map

//Java 8.

Mapm=newHashMap&lt;Integer,String&gt;();

m.put(1,"One");

m.put(2,"Two");

m=Collections.unmodifiableMap(m);

System.out.println(m.toString());

//m.put(3, "Three");

}

}

In order to create an immutable list of four values, one need six lines of code. Let’s have a look how JDK 9 resolved this issue.

New Methods for Collections

1. List Example

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

packagesafevar;

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

publicclassImmutableExample{

publicstaticvoidmain(String[]args){

//Java 9 feature is : You can create the same immutable list by this way

List list=List.of("One","Two","Three","Four");

System.out.println("Java 9 feature: "+list.toString());

//You can't add or delete or update

// lets try here

list.add("Five");

}

}

2. Map Example

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

packagesafevar;

import java.util.Collections;

import java.util.HashMap;

import java.util.Map;

publicclassImmutableExample{

publicstaticvoidmain(String[]args){

//Let's start with Map

//Java 9 feature is. You can make a Map immutable

Map&lt;Integer,String&gt;m1=Map.of(1,"One",2,"Two",3,"Three");

System.out.println("Java 9 Map feature : "+m1.toString());

//m1.put(4, "Four");

}

}

Java 9 also introduced a set of new methods to the Collections Framework. Let’s start by looking at the problem these
methods are trying to solve, by instantiating an array of String with a few values and converting array to a list.
Problem :

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

packagesafevar;

import java.util.Arrays;

import java.util.List;

publicclassArrayExample{

publicstaticvoidmain(String[]args){

Stringstr[]=newString[2];

str[0]="A";

str[1]="B";

List list=Arrays.asList(str);

System.out.println(list.toString());

list.add("C");

}

}

Solution :

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

packagesafevar;

import java.util.Arrays;

import java.util.HashSet;

import java.util.List;

import java.util.Set;

publicclassArrayExample{

publicstaticvoidmain(String[]args){

Stringstr[]=newString[2];

str[0]="A";

str[1]="B";

Sets=newHashSet&lt;&gt;(Arrays.asList(str));

s.add("C");

System.out.println(s.toString());

}

}

If you will try to add another value into the list it will throw java.lang.UnsupportedOperationException Exception. To resolve this issue

Static Entry Method for Maps

Handling varargs in Maps is bit tricky because you need both keys and values. Therefore, JDK 9 introduce a varargs method Map.Entry<K,V> and adding a static entry methods that constructs it. Find the example below.

JDK 9 also introduced some additional methods so that you can’t pass duplicate arguments when you create a Set and can’t you pass duplicate keys when you create Map. A null value is not allowed for any collection method. We will cover these methods at later stages. Please write your suggestion in comment box.