data-driven-docs

Selft training repo


Project maintained by ggranados Hosted on GitHub Pages — Theme by mattgraham

Diamond Operator


Table of Contents


Overview

Before the diamond operator, when creating instances of generic classes, you had to explicitly specify the type parameter twice, once during declaration and once during instantiation:

List<String> myList = new ArrayList<String>(); // Java 6 and earlier

With the diamond operator, the redundant type information can be omitted:

List<String> myList = new ArrayList<>(); // Java 7 and later

The diamond operator works with any generic class, not just ArrayList

Map<String, Integer> myMap = new HashMap<>(); // Java 7 and later
Set<Double> mySet = new HashSet<>(); // Java 7 and later

The Java 1.7 compiler’s type inference feature determines the most suitable constructor declaration that matches the invocation.

Back to top


Ref.

https://www.baeldung.com/java-diamond-operator


Get Started | Languages | Java Development | Java 7