Huy Than
Huy Than

Reputation: 1546

Java, Collection constructor

is there any difference between :

TreeMap<String, String> myMap = new TreeMap<>();

and

TreeMap<String, String> myMap = new TreeMap<String,String>();

Thanks!

Upvotes: 1

Views: 2014

Answers (3)

cyon
cyon

Reputation: 9538

They are the same in java 7 where the diamond operator <> was introduced. In older versions of java the diamond operator will not work.

The diamond operator brings type inference to constructors. Type inference on generic methods is available in java 5 and higher. Prior to java 7, to create a generic class using the compiler's type inference you had to use generic factory methods like static <K,T> Map<K,T> createMap().

Upvotes: 2

Priyank Doshi
Priyank Doshi

Reputation: 13151

No difference at all..! Its just a language construct. <> is newly introduced operator known as diamond operator from java 7.

Upvotes: 1

Elchin
Elchin

Reputation: 604

First one will only work in Java 7, the second one from Java 5+

Upvotes: 1

Related Questions