Like HashMap, Hashtable stores key/value pairs in a hash table. When using a Hashtable, you specify an object that is used as a key, and the value that you want linked to that key. The key is then hashed, and the resulting hash code is used as the index at which the value is stored within the table.
1.Basic java collection framework example program to get Value collection from hashtable
Output:
You Might Like::
1.Basic java collection framework example program to get Value collection from hashtable
- Collection values() This method used to get collection of values
- Important note is that if any value 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");
- Collection c = hashtable.values();
- System.out.println("Values of Collection created from Hashtable are :");
- //iterate through the Set of keys
- Iterator itr = c.iterator();
- while(itr.hasNext())
- System.out.println(itr.next());
- c.remove("Java Quiz");
- System.out.println("Elements in hash table");
- Enumeration e=hashtable.elements();
- while (e.hasMoreElements()) {
- System.out.println(e.nextElement());
- }
- }
- }
Output:
- Values of Collection created from Hashtable are :
- Real time examples
- Java Quiz
- Concept and interview Questions
- Concept and example program
- Java Interview Programs
- Java Interview Questions
- Elements in hash table
- Real time examples
- Concept and interview Questions
- Concept and example program
- Java Interview Programs
- Java Interview Questions
You Might Like::