1.

What Is A Diamond Operator?

Answer»

Diamond operator let the compiler infer the TYPE arguments for the generic classes. It is added in Java 7. 

As Example - Before JDK7 if we had to define a Map using STRING as both Key and VALUE we had to WRITE it like this -

Map<String, String&GT; cityMap = new HashMap<String, String>();

In Java SE 7, you can substitute the parameterized type of the constructor with an empty set of type parameters (<>) known as diamond operator.

Map<String, String> cityMap = new HashMap<>();

Diamond operator let the compiler infer the type arguments for the generic classes. It is added in Java 7. 

As Example - Before JDK7 if we had to define a Map using String as both Key and Value we had to write it like this -

Map<String, String> cityMap = new HashMap<String, String>();

In Java SE 7, you can substitute the parameterized type of the constructor with an empty set of type parameters (<>) known as diamond operator.

Map<String, String> cityMap = new HashMap<>();



Discussion

No Comment Found