LinkedHashMap with Looping

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

LinkedHashMap<Integer,String>   lhm = new LinkedHashMap<>();
lhm.put(1, “One”);
lhm.put(2, “Two”);
lhm.put(3, “Three”);
Set keys = lhm.keySet();
for(Integer key: keys){
System.out.println(“Value of “+key+” is: “+lhm.get(key));
}
}
}


In this program, we have accessed all the keys of LinkedHashMap 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 LinkedHashMapEnhancedForLoopEntrySetExample {
  3. public static void main(String args[]) {
  4.  
  5. LinkedHashMap<Integer,String>   lhm = new LinkedHashMap<>();
  6. lhm.put(1, “One”);
  7. lhm.put(2, “Two”);
  8. lhm.put(3, “Three”);
  9. for (Map.Entry entry : lhm.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 LinkedHashMapEnhancedForLoopEntrySetExample {
public static void main(String args[]) {

LinkedHashMap<Integer,String>   lhm = new LinkedHashMap<>();
lhm.put(1, “One”);
lhm.put(2, “Two”);
lhm.put(3, “Three”);
for (Map.Entry entry : lhm.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 LinkedHashMapIteratorExample {
  3. public static void main(String args[]) {
  4. LinkedHashMap<Integer,String> lhm = new LinkedHashMap<>();
  5. lhm.put(1, “One”);
  6. lhm.put(2, “Two”);
  7. lhm.put(3, “Three”);
  8. Iterator it = lhm.entrySet().iterator();
  9. while (it.hasNext()) {
  10. Map.Entry pair = (Map.Entry)it.next();
  11. System.out.println(pair.getKey() +=+ pair.getValue());
  12.  
  13. }
  14.  
  15. }
  16. }
import java.util.*;
public class LinkedHashMapIteratorExample {
public static void main(String args[]) {
LinkedHashMap<Integer,String> lhm = new LinkedHashMap<>();
lhm.put(1, “One”);
lhm.put(2, “Two”);
lhm.put(3, “Three”);
Iterator it = lhm.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + ” = ” + pair.getValue());

}

}
}

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on