Diamond Operator in Java

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

April 17, 2017 · 1 min · Rezha Julio