Java — Comparing Two HashMaps | Code Factory

Using Map.equals()

package com.example.java.programming;import java.util.HashMap;
import java.util.Map;
/**
* @author code.factory
*
*/
public class Test {
public static void main(String... strings) {
Map<String, String> map1 = new HashMap<>();
map1.put("Gujarat", "Ahmedabad");
map1.put("Maharastra", "Mumbai");
Map<String, String> map2 = new HashMap<>();
map2.put("Maharastra", "Mumbai");
map2.put("Gujarat", "Ahmedabad");
Map<String, String> map3 = new HashMap<>();
map3.put("Rajasthan", "Jaipur");
map3.put("Gujarat", "Ahmedabad");
System.out.println(map1.equals(map2));
System.out.println(map1.equals(map3));
}
}
true
false
  • * 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.
  • For example, Map.equals() doesn’t work when the value type is array, as an array’s equals() method compares identity and not the contents of the array.
package com.example.java.programming;import java.util.HashMap;
import java.util.Map;
/**
* @author code.factory
*
*/
public class Test {
public static void main(String... strings) {
Map<String, String[]> map1 = new HashMap<>();
map1.put("Gujarat", new String[] {"Ahmedabad", "Gandhinagar"});
map1.put("Maharastra", new String[] {"Mumbai", "Pune"});
Map<String, String[]> map2 = new HashMap<>();
map2.put("Maharastra", new String[] {"Mumbai", "Pune"});
map2.put("Gujarat", new String[] {"Ahmedabad", "Gandhinagar"});
System.out.println(map1.equals(map2));
}
}
false

Using the Java Stream API

package com.example.java.programming;import java.util.HashMap;
import java.util.Map;
/**
* @author code.factory
*
*/
public class Test {
public static void main(String... strings) {
Map<String, String> map1 = new HashMap<>();
map1.put("Gujarat", "Ahmedabad");
map1.put("Maharastra", "Mumbai");
Map<String, String> map2 = new HashMap<>();
map2.put("Maharastra", "Mumbai");
map2.put("Gujarat", "Ahmedabad");
Map<String, String> map3 = new HashMap<>();
map3.put("Rajasthan", "Jaipur");
map3.put("Gujarat", "Ahmedabad");
System.out.println(map1.entrySet().stream().allMatch(m -> m.getValue().equals(map2.get(m.getKey()))));
System.out.println(map1.entrySet().stream().allMatch(m -> m.getValue().equals(map3.get(m.getKey()))));
}
}
true
false
  • Now compare arrays value.
package com.example.java.programming;import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* @author code.factory
*
*/
public class Test {
public static void main(String... strings) {
Map<String, String[]> map1 = new HashMap<>();
map1.put("Gujarat", new String[] {"Ahmedabad", "Gandhinagar"});
map1.put("Maharastra", new String[] {"Mumbai", "Pune"});
Map<String, String[]> map2 = new HashMap<>();
map2.put("Maharastra", new String[] {"Mumbai", "Pune"});
map2.put("Gujarat", new String[] {"Ahmedabad", "Gandhinagar"});
System.out.println(map1.entrySet().stream().allMatch(m -> Arrays.equals(m.getValue(), map2.get(m.getKey()))));
}
}
true

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The world 1st ONE character TLD

Postgres replica conflicts: Part 1

Validating Urls In Ruby

The Parable of the Simpleminded Gamer (ce qu’on comprend et ce qu’on ne comprend pas)

Part 8 : Automate using Rules

Use Clojure macro to generate a random ID unique to each build

FOSS Motivations: Joining NumPy

Document Genres in Technical Communication

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Make your Java comments more informative, actionable

HashMap in Java

Is Java dead yet? Key figures about Java usage in 2022