What is Hashtable?
Hashtable class implements a hashtable, which maps keys to values.
Any non-null object can be used as a key or as a value.
To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method.
How to perform Basic Operations in Hashtable?
The Basic operations are:
- creating hashtable object
- adding key-value pair
- getting the value based on key
- checking hashtable is empty or not
- removing an element
- size of the hashtable
Basic Operations
import java.util.Hashtable; public class Basic { public static void main(String a[]){ HashtableTry it Yourselfht = 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?
We can iterate through each and every element by getting all keys as set object.
Using each element as a key , we can get values from Hashtable.
Example
import java.util.HashMap; import java.util.Hashtable; public class Read { public static void main(String a[]){ HashtableTry it Yourselfhm = 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?
putAll() method is used to copy another collection object.
Example
import java.util.HashMap; import java.util.Hashtable; public class Copy { public static void main(String a[]){ HashtableTry it Yourselfhm = 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?
The method containsKey() is used to find whether the specified key exists or not.
Example
import java.util.Hashtable; public class KeySearch { public static void main(String a[]){ HashtableTry it Yourselfhm = 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?
The method containsValue() helps us to find whether the specified value exists or not.
Example
import java.util.Hashtable; public class ValueSearch { public static void main(String a[]){ HashtableTry it Yourselfhm = 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"); } } }
How to get all keys from Hashtable?
keySet() method returns Set object will all keys.
Example
import java.util.Hashtable; import java.util.Set; public class tableKeys { public static void main(String a[]){ HashtableTry it Yourselfhm = 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?
Entry object provides getter methods to access key-value objects.
entrySet() method returns Set object with Entry values.
Example
import java.util.Hashtable; import java.util.Set; import java.util.Map.Entry; public class EntrySet { public static void main(String a[]){ HashtableTry it Yourselfhm = 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?
By calling clear() method, we can remove all elements from Hashtable at once.
Example
import java.util.Hashtable; public class Clear { public static void main(String a[]){ HashtableTry it Yourselfhm = 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); } }
Solve this!!
#20976,20977,20978- Collection
- ArrayList
- LinkedList
- Vector
- HashSet
- Conditional Statements
- Classes and Objects
- Inheritance
- Packages
- Exception Handling
- Threads
- TreeSet
- LinkedHashSet
- HashMap
- TreeMap
- LinkedHashMap
- HashTable
- Iterator and ListIterator
- String
- Scanner
- Array Small Programs
- Introduction
- Variables
- DataTypes
- Operators
- Conditional Statements
- Loop
- Arrays
- Object Oriented Programming
- Classes and Objects
- Access Modifiers
- Methods
- Abstraction
- Constructor
- Packages
- StringBuffer
- Inheritance
- StringBuilder
- Encapsulation
- Java IO
- Polymorphism
- Interface
- Types Of Variables
- Static Keyword
- Final Keyword
- Super Keyword
- This Keyword
- Exception Handling