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.
Learn and master Java Collections Framework at Learn Java Collections Framework.
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
- Conversion Between Array and Set in Java
- Conversion Between Array and List in Java
- Java Convert Map to Set Example
- Java Convert Map to List Example
- Java Convert Map to Array Example
- Convert a Map to an Array, List and Set in Java
- Java 8 Convert List to Map Example
- Java 8 - Merging Two Maps Example
- Java Convert Array to String [Snippet]
- Different Ways to Iterate over List, Set and Map in Java
- Java Comparator Interface Example
- Java Comparable Interface Example
- Java IdentityHashMap Example
- Java WeakHashMap Example
- Java EnumMap Example
- Java CopyOnWriteArraySet Example
- Java EnumSet Class Example
- Guide to Java 8 forEach Method
- Different Ways to Iterate over a List in Java [Snippet]
- Different Ways to Iterate over a Set in Java [Snippet]
- Different Ways to Iterate over a Map in Java [Snippet]
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course