Map comparison

In this chapter you will learn:

  1. How to compare two Map

Map equals

equals() methods tells us if two maps are equal. In order to be equal they must have the same key-value pairs.

import java.util.HashMap;
import java.util.Map;
/*from  j  a va2 s  . c o m*/
public class Main {
  public static void main(String[] a) {
    Map<String,String> map = new HashMap<String,String>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");
    Map<String,String> map2 = new HashMap<String,String>();
    map2.put("key2", "value2");
    map2.put("key1", "value1");
    map2.put("key3", "value3");
    System.out.println(map1.equals(map2));
  }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. What is HashMap class
  2. How to use HashMap to store command line arguments
  3. HashMap and hashCode
Home » Java Tutorial » Map
Map interface
Map element adding
Map.Entry class
Map key
Map value
Map key/value search
Map delete/remove
Map comparison
HashMap Class
HashMap search
HashMap clone
TreeMap
TreeMap key
TreeMap head sub map
TreeMap tail sub map
TreeMap sub map
NavigableMap
NavigableMap key
NavigableMap key-value pair
LinkedHashMap Class
IdentityHashMap
SortedMap