0
class Node {
    long data;
    Node node;
    int rank;
}
  1. private Map<Long, Node> map = new HashMap<>();

  2. private Map<Long, Node> map = new HashMap<Long,Node>();

I am implementing hashmap of class Node

My question is the validity of the 2 above mentioned Hashmaps.(whether both are coreect or not?). If they are valid what is the difference in the two initializations?

Héctor
  • 24,444
  • 35
  • 132
  • 243

2 Answers2

3

According the docs: https://docs.oracle.com/javase/tutorial/java/generics/types.html

In Java SE 7 and later, you can replace the type arguments required to invoke the constructor of a generic class with an empty set of type arguments (<>) as long as the compiler can determine, or infer, the type arguments from the context. This pair of angle brackets, <>, is informally called the diamond.

From Java 7, both are correct.

Héctor
  • 24,444
  • 35
  • 132
  • 243
0

Both are valid ways of creating Maps but following is the latest where we don't need to declare the datatype on the right. private Map map = new HashMap<>();

Basically Java is moving towards being a less verbose language.

Rakesh
  • 4,004
  • 2
  • 19
  • 31