java: java.util.Hashmap doesn't work for a custom object
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
java: java.util.Hashmap doesn't work for a custom object
Hi,
i have created a class named ID. I am trying to test it on a Hashmap, but the the "containsKey" and "get" operations do not work. Every operation of the main method returns as if the ID objects have not been inserted. Ideas?
Code:
package swarm.sys.id;
import java.util.*;
import java.io.*;
import swarm.sys.common.*;
import swarm.sys.interfaces.*;
public class ID implements IDInterface, Cloneable, Serializable
{
String type;
int body;
String id;
private static int idCounter=0;
public ID(String type, int body)
{
this.type=type;
this.body=body;
id=type+Ids.padWithZeroes(body);
}
public String getType()
{
return type;
}//getType
public String getBody()
{
return Ids.padWithZeroes(body);
}//getBody
public int getIntBody()
{
return body;
}//getIntBody
public boolean equals(Object o)
{
if (!(o instanceof ID))
{
return false;
}
else
{
ID id=(ID)o;
return (type.equals(id.getType()) && (body==id.getIntBody()));
}
}//equals
/* factory method, which produces new IDs and afterwards
* increments the body part
*/
public static ID newID(String type)
{
return new ID(type,idCounter++);
}//newID
//implements deep cloning
public Object clone()
{
return new ID(new String(this.type),this.body);
}//clone
public String toString()
{
return id.trim();
}//toString
public static void main(String[] args)
{
Hashtable<IDInterface,String> table=new Hashtable<IDInterface,String>();
table.put(new ID("PEER_",1),"x1");
table.put(new ID("PEER_",2),"x1");
table.put(new ID("PEER_",3),"x2");
table.put(new ID("PEER_",4),"x3");
table.put(new ID("PEER_",5),"x3");
ID id1=new ID("PEER_",1);
ID id14=new ID("PEER_",14);
System.out.println("table.containsKey(new ID(PEER_,1)): "+table.containsKey(id1)); //should be true
System.out.println("table.containsKey(new ID(PEER_,14)): "+table.containsKey(id14)); //should be false
System.out.println("table.get(new ID(PEER_,1))"+table.get(id1)); //should be x1
}
The problem would be solved if the ids that we search for and the ids used as keys inside the hashtable were references to each other and not different objects, ie:
Code:
public static void main(String[] args)
{
ID id1=new ID("PEER_",1);
ID id14=new ID("PEER_",14);
Hashtable<IDInterface,String> table=new Hashtable<IDInterface,String>();
//table.put(new ID("PEER_",1),"x1");
table.put(id1,"x1");
table.put(new ID("PEER_",2),"x1");
table.put(new ID("PEER_",3),"x2");
table.put(new ID("PEER_",4),"x3");
table.put(new ID("PEER_",5),"x3");
System.out.println("table.containsKey(id1): "+table.containsKey(id1)); //should be true
System.out.println("table.containsKey(new ID(PEER_,14)): "+table.containsKey(id14)); //should be false
System.out.println("table.get(new ID(PEER_,1))"+table.get(id1)); //should be x1
}
I think i have to implement an equals method somewhere. I have implemented it inside the ID class, but the Hashtable obviously doesn't use it... Can somebody explain how should i do it?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.