1.Basic java collection framework example program to get set view from hashtable
Output:
- Set keySet() This method used to get set view of the keys of hashtable
- Important note is that if any key is removed from set the original hashtable key also removed
- package com.setviewHashtable;
- import java.util.Hashtable;
- import java.util.Enumeration;
- import java.util.Iterator;
- import java.util.Set;
- public class HashtableExample{
- public static void main(String[] args) {
- //create Hashtable object
- Hashtable<String,String> hashtable = new Hashtable<String,String>();
- //add key value pairs to Hashtable
- hashtable.put("1","Java Interview Questions");
- hashtable.put("2","Java Interview Programs");
- hashtable.put("3","Concept and example program");
- hashtable.put("4","Concept and interview Questions");
- hashtable.put("5","Java Quiz");
- hashtable.put("6","Real time examples");
- Set st = hashtable.keySet();
- System.out.println("Set created from Hashtable Keys contains :");
- //iterate through the Set of keys
- Iterator itr = st.iterator();
- while(itr.hasNext())
- System.out.println(itr.next());
- st.remove("1");
- System.out.println("Elements in hash table");
- Enumeration e=hashtable.keys();
- while (e.hasMoreElements()) {
- System.out.println(e.nextElement());
- }
- }
- }
Output:
- Set created from Hashtable Keys contains :
- 6
- 5
- 4
- 3
- 2
- 1
- Elements in hash table
- 6
- 5
- 4
- 3
- 2