HashTable Class =============== -> HashTable class is a legacy class. Means: The Java Collection framework was introduced in Java 1.2 version. Before the Java 1.2, to deal with Maps there is a class "HashTable". About HashTable: ================= 1) HashTable can use to map keys to values. 2) No null value object can be used as a key or a value. Ex: null : null or null : 123 or 'a' : null ==> NullPointerException If we can use a null as a key or as a value: HashTable can return "NullPointerException". 3) The HashTable object is "hashCode()" based object. Means, when we can write any key-value pair: hashCode can generated by hashCode() based on this hashCode(), it can detect the bucket index value based on that bucket index value: the HashTable object can store/retrieve. 4) HashTable has defined in the class "java.util.HashTable". 5) HashTable is same as "HashMap" But the HashTable is synchronized". That means we can create only one thread on the HashTable object at a time. 6) HashTable is thread safe object. 7) HashTable can store data in key-value pair in "HashTables". 8) While creating the HashTable object, it can reside with the internal capacity of '11' and load factor of '0.75'. Creation of HashTable object: ============================= Way-01: Creating of an HashTable object with internal capacity and load factor with default values ============================================================================================ package pack.p1; import java.util.Hashtable; public class MainClass { public static void main(String[] args) { Hashtable ht1 = new Hashtable<>(); // ht1 object can be created with default internal capacity of 11 and // with load factor of 0.75 System.out.println("ht1 = "+ht1); ht1.put("apple", 1122); ht1.put("Banana", 1234); ht1.put("Cherry", 1023); System.out.println("ht1 after adding elements = "+ht1); ht1.put("abc", 1024); ht1.put("Java", 17); ht1.put("Python", 196); ht1.put("java", 23); ht1.put("python", 67); ht1.put("a", 1993); ht1.put("b", 1995); ht1.put("C",2005); System.out.println("ht1 = "+ht1); ht1.put("d", 2013); System.out.println("ht1 = "+ht1); } } ================================================== Way-02: Creation of HashTable object with custom internal capacity and with default load factor (0.75) package pack.p1; import java.util.Hashtable; public class Main { public static void main(String[] args) { Hashtable ht2 = new Hashtable<>(4); // here the HashTable object has created with the capacity of '4' // here the load factor ==> 0.75 System.out.println("ht2 = "+ht2); ht2.put(121, "a"); ht2.put(131, "b"); ht2.put(141, "c"); ht2.put(151, "d"); System.out.println("ht2 = "+ht2); ht2.put(161, "e"); System.out.println("ht2 = "+ht2); } } =========================================================== Way-03: Creating of HashTable object with specified/custom internal capacity and load factor ============================================================================================ -> Custom load factor for hash table never be accepted. So, the HashTable can always create with default load factor of '0.75'. ================================================= HashTable Iteration: ==================== package pack.p1; import java.util.Hashtable; import java.util.Map; public class MainClass { public static void main(String[] args) { Hashtable ht1 = new Hashtable<>(); // ht1 object can be created with default internal capacity of 11 and // with load factor of 0.75 System.out.println("ht1 = "+ht1); ht1.put("apple", 1122); ht1.put("Banana", 1234); ht1.put("Cherry", 1023); System.out.println("ht1 after adding elements = "+ht1); ht1.put("abc", 1024); ht1.put("Java", 17); ht1.put("Python", 196); ht1.put("java", 23); ht1.put("python", 67); ht1.put("a", 1993); ht1.put("b", 1995); ht1.put("C",2005); System.out.println("ht1 = "+ht1); ht1.put("d", 2013); System.out.println("ht1 = "+ht1); System.out.println("The Elements of ht1 = "); for(Map.Entry e:ht1.entrySet()) { System.out.println(e.getKey() + "--->"+e.getValue()); } } } ================================================================ Interview Questions: ==================== 1) How the HashTable can work internally? 2) What is differences between HashMap and HashTable? ====================================================== 1) HashMap is a class of Collection framework. Whereas the HashTable is a legacy class. 2) HashMap allows "null" as key and "null" as value whereas the HashTable never accept "null" as key or value. If we can take null as either key or value with HashTable, we can get "NullPointerExcetion". 3) HashMap is not thread safe. But HashTable is thread-safe. 4) HashMap object performance is fast. whereas the HashTable object's performance is slow because it is always working with Synchronized methods. 5) HashMap object can be stored by converting HashMap object into TreeMap object. Whereas the HashTable object elements not to be sorted. Q-3: IS HashMap object mutable or Immutable? Ans: Mutable Q-4: Do we make HashMap object as Immutable? Ans: Yes using synchronizedMap() method we can create the HashMap object as immutable object. Q-5: Is HashMap object thread-safe? Ans: No Q-6: Do we make HashMap object as thread safe? Ans: Yes using unmodifiableMap() method we can create the HashMap object as thread safe object. ====================================================================== Assignment: ========== WAP IN JAVA TO CREATE THE MAP OBJECT USING HASHTABLE CLASS AND ADD ELEMENTS INTO MAP OBJECT AND PRINT ALL ELEMENETS IN KEY AND VALUE PAIR FORMAT. Hint: ===== Map ht = new Hashtable<>();