1

Hi Guys I have a question about HashMap declaration in Java. I'd like to know what is the difference between:

HashMap<String,Integer> map = new HashMap<>();

and

HashMap<String,Integer> map = new HashMap<String,Integer>();

Most coding websites teach the second one. However, the first one works perfectly fine. Is there a reason we should use second one?

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
FNZ
  • 27
  • 1
  • 1
  • 6
  • Basically this is an indication the coding website you are using ... teaches you seriously outdated content. – GhostCat Oct 22 '20 at 11:23
  • @GhostCat I am using www.w3schools.com which is fairly reasonable. Do you have any suggestion for a better source? – FNZ Oct 22 '20 at 11:29
  • As said, it is an indication. I would just be careful and maybe research occasionally what they are putting up. – GhostCat Oct 22 '20 at 11:40

3 Answers3

1

There is no difference between the two of those since form the Java 7. Previously before Java 7 you must mention the Generic type parameters like in your second case in both sides.But to make it easier so that developer can write less code after Java 7 only you need to define it once in the left side.

Before Java Version 7 you need to code like below.

HashMap<String,Integer> map = new HashMap<String,Integer>();//For java versions below 1.7

After Java version 7

HashMap<String,Integer> map = new HashMap<>();//For java versions above 1.7
Hasindu Dahanayake
  • 1,344
  • 2
  • 14
  • 37
1

There is technically none, however since Java 1.7 it's a convention to use this <> diamond operator, it reduces the text size.

Ecto
  • 1,125
  • 1
  • 7
  • 13
1

This is called Type Inference.

Compilers from releases prior to Java SE 7 are able to infer the actual type parameters of generic constructors, similar to generic methods. However, compilers in Java SE 7 and later can infer the actual type parameters of the generic class being instantiated if you use the diamond (<>).

  • new HashMap<String,Integer>() - The type parameter is mandatory before Java 7
  • new HashMap<>() - The type parameter is inferred as of Java 7

It's recommended to use the "diamond operator" (<>) due to the brevity. Effectively, there is no difference.

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
  • Also recommended to not answer obvious duplicated questions. – GhostCat Oct 22 '20 at 11:22
  • @GhostCat: Are even the "community wiki" answers discouraged (I've marked it as one after your comment) in such case? My pure intention is to help the OP, that's what i enjoy here. If the question is closed sooner or later, let it happen. – Nikolas Charalambidis Oct 22 '20 at 15:43
  • 1
    Well. When a duplicate question is asked, the recommended policy is to close vote accordingly. This is less about reputation, more about keeping the place "tidy". – GhostCat Oct 22 '20 at 18:07
  • Thanks for the explanation. Nothing offensive, just out of curiosity: How the answer makes the place less tidy when we put the closing aside for a while (which I fully agree with). – Nikolas Charalambidis Oct 22 '20 at 18:10
  • @NikolasCharalambidis because it’s not in the same place as the other answers for this problem, where it could be ranked more easily for helpfulness. Also, if the OP were to regret their having posted a duplicate question, having answers makes it harder for them to do so. – Martijn Pieters Oct 22 '20 at 23:17