find difference between two hashmaps java code example

Example 1: difference between hashmap and map java

Hash map: 

	-> It is the implementation of map interface.
	-> It can contain null values and keys.
	-> It doesn't maintain input order.

Map: 

	-> It is an interface
	-> Map has two implementation
	-> Tree Map
	-> Hash Map
	-> Tree Map maintains the input order .
	-> It will not allow any null values.
	-> Duplicate keys are not allowed in the map

Example 2: how to compare two maps in java

@Test
public void whenCompareTwoHashMapsUsingEquals_thenSuccess() {
    Map<String, String> asiaCapital1 = new HashMap<String, String>();
    asiaCapital1.put("Japan", "Tokyo");
    asiaCapital1.put("South Korea", "Seoul");
 
    Map<String, String> asiaCapital2 = new HashMap<String, String>();
    asiaCapital2.put("South Korea", "Seoul");
    asiaCapital2.put("Japan", "Tokyo");
 
    Map<String, String> asiaCapital3 = new HashMap<String, String>();
    asiaCapital3.put("Japan", "Tokyo");
    asiaCapital3.put("China", "Beijing");
 
    asiaCapital1.equals(asiaCapital2);
    asiaCapital1.equals(asiaCapital3);
}

Tags:

Java Example