Thursday, 14 January 2016

Hash Table in Java collections

++++++    Hash Table    ++++++++++++++
- HashTable is an array of list. Each list is know as bucket. The position of bucket is identified by calling the hascode() method.
- HashTable contains values based on the Key.
- it contains only Uniq elements.
- It may not have any null key or value.
- It is Synchronized

Sample Code for HashTable:
package collections;

import java.util.Hashtable;
import java.util.Map;

public class HashTable_Test {
    public static void main(String[] args) {
        Hashtable<Integer,String> ht=new Hashtable<Integer,String>();
        ht.put(100,"pabbu");
        ht.put(200, "reddy");
        ht.put(300,"pabbu");
        ht.put(100,"abcd");
        ht.put(400," ");       
       
        for(Map.Entry m:ht.entrySet())
                {
            System.out.println(m.getKey()+" "+m.getValue());
                }
    }

}

Output:
400 
300 pabbu
200 reddy
100 abcd

No comments:

Post a Comment