Monday, January 12, 2015

Java Using Hashtable Program



Basic Hashtable Operations

package com.java2novice.hashtable;
import java.util.Hashtable;
public class MyHashtableOperations
{
public static void main(String a[])
{
 Hashtable ht = new Hashtable();
 ht.put("first", "First inserted");
        ht.put("second", "Second inserted");
        ht.put("third","Third inserted");
        System.out.println(ht);
        System.out.println("Value of key 'second': "+ht.get("second"));
        System.out.println("Is Hashtable empty? "+ht.isEmpty());
        ht.remove("third");
        System.out.println(ht);
        System.out.println("Size of the Hashtable: "+ht.size());
    }
}

How to iterate through Hashtable

package com.java2novice.hashtable;
import java.util.Hashtable;
import java.util.Set;
public class MyHashtableRead
{
 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("Value of "+key+" is: "+hm.get(key));
        }
    }
}

How to copy Map content to another Hashtable

package com.java2novice.hashtable; 
import java.util.HashMap;
import java.util.Hashtable;
public class MyHashtableCopy
{
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);
        HashMap subMap = new HashMap();
        subMap.put("s1", "S1 Value");
        subMap.put("s2", "S2 Value");
        hm.putAll(subMap);
        System.out.println(hm);
    }
}

How to search a key in Hashtable

package com.java2novice.hashtable;
import java.util.Hashtable;
public class MyHashtableKeySearch
{
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);
        if(hm.containsKey("first"))
        {
            System.out.println("The Hashtable contains key first");
        }
        else
        {
            System.out.println("The Hashtable does not contains key first");
        }
        if(hm.containsKey("fifth"))
        {
            System.out.println("The Hashtable contains key fifth");
        }
         else
       {
            System.out.println("The Hashtable does not contains key fifth");
        }
    }
}

How to search a value in Hashtable

package com.java2novice.hashtable;
import java.util.Hashtable;
public class MyHashtableValueSearch
{
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);
        if(hm.containsValue("Second inserted"))
       {
            System.out.println("The Hashtable contains value Second inserted");
        }
        else
      {
            System.out.println("The Hashtable does not contains value Second inserted");
        }
        if(hm.containsValue("first"))
       {
            System.out.println("The Hashtable contains value first");
        }
        else
      {
            System.out.println("The Hashtable does not contains value first");
        }
    }
}

No comments:

Post a Comment