package
com.JavaInterview.Programs;
import java.util.Hashtable;
import java.util.Set;
/**
* Perform Basic Operation
on Hashtable
*
*
*/
public class HashTableBasicJIP {
public static void
main(String args[]) {
// Create Hashtable Instance
Hashtable<Integer,
String> JipHT = new Hashtable<Integer, String>();
// Adding element to
Hashtable
JipHT.put(1,
"first");
JipHT.put(2,
"second");
JipHT.put(3,
"third");
// Print the Hashtable
System.out.println("Original Hashtable :"+JipHT);
// Adding element by duplicate key
JipHT.put(2,
"six");
System.out.println("Hashtable post duplicate key" + JipHT);
// Adding element by duplicate value
JipHT.put(33,
"third");
System.out.println("Hashtable post duplicate value" + JipHT);
// getting value for given key
System.out.println("value for the key '1'" + JipHT.get(1));
// Check if Hashtable is empty
System.out.println("Is Hashtable empty:" + JipHT.isEmpty());
// Remove element from Hashtable
JipHT.remove(1);
System.out.println("Hashtable
post removal of its elemnet " + JipHT);
// Size of the Hashtable
System.out.println("Size of Hashtable:" + JipHT.size());
//Iterate through the
Hashtable
Set<Integer>
keys=JipHT.keySet();
for(Integer key:keys)
{
System.out.println("Value
of "+key +" is "+JipHT.get(key));
}
}
}
Output:
Original Hashtable :{3=third,
2=second, 1=first}
Hashtable post duplicate
key{3=third, 2=six, 1=first}
Hashtable post duplicate
value{3=third, 2=six, 1=first, 33=third}
value for the key '1'first
Is Hashtable empty:false
Hashtable post removal of its
elemnet {3=third, 2=six, 33=third}
Size of Hashtable:3
Value of 3 is third
Value of 2 is six
Value of 33 is third
No comments:
Post a Comment