Monday, January 12, 2015

Define object as keys in Hashtable and defined objects with Hashtable in Java program



How to fetch values with user define object as keys in Hashtable

package com.java2novice.hashtable;
import java.util.Hashtable;
public class MyHashtableUserKeys
{
          public static void main(String a[])
          {
        Hashtable tm = new Hashtable();
        tm.put(new Emp(134,"Ram",3000), "Ram");
        tm.put(new Emp(235,"John",6000), "John");
        tm.put(new Emp(876,"Crish",2000), "Crish");
        tm.put(new Emp(512,"Tom",2400), "Tom");
        System.out.println("Fecthing value by creating new key:");
        Emp e = new Emp(512,"Tom",2400);
        System.out.println(e+" ==> "+tm.get(e));
    }
}
class Emp
{
              private String name;
    private int salary;
    private int id;
    public Emp(int id, String n, int s)
   {
        this.id = id;
        this.name = n;
        this.salary = s;
    } 
    public String getName()
   {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public int getSalary()
   {
        return salary;
    }
    public void setSalary(int salary)
    {
        this.salary = salary;
    }
    public String toString()
   {
        return "Id: "+this.id+" -- Name: "+this.name+" -- Salary: "+this.salary;
    }
    public void setId(int id)
   {
        this.id = id;
    }
    public int getId()
   {
        return id;
    }
    public int hashCode()
    {
        System.out.println("In hashcode");
        return this.getId();
    }  
    public boolean equals(Object obj)
    {
        Emp e = null;
        if(obj instanceof Emp)
       {
            e = (Emp) obj;
        }
        System.out.println("In equals");
        if(this.getId() == e.getId())
       {
            return true;
        }
        else  
        {
            return false;
        }
    }
}

How to eliminate duplicate keys user defined objects with Hashtable

package com.java2novice.hashtable;
import java.util.Hashtable;
import java.util.Set;
public class MyHashtableDupEntry
{
           public static void main(String a[])
           {
        Hashtable tm = new Hashtable();
        tm.put(new Empl(134,"Ram",3000), "Ram");
        tm.put(new Empl(235,"John",6000), "John");
        tm.put(new Empl(876,"Crish",2000), "Crish");
        tm.put(new Empl(512,"Tom",2400), "Tom");
        System.out.println("Adding duplicate entry:");
        tm.put(new Empl(512,"Tom",2400), "Tom");
        System.out.println("Hashtable entries:");
        Set keys = tm.keySet();
        for(Empl key:keys)
        {
            System.out.println(key+" ==> "+tm.get(key));
         }
        System.out.println("Duplicate got eliminated!!!");
    }
}
class Empl
{
    private String name;
    private int salary;
    private int id;  
    public Empl(int id, String n, int s)
   {
        this.id = id;
        this.name = n;
        this.salary = s;
    }
    public String getName()
    {
        return name;
    }
    public void setName(String name)
   {
        this.name = name;
    }
    public int getSalary()
   {
        return salary;
    }
    public void setSalary(int salary)
   {
        this.salary = salary;
    }
    public String toString()
   {
        return "Id: "+this.id+" -- Name: "+this.name+" -- Salary: "+this.salary;
    }
    public void setId(int id)
   {
        this.id = id;
    }
              public int getId()
              {
        return id;
    }
    public int hashCode()
    {
        System.out.println("In hashcode");
        return this.getId();
    }
    public boolean equals(Object obj)
    {
        Empl e = null;
        if(obj instanceof Empl)
     {
            e = (Empl) obj;
        }
        System.out.println("In equals");
        if(this.getId() == e.getId())
         {
            return true;
        }
        else
       {
            return false;
        } 
    }
}

No comments:

Post a Comment