HashTable with Looping
We can iterate hashTable using enhanced for loop with keyset() method or by using enhanced for loop with EntrySet() method or by using iterator
Case 1 :
Enhanced for loop with keyset()
Example
- import java.util.*;
- public class HashTableEnhancedForLoopExample {
- public static void main(String args[]) {
- Hashtable<Integer, String> hashTable = new Hashtable<Integer, String>();
- hashTable.put(1, "One");
- hashTable.put(2, "Two");
- hashTable.put(3, "Three");
- Set<Integer> keys = hashTable.keySet();
- for(Integer key: keys){
- System.out.println("Value of "+key+" is: "+hashTable.get(key));
- }
- }
- }
import java.util.*; public class HashTableEnhancedForLoopExample { public static void main(String args[]) { Hashtable<Integer, String> hashTable = new Hashtable<Integer, String>(); hashTable.put(1, "One"); hashTable.put(2, "Two"); hashTable.put(3, "Three"); Set<Integer> keys = hashTable.keySet(); for(Integer key: keys){ System.out.println("Value of "+key+" is: "+hashTable.get(key)); } } }
In this program, we have accessed all the keys of HashTable using keySet() method and then iterated all the keys using enhanced for loop and accessed value by passing key in each iteration in the loop.
Case 2 :
Enhanced for loop with entrySet()
Example
- import java.util.*;
- public class HashTableEnhancedForLoopEntrySetExample {
- public static void main(String args[]) {
- Hashtable<Integer, String> hashTable = new Hashtable<Integer, String>();
- hashTable.put(1, "One");
- hashTable.put(2, "Two");
- hashTable.put(3, "Three");
- for (Map.Entry<Integer, String> entry : hashTable.entrySet()) {
- Integer key = entry.getKey();
- Object value = entry.getValue();
- System.out.println("Value of "+key+" is: "+value);
- }
- }
- }
import java.util.*; public class HashTableEnhancedForLoopEntrySetExample { public static void main(String args[]) { Hashtable<Integer, String> hashTable = new Hashtable<Integer, String>(); hashTable.put(1, "One"); hashTable.put(2, "Two"); hashTable.put(3, "Three"); for (Map.Entry<Integer, String> entry : hashTable.entrySet()) { Integer key = entry.getKey(); Object value = entry.getValue(); System.out.println("Value of "+key+" is: "+value); } } }
Case 3 :
Using iterator
Example
- import java.util.*;
- public class HashTableIteratorExample {
- public static void main(String args[]) {
- Hashtable<Integer, String> hashTable = new Hashtable<Integer, String>();
- hashTable.put(1, "One");
- hashTable.put(2, "Two");
- hashTable.put(3, "Three");
- Iterator it = hashTable.entrySet().iterator();
- while (it.hasNext()) {
- Map.Entry pair = (Map.Entry)it.next();
- System.out.println(pair.getKey() + " = " + pair.getValue());
- }
- }
- }
import java.util.*; public class HashTableIteratorExample { public static void main(String args[]) { Hashtable<Integer, String> hashTable = new Hashtable<Integer, String>(); hashTable.put(1, "One"); hashTable.put(2, "Two"); hashTable.put(3, "Three"); Iterator it = hashTable.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry)it.next(); System.out.println(pair.getKey() + " = " + pair.getValue()); } } }