Skip to content
Rezha Julio
Go back

Diamond Operator in Java

1 min read

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<>();

Related Posts


Previous Post
Using the Deprecated annotation
Next Post
Lambda Functions in Python