Learnitweb

Compare two HashMap in Java

equals() method in the HashMap compares two maps by key-value pairs. This method returns true if two maps represent the same mappings. Since HashMap is not an ordered collection, the order of keys in both maps may be different.

Compare HashMaps by keys and values

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

public class CompareHashMapExample {

	public static void main(String[] args) {
		Map<Integer, String> firstMap = new HashMap<>();

		firstMap.put(1, "one");
		firstMap.put(2, "two");
		firstMap.put(3, "three");

		Map<Integer, String> secondMap = new HashMap<>();

		secondMap.put(3, "three");
		secondMap.put(1, "one");
		secondMap.put(2, "two");

		Map<Integer, String> thirdMap = new HashMap<>();

		thirdMap.put(1, "one");
		thirdMap.put(2, "two");
		thirdMap.put(3, "three");
		thirdMap.put(4, "four");

		System.out.println("firstMap.equals(secondMap): " + firstMap.equals(secondMap));
		System.out.println("firstMap.equals(thirdMap): " + firstMap.equals(thirdMap));
	}
}

Output

firstMap.equals(secondMap): true
firstMap.equals(thirdMap): false

Compare two hashmaps for same keys

If we want to compare two maps for same keys, then we can use keySet() method. This method returns a Set view of the keys contained in this map.

Now, we can compare these two Set views to check if two maps have same keys. equals() method of Set returns true if the two sets have the same size and two sets have same members.

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

public class CompareHashMapExample {

	public static void main(String[] args) {
		Map<Integer, String> firstMap = new HashMap<>();

		firstMap.put(1, "one");
		firstMap.put(2, "two");
		firstMap.put(3, "three");

		Map<Integer, String> secondMap = new HashMap<>();

		secondMap.put(3, "three");
		secondMap.put(1, "one");
		secondMap.put(2, "two");

		Map<Integer, String> thirdMap = new HashMap<>();

		thirdMap.put(1, "one");
		thirdMap.put(2, "two");
		thirdMap.put(3, "three");
		thirdMap.put(4, "four");

		Set firstMapKeys = firstMap.keySet();
		Set secondMapKeys = secondMap.keySet();
		Set thirdMapKeys = thirdMap.keySet();

		System.out.println("firstMapKeys.equals(secondMapKeys): " + firstMapKeys.equals(secondMapKeys));
		System.out.println("firstMapKeys.equals(thirdMapKeys): " + firstMapKeys.equals(thirdMapKeys));
	}
}

Output

firstMapKeys.equals(secondMapKeys): true
firstMapKeys.equals(thirdMapKeys): false

Compare HashMap for values

If we want to compare two maps for same values, then we can use values() method. This method returns a Collection view of the values contained in this map.
Please note that HashMap allows duplicate values.

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

public class CompareHashMapExample {

	public static void main(String[] args) {
		Map<Integer, String> firstMap = new HashMap<>();

		firstMap.put(1, "one");
		firstMap.put(2, "two");
		firstMap.put(3, "three");

		Map<Integer, String> secondMap = new HashMap<>();

		secondMap.put(3, "three");
		secondMap.put(1, "one");
		secondMap.put(2, "two");

		Map<Integer, String> thirdMap = new HashMap<>();

		thirdMap.put(1, "one");
		thirdMap.put(2, "two");
		thirdMap.put(3, "three");
		thirdMap.put(4, "four");

		ArrayList firstMapValues = new ArrayList(firstMap.values());
		ArrayList secondMapValues = new ArrayList(secondMap.values());
		ArrayList thirdMapValues = new ArrayList(thirdMap.values());

		System.out.println("firstMapValues.equals(secondMapValues): " + firstMapValues.equals(secondMapValues));
		System.out.println("firstMapValues.equals(thirdMapValues): " + firstMapValues.equals(thirdMapValues));
	}
}

Output

firstMapValues.equals(secondMapValues): true
firstMapValues.equals(thirdMapValues): false

If you want to remove duplicates before comparison, convert collection of values to Set.

HashSet firstMapValues = new HashSet(firstMap.values());
HashSet secondMapValues = new HashSet(secondMap.values());
HashSet thirdMapValues = new HashSet(thirdMap.values());
        
System.out.println("firstMapValues.equals(secondMapValues): " + firstMapValues.equals(secondMapValues));
System.out.println("firstMapValues.equals(thirdMapValues): " + firstMapValues.equals(thirdMapValues));