3

What is he difference between two approaches of instantiating a map:

  Map<String, String> map = new TreeMap<String, String>();

and

  Map<String, String> map = new TreeMap<>();

and which one is better?

Anders
  • 8,307
  • 9
  • 56
  • 88
Dhanesh Khurana
  • 159
  • 1
  • 13

1 Answers1

11

They are equivalent. The second syntax (known as the diamond operator) was added in Java 7 and allows you to type less code.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • I have one more question why Map> map = new TreeMap>() produces compile time error whilst Map> map = new TreeMap<>() does not! – Dhanesh Khurana Sep 17 '15 at 11:56
  • 2
    @DhaneshKhurana In `Map> map = new TreeMap>();` the assigned instance doesn't match the type of the variable. It should be `Map> map = new TreeMap>();` – Eran Sep 17 '15 at 12:00