mercoledì 12 febbraio 2014

Diamond

Generics arrived with Java SE 5 with a rather verbose syntax. Java SE 7 brought a slightly lighter notation, called diamond, which does not repeat the declaration in the instantiation of an object. The following code gives an example of declaring generics both with and without the diamond operator.

Example: Declaring Generics with and Without Diamond
// Without diamond operator
List<String> list = new ArrayList<String>();
Map<Reference<Object>, Map<Integer, List<String>>> map =
new HashMap<Reference<Object>, Map<Integer, List<String>>>();

// With diamond operator
List<String> list = new ArrayList<>();
Map<Reference<Object>, Map<Integer, List<String>>> map = new HashMap<>();

Nessun commento:

Posta un commento