1

is there any difference between :

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

and

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

Thanks!

Sreenath S
  • 1,269
  • 1
  • 14
  • 21
Huy Than
  • 1,538
  • 2
  • 16
  • 31

3 Answers3

2

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().

cyon
  • 9,340
  • 4
  • 22
  • 26
1

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

Elchin
  • 584
  • 6
  • 19
  • Although note that since Generics are only used at compile-time, and don't exist in the byte-code, code using only Java-6 features plus the diamond operator, and compiled with a Java 7 compiler will run on a Java 6 JVM. – Edd Feb 01 '13 at 10:34
1

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

Priyank Doshi
  • 12,895
  • 18
  • 59
  • 82