Specifying collection elements type while instance creation

arraylist, collections, java, java-7, list

Solution

There is no difference. However, the first one is legal in Java <= 7 whereas the second one is legal only in Java 7 and was introduced as a short-hand notation*. The compiler will infer the generic type from the declaration.

*It was basically introduced to remove redundant information and reduce code-noise. So you now have:

Map<String, List<String>> myMap = new HashMap<>();

versus:

Map<String, List<String>> myMap = new HashMap<String, List<String>>();

The first one is a lot easier on the eyes.

Problem

Is there any difference between the following declarations - ``` List<String> list = new ArrayList<String>(); ``` and ``` List<String> list = new ArrayList<>(); ``` In both cases anyhow , list will have elements of type String only.

Original source