I found in the util.TreeSet
class that one of the constructor is calling another constructor with a new TreeMap
with empty generic type.
public TreeSet(Comparator<? super E> comparator) {
this(new TreeMap<>(comparator));
}
What does new TreeMap<>
mean ? is that equivalent to new TreeMap<?>
?
Diamond Operator: Diamond operator was introduced in Java 7 as a new feature. The main purpose of the diamond operator is to simplify the use of generics when creating an object.
The diamond operator – introduced in Java 1.7 – adds type inference and reduces the verbosity in the assignments – when using generics: List<String> cars = new ArrayList<>(); The Java 1.7 compiler's type inference feature determines the most suitable constructor declaration that matches the invocation.
Generics means parameterized types. The idea is to allow type (Integer, String, … etc., and user-defined types) to be a parameter to methods, classes, and interfaces. Using Generics, it is possible to create classes that work with different data types.
It's called diamond operator and infers the generic type from the left hand side, if possible.
This is Java 7 syntax. The diamond (<>
) is a short-hand to ask the Java compiler to fill generic arguments with whatever makes sense in the local context (in this case, it'll be ? super E
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With