This example shows how to check if a key exists in Java TreeMap using the containsKey method. The example also shows how to check if the custom class key exists in the Java TreeMap object.
How to check if a key exists in the TreeMap object in Java?
The containsKey
method of the TreeMap class returns true if the specified key exists in the TreeMap object.
1 |
public boolean containsKey(Object key) |
This method returns true if the specified key is mapped to any value in the map object, false otherwise.
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 |
import java.util.TreeMap; public class TreeMapCheckIfKeyExistsExample { 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"); /* * To check if the key exists in the TreeMap, use * the containsKey method. * * This method returns true if the specified key * is mapped to any value in the map, false otherwise. */ //this will return true, as the key 2 exists in the map System.out.println( tmapColors.containsKey(2) ); //this will return false as the key 4 does not exist in the map System.out.println( tmapColors.containsKey(4) ); } } |
Output
1 2 |
true false |
How to check if the TreeMap contains a custom class key?
If you are using the custom class objects as the keys of the TreeMap, then the custom class must implement the Comparable interface or a custom comparator must be provided while creating the TreeMap object.
Since this is the requirement of putting custom class object keys in the TreeMap in the first place, you do not need to do anything extra for the containsKey
method to work as expected. Let’s have a look at an example of that.
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import java.util.TreeMap; class Emp implements Comparable<Emp>{ private Integer id; private String name; public Emp(Integer id, String name){ this.id = id; this.name = name; } public String toString(){ return "[" + this.id + " => " + this.name + "]"; } public int getId(){ return this.id; } /* * This method will compare this employee object with another */ public int compareTo(Emp o) { return this.getId() - o.getId(); } } public class TreeMapCheckIfKeyExistsExample { public static void main(String[] args) { TreeMap<Emp, Integer> treemap = new TreeMap<Emp, Integer>(); treemap.put(new Emp(3, "Jack"), 3); treemap.put(new Emp(1, "Emily"), 1); treemap.put(new Emp(2, "Maria"), 2); //this will return true System.out.println( treemap.containsKey( new Emp(1, "Emily") ) ); //this will return false System.out.println( treemap.containsKey( new Emp(4, "John") ) ); } } |
Output
1 2 |
true false |
As you can see from the output since our Emp class has already implemented the Comparable interface, the containsKey
method worked as expected.
Please also see how to check If a value exists in the 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