Java Compare Two TreeMap objects Example

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.

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.

Output

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.

Output

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.

Output

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.

Output

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

About the author

RahimV

RahimV

My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.

Add Comment

Your email address will not be published. Required fields are marked *

Online Shopping