Core Java

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

  1. import java.util.*;
  2. public class HashTableEnhancedForLoopExample {
  3. public static void main(String args[]) {
  4.  
  5. Hashtable<Integer, String> hashTable = new Hashtable<Integer, String>();
  6.       hashTable.put(1, "One");
  7.       hashTable.put(2, "Two");
  8.       hashTable.put(3, "Three");
  9. Set<Integer> keys = hashTable.keySet();
  10.         for(Integer key: keys){
  11.             System.out.println("Value of "+key+" is: "+hashTable.get(key));
  12.         }
  13. }
  14. }
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

  1. import java.util.*;
  2. public class HashTableEnhancedForLoopEntrySetExample {
  3. public static void main(String args[]) {
  4.  
  5. Hashtable<Integer, String> hashTable = new Hashtable<Integer, String>();
  6.       hashTable.put(1, "One");
  7.       hashTable.put(2, "Two");
  8.       hashTable.put(3, "Three");
  9. for (Map.Entry<Integer, String> entry : hashTable.entrySet()) {
  10.     Integer key = entry.getKey();
  11.     Object value = entry.getValue();
  12. System.out.println("Value of "+key+" is: "+value);
  13. }
  14.        
  15. }
  16. }
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

  1. import java.util.*;
  2. public class HashTableIteratorExample {
  3. public static void main(String args[]) {
  4.  
  5. Hashtable<Integer, String> hashTable = new Hashtable<Integer, String>();
  6.       hashTable.put(1, "One");
  7.       hashTable.put(2, "Two");
  8.       hashTable.put(3, "Three");
  9. Iterator it = hashTable.entrySet().iterator();
  10.     while (it.hasNext()) {
  11.         Map.Entry pair = (Map.Entry)it.next();
  12.         System.out.println(pair.getKey() + " = " + pair.getValue());
  13.        
  14.     }
  15.        
  16. }
  17. }
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());
        
    }
        
}
}
Share this article on

Comment (9)

    1. Karibasappa G C (KB)
      Karibasappa G C (KB)

      First thing, static variables will never be a part of method, it will be a part of class always.

      So only we cant declare any static variables inside a method but we can access them inside a method.

      All static variables will be stored in separate segment inside Heap area only.

      1. The blog is very helpful. Thanks a lot for sharing! Also the “Inheritance in interface” page is not opening. Can you please fix that?

Leave a Comment

  • You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" extra="">

    • Please answer this simple challenge to post your valuable comment *