Java Utility Library

Java EnumMap - equals() Method



The java.util.EnumMap.equals() method is used to compare the specified object with this map for equality. The method returns true if the given object is also a map and the two maps represent the same mappings.

Syntax

public boolean equals(Object obj)

Parameters

obj Specify the object to be compared for equality with this map.

Return Value

Returns true if the specified object is equal to this map, false otherwise.

Exception

NA.

Example:

In the example below, the java.util.EnumMap.equals() method is used to compare EnumMaps for equality.

import java.util.*;

public class MyClass {
  
  //creating an enum
  public enum weekday{
    RED, BLUE, GREEN
  }

  public static void main(String[] args) {
    //creating EnumMaps
    EnumMap<weekday,Integer> Map1 = 
        new EnumMap<weekday,Integer>(weekday.class);
    EnumMap<weekday,Integer> Map2 = 
        new EnumMap<weekday,Integer>(weekday.class);
    EnumMap<weekday,Integer> Map3 = 
        new EnumMap<weekday,Integer>(weekday.class);

    //associate values in Map1
    Map1.put(weekday.RED, 1);
    Map1.put(weekday.BLUE, 2);
    Map1.put(weekday.GREEN, 3);

    //associate values in Map2
    Map2.put(weekday.RED, 1);
    Map2.put(weekday.BLUE, 2);
    Map2.put(weekday.GREEN, 3);

    //associate values in Map3
    Map3.put(weekday.RED, 10);
    Map3.put(weekday.BLUE, 20);
    Map3.put(weekday.GREEN, 30);

    //check if Map1 and Map2 are equal
    System.out.println("Map1 and Map2 are equal? - " + Map1.equals(Map2));

    //check if Map1 and Map2 are equal
    System.out.println("Map1 and Map3 are equal? - " + Map1.equals(Map3));
  }
}

The output of the above code will be:

Map1 and Map2 are equal? - true
Map1 and Map3 are equal? - false

❮ Java.util - EnumMap