AddElementsToHashtable.java 888 B

1234567891011121314151617181920212223242526272829303132
  1. // Using Hashtable() Constructor
  2. import java.io.*;
  3. import java.util.*;
  4. class AddElementsToHashtable
  5. {
  6. public static void main(String args[])
  7. {
  8. // No need to mention the
  9. // Generic type twice
  10. Hashtable<String, String> ht1 = new Hashtable<>();
  11. // Initialization of a Hashtable
  12. // using Generics
  13. Hashtable<Integer, String> ht2
  14. = new Hashtable<Integer, String>();
  15. // Inserting the Elements
  16. // using put() method
  17. ht1.put("192.168.1.20", "one");
  18. ht1.put("192.16.1.1", "two");
  19. ht1.put("10.1.2.1", "three");
  20. ht2.put(4, "four");
  21. ht2.put(5, "five");
  22. ht2.put(6, "six");
  23. // Print mappings to the console
  24. System.out.println("Mappings of ht1 : " + ht1);
  25. System.out.println("Mappings of ht2 : " + ht2);
  26. }
  27. }