This example shows how to iterate LinkedHashMap in Java. The example also shows various ways to iterate over LinkedHashMap keys, values and entries using an iterator and for loop.
How to iterate LinkedHashMap in Java?
There are various ways using which you can iterate over keys, values, and entries of the LinkedHashMap object in Java.
How to iterate over keys?
The entrySet
method of the LinkedHashMap class returns a Set view of all the keys contained in the map. You can then iterate through keys as given below.
Using for loop
You can use the for loop to iterate through all the keys as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); lhmap.put(1, "One"); lhmap.put(2, "Two"); lhmap.put(3, "Three"); lhmap.put(4, "Four"); lhmap.put(5, "Five"); Set<Integer> keys = lhmap.keySet(); //using for loop for(Integer key : keys){ System.out.println(key); } |
Using an Iterator
Alternatively, you can obtain an iterator object for the key set and iterate through the keys using the hasNext
and the next
methods of the Iterator.
1 2 3 4 5 6 |
//using an iterator Iterator<Integer> iterator = keys.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } |
Output
1 2 3 4 5 |
1 2 3 4 5 |
How to iterate over values?
The values
method of the LinkedHashMap class returns a Collection view of all the values contained in the LinkedHashMap object. You can then iterate through them as given below.
Using for loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); lhmap.put(1, "One"); lhmap.put(2, "Two"); lhmap.put(3, "Three"); lhmap.put(4, "Four"); lhmap.put(5, "Five"); Collection<String> values = lhmap.values(); //using for loop for(String value : values){ System.out.println(value); } |
Using an Iterator
You can also iterate through values using the hasNext
and the next
methods of the iterator as given below.
1 2 3 4 5 6 7 |
Collection<String> values = lhmap.values(); Iterator<String> iterator = values.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } |
Output
1 2 3 4 5 |
One Two Three Four Five |
How to iterate over entries (key-value mappings)?
The entrySet
method of the LinkedHashMap class returns a Set view of all the entries (key-value mappings) contained in the LinkedHashMap object. You can then iterate through the mappings of the LinkedHashMap as given below.
Using for loop
1 2 3 4 5 6 7 8 9 10 11 |
LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); lhmap.put(1, "One"); lhmap.put(2, "Two"); lhmap.put(3, "Three"); lhmap.put(4, "Four"); lhmap.put(5, "Five"); for( Map.Entry<Integer, String> entry : lhmap.entrySet() ){ System.out.println( entry.getKey() + "=>" + entry.getValue() ); } |
Using an Iterator
You can also obtain an iterator for the entry set using the iterator
method and then iterate using the hasNext
and the next
methods as given below.
1 2 3 4 5 6 7 8 9 10 |
Set<Map.Entry<Integer, String>> entries = lhmap.entrySet(); Iterator<Map.Entry<Integer, String>> iterator = entries.iterator(); Map.Entry<Integer, String> entry = null; while(iterator.hasNext()){ entry = iterator.next(); System.out.println( entry.getKey() + "=>" + entry.getValue() ); } |
Output
1 2 3 4 5 |
1=>One 2=>Two 3=>Three 4=>Four 5=>Five |
This example is a part of the LinkedHashMap in Java Tutorial.
Please let me know your views in the comments section below.
References:
Java 8 LinkedHashMap