Java 9 Collection Improvements

Gowgi
3,285 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Java 9 - Collection Improvements

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, Java 9 introduced 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

  • 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.
List<String> list=List.of("apple","bat");
list.add("cat");
  • It doesn't allow null values. If we try to add null values to any collection, then it throws a null pointer exception.
List<String> list=List.of("apple",null);
  • They are serializable if all the elements are serializable.

List (with Values and empty List)

List.of() Demo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.demo.java9;
import java.util.List;
/**
*
* @author Gowtham Girithar Srirangasamy
* The code to explain the List.of() in java 9.
*/
public class ListOfDemo {
public static void main(String[] args){
List<String> list1=List.of("apple","bat");
List<String> list2= List.of();
System.out.println("** List with values **");
list1.forEach(value-> System.out.println(value));
System.out.println("** List empty **");
list2.forEach(value-> System.out.println(value));
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Set (with Values and empty Set)

Set.of() Demo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.demo.java9;
import java.util.Set;
/**
*
* @author Gowtham Girithar Srirangasamy
* The code to explain the set.of() in java 9.
*/
public class SetOfDemo {
public static void main(String[] args){
Set<String> set1= Set.of("apple","bat");
Set<String> set2= Set.of();
System.out.println("** Set with values **");
set1.forEach(value-> System.out.println(value));
System.out.println("** List empty **");
set2.forEach(value-> System.out.println(value));
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Map (with Values and empty Map)

Map.of() Demo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.demo.java9;
import java.util.Map;
/**
*
* @author Gowtham Girithar Srirangasamy
* The code to explain the Map.of() in java 9.
*/
public class MapOfDemo {
public static void main(String[] args){
Map<Integer,String> emptyMap = Map.of();
Map<Integer,String> map = Map.of(1, "Apple", 2, "Bat", 3, "Cat");
System.out.println("** Empty Map **");
emptyMap.forEach((k,v) -> System.out.println( k +"-"+ v));
System.out.println("** Map with values **");
map.forEach((k,v) -> System.out.println( k +"-"+ v));
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Map.Entry (with Values and empty Map.Entry)

Map.Entry() Demo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.demo.java9;
import java.util.Map;
/**
*
* @author Gowtham Girithar Srirangasamy
* The code to explain the Map.Entry in java 9.
*/
public class MapEntryDemo {
public static void main(String[] args){
System.out.println("** Empty Map Entry **");
Map<Integer,String> emptyEntry = Map.ofEntries();
emptyEntry.forEach((k,v) -> System.out.println(k+"-"+v));
System.out.println("** Map Entry with value **");
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);
mapEntry.forEach((k,v) -> System.out.println(k+"-"+v));
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content