Custom class object as a key in Map


V

Vijay

hi all,

What do I need to do in order to make the following code work.
I expect the output to be "K1 value", but it returns me null.

Map m = new HashMap();
Key k1 = new Key("h","u");
Key k2 = new Key("h","u");
m.put(k1,"K1 value");
System.out.println(m.get(k2));

The Key class is
public class Key {
private String host;
private String user;
public Key(String h, String u) {
host = h;
user = u;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
}
 
Ad

Advertisements

G

Gordon Beaton

What do I need to do in order to make the following code work. I
expect the output to be "K1 value", but it returns me null.

You need to implement hashCode() and equals() methods in your Key
class.

Read the documentation for those methods in java.lang.Object.

/gordon
 
V

Vijay

Even after implementing them, it's the same output remains

public boolean equals(Key k) {
return host.equals(k.getHost()) && user.equals(k.getUser());
}
public int hashCode() {
return 1;
}

Thanks,
 
A

Andrew McDonagh

Vijay said:
Even after implementing them, it's the same output remains

public boolean equals(Key k) {
return host.equals(k.getHost()) && user.equals(k.getUser());
}
public int hashCode() {
return 1;
}

Thanks,

Further to Gordon's message...

thats the worst implementation of hashCode you could possibly do.

For a starter you may want to do...

public int hashCode() {
return host.hasCode + user.hashCode();
}
 
G

Gordon Beaton

Even after implementing them, it's the same output remains

public boolean equals(Key k) {
return host.equals(k.getHost()) && user.equals(k.getUser());

That should be: equals(Object o)

not: equals(Key k)

/gordon
 
B

Betty

Vijay said:
hi all,

What do I need to do in order to make the following code work.
I expect the output to be "K1 value", but it returns me null.

Map m = new HashMap();
Key k1 = new Key("h","u");
Key k2 = new Key("h","u");
m.put(k1,"K1 value");
System.out.println(m.get(k2));

You put k1, but get k2
 
Ad

Advertisements

A

Andrew McDonagh

Betty said:
You put k1, but get k2

Yes Vijay did... the point being the internal values of K1 and K2 are
the Equal, so Vijay thought that it would be possible to get the value
put into the map with K1, by using K2.

However, Vijay didn't realise that unless they over ride the .equals()
method, K1 and K2 are not the equal and so the Map will not find the
value when K2 is used.

As others have suggested, Vijay simply needs to over ride .equals() and
..hashCode() in order to get their code working.
 
V

Vijay

Hi Andrew and Gordon,

Thank you very much for your help. I only needed to change the argument
from Key to Object and it worked. One last thing - Can you suggest me a
better implementation of hashCode in this case. You mentioned the one
you gave as a starter.

Thanks,
 
Ad

Advertisements

A

Andrew McDonagh

Vijay said:
Hi Andrew and Gordon,

Thank you very much for your help. I only needed to change the argument
from Key to Object and it worked. One last thing - Can you suggest me a
better implementation of hashCode in this case. You mentioned the one
you gave as a starter.

Thanks,

the suggestion i posted should be fine. A simple rule of thumb, is to at
least sum the hashcode values of all of the member vars that are used in
the overridden equals() method.

Get the 'Effective Java' book by Joshua Block for further info...it
actually shows you how to write a good equals() and hashcode methods as
well as bad ones.

But one example is to use a combination of what I said above, with Prime
numbers.



public int hashCode() {
int result = 17;

result = 37*result + host.hasCode();
result = 37*result + user.hashCode();

return result;
}

HTH

Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top