This example shows how to iterate TreeMap in Java using the forEach. This example also shows how to iterate TreeMap keys, values, and entries using the forEach.
How to iterate TreeMap using the forEach?
There are several ways using which you can iterate TreeMap keys, values, and entries using the forEach. Please remember that you will need Java version 8 or later in order to use the forEach construct.
1. Iterate TreeMap keys using forEach
The keySet
method returns a Set view of all the keys contained in the TreeMap object. Once we get that, we can iterate through the keys using the forEach as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Set; import java.util.TreeMap; public class TreeMapForEachExample { public static void main(String[] args) { TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); tmapColors.put(1, "Red"); tmapColors.put(2, "Green"); tmapColors.put(3, "Blue"); /* * Get all keys using the keySet method */ Set<Integer> keys = tmapColors.keySet(); //iterate using forEach keys.forEach( key -> { System.out.println(key); }); } } |
Output
1 2 3 |
1 2 3 |
2. Iterate TreeMap values using forEach
The values
method returns a Collection view of all the values contained in the TreeMap object. Once we get the Collection, we can iterate through the values using the forEach as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Collection; import java.util.TreeMap; public class TreeMapForEachExample { public static void main(String[] args) { TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); tmapColors.put(1, "Red"); tmapColors.put(2, "Green"); tmapColors.put(3, "Blue"); /* * Get all values using the values method */ Collection<String> values = tmapColors.values(); //iterate values using forEach values.forEach( value -> { System.out.println( value ); }); } } |
Output
1 2 3 |
Red Green Blue |
3. Iterate TreeMap entries using forEach
The entrySet
method of the TreeMap object returns a Set view of all the entries (i.e. key-value mappings) stored in the map object. Once we get the Set, we can iterate through TreeMap entries using the forEach as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.Map; import java.util.Set; import java.util.TreeMap; public class TreeMapForEachExample { public static void main(String[] args) { TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); tmapColors.put(1, "Red"); tmapColors.put(2, "Green"); tmapColors.put(3, "Blue"); /* * Get all entries using the entrySet method */ Set<Map.Entry<Integer, String>> entries = tmapColors.entrySet(); //iterate entries using the forEach entries.forEach( entry -> { System.out.println(entry.getKey() + "->" + entry.getValue()); }); } } |
Output
1 2 3 |
1->Red 2->Green 3->Blue |
Please also visit how to iterate TreeMap example to know more ways to iterate the TreeMap object.
This example is a part of the TreeMap in Java Tutorial.
Please let me know your views in the comments section below.
References:
Java 8 TreeMap