Monday, January 12, 2015

Java Elements from Hashtable Program



How to get all keys from Hashtable

package com.java2novice.hashtable;
import java.util.Hashtable;
import java.util.Set;
public class MyHashtableKeys
          {
public static void main(String a[])
{
        Hashtable hm = new Hashtable();
        hm.put("first", "First inserted");
        hm.put("second", "Second inserted");
        hm.put("third","Third inserted");
        System.out.println(hm);
        Set keys = hm.keySet();
        for(String key: keys)
       {
            System.out.println(key);
        }
    }
}

How to get entry set from Hashtable

package com.java2novice.hashtable;
import java.util.Hashtable;
import java.util.Set;
import java.util.Map.Entry;
public class MyHashtableEntrySet
{
public static void main(String a[])
{
        Hashtable hm = new Hashtable();
        hm.put("first", "First inserted");
        hm.put("second", "Second inserted");
        hm.put("third","Third inserted");
        System.out.println(hm);
        Set> entires = hm.entrySet();
        for(Entry ent:entires)
        {
            System.out.println(ent.getKey()+" ==> "+ent.getValue());
        }
    }
}

How to delete all elements from Hashtable

package com.java2novice.hashtable;
import java.util.Hashtable;
public class MyHashtableClear
{
public static void main(String a[])
{
        Hashtable hm = new Hashtable();
        hm.put("first", "First inserted");
        hm.put("second", "Second inserted");
        hm.put("third","Third inserted");
        System.out.println("My Hashtable content:");
        System.out.println(hm);
        System.out.println("Clearing Hashtable:");
        hm.clear();
        System.out.println("Content After clear:");
        System.out.println(hm);
    }
}

How to read elements using Enumeration in Hashtable

package com.java2novice.hashtable;
import java.util.Enumeration;
import java.util.Hashtable;
public class MyHashtableEnumaration
{
public static void main(String a[])
{     
        Hashtable hm = new Hashtable();
        hm.put("first", "First inserted");
        hm.put("second", "Second inserted");
        hm.put("third","Third inserted");
        Enumeration keys = hm.keys();
        while(keys.hasMoreElements())
        {
            String key = keys.nextElement();
            System.out.println("Value of "+key+" is: "+hm.get(key));
        }
    }
}

Hashtable implementation with equals and hashcode

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;
        }
    }
}

No comments:

Post a Comment