This example shows how to check if a value exists in the TreeMap object in Java using the containsValue method. The example also shows how to check if TreeMap contains a custom class value.
How to check if the value exists in Java TreeMap using the containsvalue method?
The containsValue
method of the TreeMap class returns true if the specified value exists in the map object.
1 |
public boolean containsValue(Object value) |
The return type of the containsValue
method is a boolean. It returns true if the specified value is mapped to one or more keys in the TreeMap 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 TreeMapCheckIfValueExistsExample { 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 value exists in the TreeMap, use * the containsValue method. * * This method returns true if the specified value object * is mapped to one or more keys in the map, false otherwise. */ //this will return true, as the value "Green" exists in the map System.out.println( tmapColors.containsValue("Green") ); //this will return false as the value "Yellow" does not exist in the map System.out.println( tmapColors.containsValue("Yellow") ); } } |
Output
1 2 |
true false |
How to check if TreeMap contains custom class value?
Let’s first see an example of TreeMap having custom class value objects.
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 |
import java.util.TreeMap; class 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; } } public class TreeMapCheckIfValueExistsExample { public static void main(String[] args) { TreeMap<Integer, Emp> treemap = new TreeMap<Integer, Emp>(); treemap.put(1, new Emp(1, "John")); treemap.put(2, new Emp(2, "Rajesh")); treemap.put(3, new Emp(3, "Bob")); System.out.println( treemap.containsValue( new Emp(2, "Rajesh") ) ); } } |
Output
1 |
false |
As you can see from the output, even if the specified Emp value was present in the TreeMap object, the containsValue
method returned false. It is because our custom class has not defined the equals
method (and preferably the hashCode
method as well).
Let’s override these two methods in our Emp class and try again.
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 46 47 48 49 50 51 52 53 54 55 56 57 58 |
import java.util.TreeMap; class 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; } public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Emp other = (Emp) obj; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; return true; } } public class TreeMapCheckIfValueExistsExample { public static void main(String[] args) { TreeMap<Integer, Emp> treemap = new TreeMap<Integer, Emp>(); treemap.put(1, new Emp(1, "John")); treemap.put(2, new Emp(2, "Rajesh")); treemap.put(3, new Emp(3, "Bob")); System.out.println( treemap.containsValue( new Emp(2, "Rajesh") ) ); } } |
Output
1 |
true |
As you can see from the output, this time it worked as expected and printed true.
Please also see how to check If a key 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