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?

1 Answer 1

11

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

Sign up to request clarification or add additional context in comments.

2 Comments

I have one more question why Map<String, Map<String, String>> map = new TreeMap<String, HashMap<String, String>>() produces compile time error whilst Map<String, Map<String, String>> map = new TreeMap<>() does not!
@DhaneshKhurana In Map<String, Map<String, String>> map = new TreeMap<String, HashMap<String, String>>(); the assigned instance doesn't match the type of the variable. It should be Map<String, Map<String, String>> map = new TreeMap<String, Map<String, String>>();

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.