The use of the diamond operator simplifies the use of generics when creating an object. It avoids unchecked warnings in a program, and it reduces generic verbosity by not requiring explicit duplicate specification of parameter types. Instead, the compiler infers the type.
To use the diamond operator:
This diamond syntax <>
simplify how we instantiate generic type variables. In the previous
version of Java when declaring and instantiating generic types we’ll do
it like the snippet below:
List and the Map on both left and the right
side. By using the diamond syntax the compiler will infer the type of
the right side expression argument automatically. So in Java 7 we can
write the above code snippet like this:This make our code simpler and more readable and by using the diamond syntax the compiler will ensure that we have the generic type safe checking available in our code. This will make any error due to type incompatibility captured at the compile time.