How to Compare Two HashMaps in Java [Snippet]

In this quick article, we’re going to explore different ways to compare two HashMaps in Java.
We’ll discuss multiple ways to check if two HashMaps are similar. We’ll also use Java 8 Stream API.

Using Map.equals() Method

First, we’ll use Map.equals() to check if two HashMaps have the same entries. Let's write a Java program to demonstrates comparing two HashMap using Map.equals() method.
package net.javaguides.examples;

import java.util.HashMap;
import java.util.Map;

/**
 * This class demonstrates different ways to compare HashMaps in Java.
 * @author Ramesh Fadatare
 *
 */
public class CompareHashMapExample {
    public static void main(String[] args) {
        Map < String, String > sourceMap = new HashMap < String, String > ();
        sourceMap.put("Key1", "Value1");
        sourceMap.put("Key2", "Value2");

        Map < String, String > dstMap = new HashMap < String, String > ();
        dstMap.put("Key1", "Value1");
        dstMap.put("Key2", "Value2");

        CompareHashMapExample hashMapExample = new CompareHashMapExample();

        // call equal method
        System.out.println("Are two HashMap equals :: " + hashMapExample.equal(sourceMap, dstMap));
    }

    /**
     * using Map.equals() to check if two HashMaps have the same entries
     * @param source
     * @param destination
     * @return
     */
    public boolean equal(Map < String, String > source, Map < String, String > destination) {
        return source.equals(destination);
    }
}
Output:
Are two HashMap equals :: true
Here, we’re creating two HashMap objects and adding entries. Then we’re using Map.equals() to check if two HashMaps have the same entries.
The way that Map.equals() works is by comparing keys and values using the Object.equals() method. This means it only works when both key and value objects implement equals() properly.

Using the Java Stream API

Let's write a Java program to compare HashMaps using the Java 8 Stream API:
package net.javaguides.examples;

import java.util.HashMap;
import java.util.Map;

/**
 * This class demonstrates different ways to compare HashMaps in Java.
 * @author Ramesh Fadatare
 *
 */
public class CompareHashMapExample {
    public static void main(String[] args) {
        Map < String, String > sourceMap = new HashMap < String, String > ();
        sourceMap.put("Key1", "Value1");
        sourceMap.put("Key2", "Value2");

        Map < String, String > dstMap = new HashMap < String, String > ();
        dstMap.put("Key1", "Value1");
        dstMap.put("Key2", "Value2");

        CompareHashMapExample hashMapExample = new CompareHashMapExample();

        // call areEqual method
        System.out.println("Are two HashMap equals :: " + hashMapExample.areEqual(sourceMap, dstMap));
    }

    /**
     * Compare HashMaps using the Java 8 Stream API
     * @param first
     * @param second
     * @return
     */
    public boolean areEqual(Map < String, String > first, Map < String, String > second) {
        if (first.size() != second.size()) {
            return false;
        }

        return first.entrySet()
            .stream()
            .allMatch(e - > e.getValue().equals(second.get(e.getKey())));
    }
}
Output:
Are two HashMap equals :: true

Collections Framework Examples

Comments