Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Diamond Generic and Legacy in Java

Tags:

java

generics

In Collections I've found the following:

@SuppressWarnings("unchecked")
public static final List EMPTY_LIST = new EmptyList<>();

I really can't find any reason to use:

new EmptyList<>()

here instead of:

new EmptyList()

as it would be for java < 1.7.

Is there any difference between this approach?

like image 465
krems Avatar asked Dec 07 '25 08:12

krems


1 Answers

If you are concerned only about <> operator then you should see What is the point of the diamond operator in Java 7?.

Regarding EMPTY_LIST: Either you do :

@SuppressWarnings("rawtypes")
List list = Collections.EMPTY_LIST;

OR Something like:

List<String> s = Collections.emptyList();

The reason to use <> operator is maintaining compile time checking for raw types. The above links describes that well.

like image 147
Raju Singh Avatar answered Dec 08 '25 22:12

Raju Singh