How to Iterate Through LinkedHashMap Values in Java


In this example, we will learn how to iterate through the values contained in the LinkedHashMap object.

Source Code

package com.beginner.examples;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;

public class IterateValuesOfLinkedHashMap {

	public static void main(String[] args) {
		
		//create LinkedHashMap.
		LinkedHashMap linkedHashMap = new LinkedHashMap();
		
		linkedHashMap.put("1","Alice");
		linkedHashMap.put("2","Bob");
		linkedHashMap.put("3","Jack");
		
		/*
		 * get Collection of values contained in LinkedHashMap using Collection values() method of LinkedHashMap.
		 */
		Collection collection = linkedHashMap.values();
		Iterator iterator = collection.iterator();
		
		while(iterator.hasNext()) {
			System.out.println(iterator.next());
		}

	}

}

Output:

Alice
Bob
Jack

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments