package com.kt4study.corejava.collection;
import java.util.Hashtable;
/**
* @author kt4study.blogspot.co.uk
*
*/
public class HashtableExample {
public static Hashtable<Integer, String> getHashtable() {
// Create An Instance Of Hashtable
Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>();
// Add Integer as Key and String as Value in Hashtable.
hashtable.put(11, "Eleven");
hashtable.put(1, "One");
hashtable.put(6, "Six");
//hashtable.put(null, null); //Hashtable can't have "null" key and "null" value
return hashtable;
}
public static void main(String[] args) {
Hashtable<Integer, String> hashtable = HashtableExample.getHashtable();
// Print all the elements From Hashtable
for (Integer key : hashtable.keySet()) {
System.out.println("Element from Hashtable:: " + key + " - " + hashtable.get(key));
}
}
}
|
Output → Hashtable can't have "null" key and "null" value. All keys are unique in Hashtable.
Element from Hashtable:: 6 - Six
Element from Hashtable:: 1 - One
Element from Hashtable:: 11 - Eleven
No comments:
Post a Comment