Rezha Julio

The Hard Coded Chemist

Diamond Operator in Java

2017-04-17

Since Java 7 it’s not necessary to declare the type parameter twice while instantiating objects like Maps, Sets and Lists.

Consider the following code:

Map<String, List<String>> phoneBook = new HashMap<String, List<String>>();

The type parameter for HashMap in the right hand side of the expression seems redundant. This can be shortened using an empty “Diamond Operator” to give:

Map<String, List<String>> phoneBook = new HashMap<>();