This example shows how to compare two TreeMap objects in Java. The example also shows how to compare two TreeMap object keys or values using the equals method.
How to compare two TreeMap objects in Java?
The TreeMap equals
method compares two TreeMap objects and returns true if both of the maps have the same mappings.
1 |
boolean equals(Object o) |
The equals
method is declared in the Map interface that is implemented by the TreeMap class. It returns true if and only if both the TreeMap objects have the same key-value pairs.
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 |
import java.util.ArrayList; import java.util.TreeMap; public class CompareTwoTreeMapsExample { public static void main(String[] args) { //first TreeMap TreeMap<Integer, String> tmapColors1 = new TreeMap<Integer, String>(); tmapColors1.put(1, "Red"); tmapColors1.put(2, "Green"); tmapColors1.put(3, "Blue"); //second TreeMap TreeMap<Integer, String> tmapColors2 = new TreeMap<Integer, String>(); tmapColors2.put(1, "Red"); tmapColors2.put(2, "Green"); tmapColors2.put(3, "Blue"); /* * Compare two TreeMap objects using the equals method */ //this will return true as both TreeMap objects contain same mappings System.out.println( tmapColors1.equals( tmapColors2 ) ); //add new mapping to first TreeMap tmapColors1.put(4, "Yellow"); //this will return false as both TreeMap objects do not contain same mappings System.out.println( tmapColors1.equals( tmapColors2 ) ); } } |
Output
1 2 |
true false |
How to compare the keys of two TreeMap objects?
The above example compares all the entries of the map objects for equality. If you want to compare only the keys of the map objects, you can get all keys of TreeMap objects using the keySet
method and then compare them using the equals
method of the Set.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
//first TreeMap TreeMap<Integer, String> tmapColors1 = new TreeMap<Integer, String>(); tmapColors1.put(1, "Red"); tmapColors1.put(2, "Green"); tmapColors1.put(3, "Blue"); //second TreeMap TreeMap<Integer, String> tmapColors2 = new TreeMap<Integer, String>(); tmapColors2.put(1, "Red"); tmapColors2.put(2, "Green"); tmapColors2.put(3, "Blue"); /* * To compare keys of two TreeMap objects, use * the equals method of the Set returned by the * keySet method. */ System.out.println( tmapColors1.keySet().equals( tmapColors2.keySet() ) ); |
Output
1 |
true |
How to compare the values of two TreeMap objects?
We can get all values stored in the TreeMap objects using the values
method. The values
method returns a Collection view of all the values contained in the TreeMap object.
We cannot compare the values Collection objects directly with each other as we did for the key set above using the equals
method. The reason is, the Collection interface defaults to reference comparison instead of the value comparison. Here is the excerpt from the Collection interface equals
method documentation.
While the Collection interface adds no stipulations to the general contract for the Object.equals, programmers who implement the Collection interface “directly” (in other words, create a class that is a Collection but is not a Set or a List) must exercise care if they choose to override the Object.equals. It is not necessary to do so, and the simplest course of action is to rely on Object‘s implementation, but the implementor may wish to implement a “value comparison” in place of the default “reference comparison.”
Let’s quickly check that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//first TreeMap TreeMap<Integer, String> tmapColors1 = new TreeMap<Integer, String>(); tmapColors1.put(1, "Red"); tmapColors1.put(2, "Green"); tmapColors1.put(3, "Blue"); //second TreeMap TreeMap<Integer, String> tmapColors2 = new TreeMap<Integer, String>(); tmapColors2.put(1, "Red"); tmapColors2.put(2, "Green"); tmapColors2.put(3, "Blue"); System.out.println( tmapColors1.values().equals( tmapColors2.values()) ); |
Output
1 |
false |
As we can see from the output, even though the values were exactly the same, the equals
method returned false because it compared the object references, not the values.
In order to compare two Collection objects by values instead of the references, we need to convert the value collection to a List or Set. Let’s convert the Collection to a List 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 |
//first TreeMap TreeMap<Integer, String> tmapColors1 = new TreeMap<Integer, String>(); tmapColors1.put(1, "Red"); tmapColors1.put(2, "Green"); tmapColors1.put(3, "Blue"); //second TreeMap TreeMap<Integer, String> tmapColors2 = new TreeMap<Integer, String>(); tmapColors2.put(1, "Red"); tmapColors2.put(2, "Green"); tmapColors2.put(3, "Blue"); /* * To compare values of two TreeMap, get all values * using the values method, convert it to a List or Set * and then use the equals method. */ List<String> values1 = new ArrayList<String>(tmapColors1.values()); List<String> values2 = new ArrayList<String>(tmapColors2.values()); System.out.println( values1.equals(values2) ); |
Output
1 |
true |
As we can see from the output, it worked this time. We can also convert the collection view to a Set (HashSet) instead of the List (ArrayList or LinkedList) object.
Comparing TreeMap objects of custom class objects?
If the TreeMap keys or values are objects of a custom class, then the custom class must implement the equals
method (and preferably the hashCode
method too) for the TreeMap comparison to work.
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