The Hashtable works like HashMap. Only diffrence is that, all methods are synchronized



package collection.demos;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;
public class HashTableDemo {
    public static void main(String[] args) {
        Map<String,Long> phoneNumbers=new Hashtable<String,Long>();
        //Putting values in HashMap
        phoneNumbers.put("Abc"12345674L);
        phoneNumbers.put("xyz"453453344L);
        phoneNumbers.put("Shoye"23423433L);
        phoneNumbers.put("Jusd"656565653L);
        phoneNumbers.put("Korp"775554343L);
        System.out.println("Values : "+phoneNumbers);
        //getting value for specific key
        System.out.println("Value of key xyz : "+phoneNumbers.get("xyz"));
        //checking for specific key
        System.out.println("There is a value of key xyz "+phoneNumbers.containsKey("xyz"));
        //checking for specific Object
        System.out.println("There is a value 453453344L "+phoneNumbers.containsValue(453453344L));
        //getting collection of values
        Collection<Long> phones=phoneNumbers.values();
        System.out.println("Collection values : "+phones);
        System.out.println("Size of HashMap : "+phoneNumbers.size());
        //Removing the element associated with specified key
        phoneNumbers.remove("Jusd");
        System.out.println("Values after removed : "+phoneNumbers);
        //Getting set of keys
        Set<String> keys=phoneNumbers.keySet();
        System.out.println("Set of Keys : "+keys);
        //Getting entryset of mappings
        Set<Map.Entry<String,Long>> entryset=phoneNumbers.entrySet();
        System.out.println("Entry set : "+entryset);
        //Removing all entries
        phoneNumbers.clear();
        System.out.println("Values after cleared : "+phoneNumbers);
    }
}

No comments:

Popular Posts