10 Example of Hashtable in Java – Java Hashtable Tutorial

Hashtable Examples in Java
Before using Hashtable is worth remembering thatit’san obsolete collection and there are better alternatives available in form of HashMap and ConcurrentHashMap. You can also seeDifference between Hashtable and HashMapfor comparing each other.Examples of hashtable is not very different than hashmap or ConcurrentHashMap and put() and get() almost work same except some synchronization difference e.g. on ConcurrentHashMap get() and put() can overlap but on hashtable they can’t because whole map gets locked on either operation. On the other hand since hashmap is not synchronized at all using it on concurrent java application is not advised.


10 JAVA HASHTABLE EXAMPLES:
Now let’s move on Hashtable examples in Java following is a list of all examples of hashtable operation we will see in this article:


1. How to put object into Hashtable?
2. How to retrieve objectfrom Hashtable in Java?
3. How to reuse Hashtable by using clear()?
4. How to check if Hastable contains a particular value?
5. How to check if Hashtable contains a particular key?
6. How to traverseHashtable in Java?
7. How to check if Hashtable is empty in Java?
8. How to Copy content of Hashtable into HashMap?
9. How to find size of Hashtable in Java?
10. Howto get all values form hashtable in Java?
11. Howto get all keys from hashtable in Java?


Here is complete code example of all above hashtable example or exercises:

importjava.util.Collection;
importjava.util.Enumeration;
importjava.util.Hashtable;
importjava.util.Set;

publicclassHashtableDemo {

publicstaticvoidmain(String args[]) {

// Creating Hashtable for example
Hashtable companies = newHashtable();


// Java Hashtable example to put object into Hashtable
// put(key, value) is used to insert object into map
companies.put("Google", "United States");
companies.put("Nokia", "Finland");
companies.put("Sony", "Japan");


// Java Hashtable example to get Object from Hashtable
// get(key) method is used to retrieve Objects from Hashtable
companies.get("Google");


// Hashtable containsKey Example
// Use containsKey(Object) method to check if an Object exits as key in
// hashtable
System.out.println("Does hashtable contains Google as key: "
+ companies.containsKey("Google"));


// Hashtable containsValue Example
// just like containsKey(), containsValue returns true if hashtable
// contains specified object as value
System.out.println("Does hashtable contains Japan as value: "
+ companies.containsValue("Japan"));


// Hashtable enumeration Example
// hashtabl.elements() return enumeration of all hashtable values
Enumeration enumeration = companies.elements();

while(enumeration.hasMoreElements()) {
System.out
.println("hashtable values: "+ enumeration.nextElement());
}


// How to check if Hashtable is empty in Java
// use isEmpty method of hashtable to check emptiness of hashtable in
// Java
System.out.println("Is companies hashtable empty: "
+ companies.isEmpty());


// How to find size of Hashtable in Java
// use hashtable.size() method to find size of hashtable in Java
System.out.println("Size of hashtable in Java: "+ companies.size());


// How to get all values form hashtable in Java
// you can use keySet() method to get a Set of all the keys of hashtable
// in Java
Set hashtableKeys = companies.keySet();


// you can also get enumeration of all keys by using method keys()
Enumeration hashtableKeysEnum = companies.keys();


// How to get all keys from hashtable in Java
// There are two ways to get all values form hashtalbe first by using
// Enumeration and second getting values ad Collection

Enumeration hashtableValuesEnum = companies.elements();


CollectionhashtableValues = companies.values();


// Hashtable clear example
// by using clear() we can reuse an existing hashtable, it clears all
// mappings.
companies.clear();
}
}


Output:
Does hashtable contains Google as key: true
Does hashtable contains Japan as value: true
hashtable values: Finland
hashtable values: United States
hashtable values: Japan
Is companies hashtable empty: false
Size of hashtable in Java: 3

That’s all on Hashtable Example in Java. We have seen almost all frequently used hashtable operations in java. As I said earlier don’t use Hashtable as its pretty old and there are better alternatives available like ConcurrentHashMap until it’s absolutely necessary for you.


If you enjoyed this article, you might also like..