This example shows how to get TreeMap value by key in Java. This example also shows various ways using which you can get TreeMap key using a value.
How to get key using value from TreeMap in Java?
The TreeMap class in Java is a map implementation that maps keys to values. It is very easy to get TreeMap value from the key using the get
method. However, it is not so easy to do the opposite, i.e. get key by value as there is no direct method to do that. However, you can use the below given approaches if you still need to do that.
1. Using the entrySet method
Get all the entries stored in the TreeMap object using the entrySet
method. Once you get all the entries, iterate through them and compare them with the desired value. If the value is found, fetch the key for that value 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 26 27 28 29 |
import java.util.Map; import java.util.Set; import java.util.TreeMap; public class TreeMapGetKeyByValueExample { 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 the TreeMap entries Set<Map.Entry<Integer, String>> entries = tmapColors.entrySet(); //value for which we need to fetch key String strValue = "Green"; for( Map.Entry<Integer, String> entry : entries ){ if(entry.getValue().equals(strValue)){ System.out.println("Key for value " + strValue + " is: " + entry.getKey()); break; } } } } |
Output
1 |
Key for value Green is: 2 |
2. Using the keySet method
Instead of getting all the entries, you can also get all the keys stored in the TreeMap using the keySet
method as given below. You can also use an Iterator instead of the for loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); tmapColors.put(1, "Red"); tmapColors.put(2, "Green"); tmapColors.put(3, "Blue"); //get all the TreeMap keys Set<Integer> keys = tmapColors.keySet(); //value for which we need to fetch key String strValue = "Blue"; for( Integer key : keys ){ if( tmapColors.get(key).equals(strValue)){ System.out.println("Key for value " + strValue + " is: " + key); break; } } |
Output
1 |
Key for value Blue is: 3 |
How about TreeMap having duplicate values mapped to different keys?
Both of the above given approaches work only if the TreeMap is having a 1:1 relationship. They do not work if the same value is mapped to different keys in the TreeMap. If the TreeMap has duplicate values, you need to change the code a little bit 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 |
TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); tmapColors.put(1, "Red"); tmapColors.put(2, "Green"); tmapColors.put(3, "Blue"); tmapColors.put(4, "Green"); tmapColors.put(5, "Yellow"); //get all the TreeMap entries Set<Map.Entry<Integer, String>> entries = tmapColors.entrySet(); //value for which we need to fetch key(s) String strValue = "Green"; //this list will hold all keys having the given value List<Integer> listKeys = new ArrayList<Integer>(); for( Map.Entry<Integer, String> entry : entries ){ if(entry.getValue().equals(strValue)){ listKeys.add(entry.getKey()); } } |
Output
1 |
All keys having value Green are: [2, 4] |
What is the preferred way to fetch a key using the value from the TreeMap object?
The above given approaches iterate the TreeMap in order to find the key for a matching value. This operation may not perform well if the map is too large and could slow down the execution of the code.
If you really need to have this functionality, you should consider using the data structure specifically built for this purpose called bi-maps. As the name suggests, they are bi-directional maps and supports lookups from key to value as well as value to key.
The Java standard library does not have any such map, but the Apache Commons library provides one named BidiMap. Here is how to use that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import org.apache.commons.collections4.BidiMap; import org.apache.commons.collections4.bidimap.DualHashBidiMap; public class TreeMapGetKeyByValueExample { public static void main(String[] args) { BidiMap<Integer, String> bidiMapColors = new DualHashBidiMap<Integer, String>(); bidiMapColors.put(1, "Red"); bidiMapColors.put(2, "Green"); bidiMapColors.put(3, "Blue"); //this will return value by key System.out.println("Value for key 1: " + bidiMapColors.get(1)); //this will return key by value System.out.println("Key for value Red: " + bidiMapColors.getKey("Red")); } } |
Output
1 2 |
Value for key 1: Red Key for value Red: 1 |
You can also use the BiMap class from the Google Guava library which supports generics too.
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
Hi!
In test Java Object Oriented Programming Quiz 7 there is a mistake:
Will this code compile?
class Test{
interface TestInterface{
}
}
1. Yes
2. No
Incorrect answer.
Yes is the correct choice. Defining an interface inside a class is valid in Java.
No is shown as correct answer
Hello Paul,
Thanks a lot again for catching the error. I’ve fixed it.
Keep up the good work!
Thank you.