How to initialize Key Class in HashMap in Java
<p>I have a question related to Key Type initialization in HashMap. For example, I have defined the below Node class with over-ridden equals() and hashCode() as follows:</p>

<pre><code>public class Node implements Comparable&lt;Node&gt; {
    private int Id;
    public Node(int i) {
    ...
    }
    void setId(int i) {
        Id = i;
    }
    int getId() {
        return Id;
    }
    @Override
    public boolean equals(Object o) {
        if (o == null) {
            throw new NullPointerException();
        }
        if (o instanceof Node &amp;&amp; this != o) {
            if (((Node) o).getId() == this.getId())
                return true;
        }
        return false;
    }
    public int hashCode() {
        return Id;
    }
}
</code></pre>

<p>Now I am building a HashMap with key as type Node as follows:</p>

<pre><code>public class AdjList {

    public HashMap&lt;Node,Double&gt; adj;
    public AdjList() {
        adj = new HashMap&lt;Node,Double&gt;(maxSize);
    }
    ...
}
</code></pre>

<p>As you can possibly figure out, I am trying to generate a graph adjacency list with the node class as HashMap.</p>

<p>Now, my question is when I call AdjList() constructor where I create a new HashMap with some maxSize, will it initialize the Node() class as key type? Or I need to separately initialize Node() clas for the key? If I need to initialize Node() in AdjList constructor, then how it can be possible?</p>

<p>Any suggestion will be valuable and useful suggestions will be rewarded.</p>

<p>Thanks,
Somnath</p>
