0

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:

public class Node implements Comparable<Node> {
    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 && this != o) {
            if (((Node) o).getId() == this.getId())
                return true;
        }
        return false;
    }
    public int hashCode() {
        return Id;
    }
}

Now I am building a HashMap with key as type Node as follows:

public class AdjList {

    public HashMap<Node,Double> adj;
    public AdjList() {
        adj = new HashMap<Node,Double>(maxSize);
    }
    ...
}

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

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?

Any suggestion will be valuable and useful suggestions will be rewarded.

Thanks, Somnath

0

2 Answers 2

1

when I call AdjList() constructor where I create a new HashMap with some maxSize, will it initialize the Node() class as key type?

No! You are instantiate the Map with initialCapacity which is the loadFactor value not maxSize (See the documentation).

You may define a method in AddList that adds an entry.

public void add(int i,Double d)
{
  adj.put(new Node(i),d);
} 

Second, you've implemented Comparable so you must have to define the compareTo method.

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

1 Comment

yes you are right! I have deliberately removed certain parts from my code scriplet as I wanted to make it more readable.
1

There are a number of things that confuse me about your question (even after reading about adjacency lists which I had not heard of before). Firstly why is overriding equals and hash map important? Secondly how does this code form an adjacency list (reading http://en.wikipedia.org/wiki/Adjacency_list) I could not see the relationship between what you have and an adjacency list. I'm also not sure what you are asking when talking about initialising the Node class. It sounds like yoga re confused about some things, I'm just not sure what.

Finally, based on what I read about adjacency lists, I would simply use the following code:

Map<int, List<int>> adjList = new HashMap<int, List<int>>();

Then I can store node 1 with a adjacency list of nodes 2 and 3 as:

adjList.put(1, Arrays.aslist(2,3));

and retrieve then with:

adjList.get(1);

etc, etc. No need for any custom classes at all.

5 Comments

No, actually I need to store some floating value for each node along with the list of neighbors for that node. So it is not exactly adjacency list. That's the reason, I need custom class.
Also a question on your answer: Can Map or List the Collection Types have primitive types like int or accept Integer only?
Maps and Lists will auto box like everything else. So you can define List<int> or List<Integer> and it will all work. The only time it really matters is if you want to access methods on the Integer class. Personally I don't like triggering instantiation of objects unless necessary so I prefer primitives generally. But it all comes down to the application and what you need it to do. Sometimes classes are a better option.
As for the other part about other values and needing a class - Would something like Map<Node, List<Node>> adjList = .... do the job?
yes, it should be like that if it was an exact adjacency list, which it's not as I need to store some Double Value as well for each node. So, I have made the Key as <Node,List<Node>> and Val as Double. So that, I can generate easy hashCode using just Node Id. -Somnath

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.