This example shows how to get all keys of the TreeMap object using the keySet method of the TreeMap class. The example also shows how to iterate all keys of the TreeMap object.
How to get all keys of TreeMap using the keySet method in Java?
The keySet
method of the TreeMap class returns a Set view of the keys contained in the TreeMap object.
1 |
public Set<K> keySet() |
The returned Set object contains all keys present in the TreeMap object.
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 26 27 28 29 30 31 |
import java.util.Set; import java.util.TreeMap; public class TreeMapGetAllKeysExample { public static void main(String[] args) { //an empty TreeMap object TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); //add some mappings tmapColors.put(1, "Red"); tmapColors.put(2, "Green"); tmapColors.put(3, "Blue"); /* * To get all keys of the TreeMap, use * the keySet method. */ Set<Integer> keys = tmapColors.keySet(); /* * Iterate all keys */ System.out.println("Iterating all keys of TreeMap"); for(Integer key : keys){ System.out.println( key ); } } } |
Output
1 2 3 4 |
Iterating all keys of TreeMap 1 2 3 |
Important Note:
The Set returned from the keySet
method is a view that is backed by the original TreeMap object. It means any changes you make to the key set will be reflected in the TreeMap object, and vice versa.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); tmapColors.put(1, "Red"); tmapColors.put(2, "Green"); tmapColors.put(3, "Blue"); //get the key set Set<Integer> keys = tmapColors.keySet(); //remove a key from key set keys.remove(2); //the key will also be removed from the TreeMap object! System.out.println("Key Set contains: " + keys); System.out.println("TreeMap contains: " + tmapColors); //add new mapping to the TreeMap tmapColors.put(4, "Yellow"); //the new keys will be added to the key set too! System.out.println("Key Set contains: " + keys); System.out.println("TreeMap contains: " + tmapColors); |
Output
1 2 3 4 |
Key Set contains: [1, 3] TreeMap contains: {1=Red, 3=Blue} Key Set contains: [1, 3, 4] TreeMap contains: {1=Red, 3=Blue, 4=Yellow} |
As you can see from the output when we change the key set the TreeMap also changed, and vice versa.
However, remember that add
and addAll
operations are not supported by the key set. Have a look at the below given example.
1 2 3 4 5 6 7 8 9 10 |
TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); tmapColors.put(1, "Red"); tmapColors.put(2, "Green"); tmapColors.put(3, "Blue"); //get the key set Set<Integer> keys = tmapColors.keySet(); keys.add(4); |
Output
1 2 |
Exception in thread "main" java.lang.UnsupportedOperationException at java.util.AbstractCollection.add(Unknown Source) |
As you can see from the output if you try to use add
or addAll
methods of the Set, the code will throw java.lang.UnsupportedOperationException exception as these operations are not supported by the key set.
Please also visit how to get all values of TreeMap example.
This example is a part of the Java TreeMap Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 TreeMap