LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-15-2007, 12:54 PM   #1
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 704

Rep: Reputation: 30
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
    }
 
Old 04-15-2007, 01:48 PM   #2
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 704

Original Poster
Rep: Reputation: 30
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?
 
Old 04-15-2007, 03:25 PM   #3
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 40
You need to implement the hashCode :: int method in your class. Hashes are computed based on this value, not the value of equals :: bool.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Where is java.util.Scanner? pda_h4x0r Programming 8 11-12-2007 03:53 PM
java.util.*; in Linux? vmlinuz.gz Programming 3 04-05-2006 02:26 PM
Does HashMap.get(Object) use the method equals? eantoranz Programming 10 12-03-2004 07:05 PM
Updated Java... now java apps dont work zidane2010 Linux - Software 6 05-04-2004 08:02 PM
Java can't find package, but PATH set correctly (j2se1.4.0, java.util.regex package) Ethan Programming 5 02-06-2004 09:55 PM


All times are GMT -5. The time now is 11:12 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration