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.

Some of the previous collections related posts are given below:

Collections in Java

Map Interface in Java

ArrayList in Java

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

package safevar;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ImmutableExample {

	public static void main(String[] args) {
		List list = new ArrayList<>(); 

		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

package safevar;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class ImmutableExample {

	public static void main(String[] args) {

		//Let's start with Map
		//Java 8.
		Map m = new HashMap<Integer, String>();
		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

package safevar;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ImmutableExample {

	public static void main(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

package safevar;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class ImmutableExample {

	public static void main(String[] args) {
		//Let's start with Map
		
		//Java 9 feature is. You can make a Map immutable 
		
		Map<Integer,String> 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 :

package safevar;

import java.util.Arrays;
import java.util.List;

public class ArrayExample {

	public static void main(String[] args) {
		String str[] = new String[2];
		str[0]="A";
		str[1]="B";
		
		List list = Arrays.asList(str);

		System.out.println(list.toString());
		
		list.add("C");
	}

}

Solution :

package safevar;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ArrayExample {

	public static void main(String[] args) {
		String str[] = new String[2];
		str[0]="A";
		str[1]="B";

		Set s = new HashSet<>(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.

Map<String, Integer> m = Map.ofEntries(Map.entry("A",1),Map.entry("B",2));
	
	System.out.println("Using Static Map.entry Method : "+ m.toString());

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.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.