Different ways to iterate LinkedHashMap in Java

1. Using Iterator – Iterating LinkedHashMap using an iterator.

package linkedhashmapiter;
 
import java.util.*;
 
 
public class Example1 {
	public static void main(String[] args) {
		
		Map<Integer,String> linkedHashMapObj = new LinkedHashMap<>();
		linkedHashMapObj.put(2, "ram");
		linkedHashMapObj.put(8, "mohan");
		linkedHashMapObj.put(3, "sohan");
		linkedHashMapObj.put(4, "rahul");
		linkedHashMapObj.put(9, "rohan");
		linkedHashMapObj.put(0, "suresh");
		linkedHashMapObj.put(1, "ganesh");
 
		// we will iterate LinkedHashMap using Iterator
		Iterator<Map.Entry<Integer, String>> iterator = linkedHashMapObj
				.entrySet().iterator();
	 
		while (iterator.hasNext()) {
			Map.Entry<Integer, String> entry = iterator.next();
			System.out.println("Key is = " + entry.getKey() + ", Value is = "
					+ entry.getValue());
		}
	}
}

Output is –

Key is = 2, Value is = ram
Key is = 8, Value is = mohan
Key is = 3, Value is = sohan
Key is = 4, Value is = rahul
Key is = 9, Value is = rohan
Key is = 0, Value is = suresh
Key is = 1, Value is = ganesh

2. Using for each loop -Iterating Hashtable using for each loop.

package linkedhashmapiter;

import java.util.*;

public class Example2 {
public static void main(String[] args) {
	Map<Integer,String> linkedHashMapObj = new LinkedHashMap<>();
	linkedHashMapObj.put(2, "ram");
	linkedHashMapObj.put(8, "mohan");
	linkedHashMapObj.put(3, "sohan");
	linkedHashMapObj.put(4, "rahul");
	linkedHashMapObj.put(9, "rohan");
	linkedHashMapObj.put(0, "suresh");
	linkedHashMapObj.put(1, "ganesh");

	Set<Map.Entry<Integer, String>> entrySet = linkedHashMapObj.entrySet();
	 //using for each loop
	for (Map.Entry<Integer, String> entry : entrySet) {
		System.out.println("Key is : " + entry.getKey() + "   Value is : "
				+ entry.getValue());
	}
}
}

Output is –

Key is : 2 Value is : ram
Key is : 8 Value is : mohan
Key is : 3 Value is : sohan
Key is : 4 Value is : rahul
Key is : 9 Value is : rohan
Key is : 0 Value is : suresh
Key is : 1 Value is : ganesh

3. Using Lambda expression – Iterating LinkedHashMap using a lambda expression.

package linkedhashmapiterate;

import java.util.*;


public class Example3 {
	public static void main(String[] args) {
		Map<Integer, String> linkedHashMapObj = new LinkedHashMap<>();
 
		linkedHashMapObj.put(2, "ram");
		linkedHashMapObj.put(5, "mohan");
		linkedHashMapObj.put(3, "sohan");
		linkedHashMapObj.put(9, "rohit");
		
		// iterating LinkedHashMap using lambda expression
		linkedHashMapObj.forEach((k, v) -> {
			System.out.println("Key is : " + k + "   Value is : " + v);
		});
 
	}
}

Output –


Key is : 2 Value is : ram
Key is : 5 Value is : mohan
Key is : 3 Value is : sohan
Key is : 9 Value is : rohit

4. Using Java 8 stream() method –


package linkedhashmapiterate;

import java.util.*;


public class Example4 {
	public static void main(String[] args) {
		Map<Integer, String> linkedHashMapObj = new LinkedHashMap<>();
		 
		linkedHashMapObj.put(2, "ram");
		linkedHashMapObj.put(5, "mohan");
		linkedHashMapObj.put(3, "sohan");
		linkedHashMapObj.put(9, "rohit");
 
		Set<Map.Entry<Integer, String>> setReference= linkedHashMapObj.entrySet();
 
		setReference.stream().forEach
		(mapEntry-> System.out.println("Key is : "+mapEntry.getKey()+"   Value is : "+mapEntry.getValue()));
	}
}

Output –

Key is : 2 Value is : ram
Key is : 5 Value is : mohan
Key is : 3 Value is : sohan
Key is : 9 Value is : rohit

5. Using the keySet() and Iterator – 

package linkedhashmapiterate;

import java.util.*;


public class Example5 {
	public static void main(String[] args) {
		Map<Integer, String> linkedHashMap = new LinkedHashMap<>();
 
		linkedHashMap.put(2, "ram");
		linkedHashMap.put(5, "mohan");
		linkedHashMap.put(3, "sohan");
		linkedHashMap.put(9, "rohit");
 
		Iterator iterator = linkedHashMap.keySet().iterator();
 
		while (iterator.hasNext()) {
			Integer key = (Integer) iterator.next();
 
			System.out.println("Key is : " + key + "   Value is : " + linkedHashMap.get(key));
		}
	}
}

Output –

Key is : 2 Value is : ram
Key is : 5 Value is : mohan
Key is : 3 Value is : sohan
Key is : 9 Value is : rohit

6. Using keySet() and for each loop – 

package linkedhashmapiterate;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
 
public class Example6 {
	public static void main(String[] args) {
		Map<Integer, String> linkedHashMap = new LinkedHashMap<>();
		 
		linkedHashMap.put(2, "ram");
		linkedHashMap.put(5, "mohan");
		linkedHashMap.put(3, "sohan");
		linkedHashMap.put(9, "rohit");
 
		Iterator iterator = linkedHashMap.keySet().iterator();
 
		for (Integer key : linkedHashMap.keySet()) {
			System.out.println("Key is : " + key + "   Value is : " + linkedHashMap.get(key));
		}
	}
}

Output –

Key is : 2 Value is : ram
Key is : 5 Value is : mohan
Key is : 3 Value is : sohan
Key is : 9 Value is : rohit