Java EnumMap equals() 方法



描述

Java EnumMap equals(Object o) 方法比较指定的对象与此映射的相等性。两个对象都必须是映射。

声明

以下是java.util.EnumMap.equals() 方法的声明

public boolean equals(Object o)

参数

o − 要与此映射比较相等性的对象。

返回值

如果指定的对象等于此映射,则此方法返回 true

异常

检查枚举、整数对的 EnumMap 相等性示例

以下示例演示了 Java EnumMap equals() 方法的使用,用于比较 EnumMap 实例。我们创建了一个枚举 Numbers。然后创建了两个 enum Numbers 和 Integer 的 EnumMap 实例。添加了一些条目并比较了 enumMap 对象。

package com.tutorialspoint;

import java.util.EnumMap;

public class EnumMapDemo {

   // create an enum
   public enum Numbers {
      ONE, TWO, THREE, FOUR, FIVE
   };

   public static void main(String[] args) {
   
      EnumMap<Numbers, Integer> map1 =
         new EnumMap<>(Numbers.class);

      EnumMap<Numbers, Integer> map2 =
      new EnumMap<>(Numbers.class);

      // associate values in map1
      map1.put(Numbers.ONE, 1);
      map1.put(Numbers.TWO, 2);
      map1.put(Numbers.THREE, 3);
      map1.put(Numbers.FOUR, 4);

      // associate values in map2
      map2.put(Numbers.ONE, 1);
      map2.put(Numbers.TWO, 2);
      map2.put(Numbers.THREE, 3);
      map2.put(Numbers.FOUR, 4);

      // print the maps
      System.out.println("map1:" + map1);
      System.out.println("map2:" + map2);

      // check for equality between maps
      boolean equal = map1.equals(map2);

      // print the result
      System.out.println("Map1 and map 2 are equal:" + equal);

      // add one more value in map2
      map2.put(Numbers.FIVE, 5);

      // check for equality between maps
      equal = map1.equals(map2);

      // print the result
      System.out.println("Map1 and map 2 are equal:" + equal);
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

map1:{ONE=1, TWO=2, THREE=3, FOUR=4}
map2:{ONE=1, TWO=2, THREE=3, FOUR=4}
Map1 and map 2 are equal:true
Map1 and map 2 are equal:false

检查枚举、字符串对的 EnumMap 相等性示例

以下示例演示了 Java EnumMap equals() 方法的使用,用于比较 EnumMap 实例。我们创建了一个枚举 Numbers。然后创建了两个 enum Numbers 和 String 的 EnumMap 实例。添加了一些条目并比较了 enumMap 对象。

package com.tutorialspoint;

import java.util.EnumMap;

public class EnumMapDemo {

   // create an enum
   public enum Numbers {
      ONE, TWO, THREE, FOUR, FIVE
   };

   public static void main(String[] args) {
   
      EnumMap<Numbers, String> map1 =
         new EnumMap<Numbers, String>(Numbers.class);

      EnumMap<Numbers, String> map2 =
      new EnumMap<Numbers, String>(Numbers.class);

      // associate values in map1
      map1.put(Numbers.ONE, "1");
      map1.put(Numbers.TWO, "2");
      map1.put(Numbers.THREE, "3");
      map1.put(Numbers.FOUR, "4");

      // associate values in map2
      map2.put(Numbers.ONE, "1");
      map2.put(Numbers.TWO, "2");
      map2.put(Numbers.THREE, "3");
      map2.put(Numbers.FOUR, "4");

      // print the maps
      System.out.println("map1:" + map1);
      System.out.println("map2:" + map2);

      // check for equality between maps
      boolean equal = map1.equals(map2);

      // print the result
      System.out.println("Map1 and map 2 are equal:" + equal);

      // add one more value in map2
      map2.put(Numbers.FIVE, "5");

      // check for equality between maps
      equal = map1.equals(map2);

      // print the result
      System.out.println("Map1 and map 2 are equal:" + equal);
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

map1:{ONE=1, TWO=2, THREE=3, FOUR=4}
map2:{ONE=1, TWO=2, THREE=3, FOUR=4}
Map1 and map 2 are equal:true
Map1 and map 2 are equal:false

检查枚举、对象对的 EnumMap 相等性示例

以下示例演示了 Java EnumMap equals() 方法的使用,用于比较 EnumMap 实例。我们创建了一个枚举 Numbers。然后创建了两个 enum Numbers 和 Student 对象的 EnumMap 实例。添加了一些条目并比较了 enumMap 对象。

package com.tutorialspoint;

import java.util.EnumMap;

public class EnumMapDemo {

   // create an enum
   public enum Numbers {
      ONE, TWO, THREE, FOUR, FIVE
   };

   public static void main(String[] args) {
   
      EnumMap<Numbers, Student> map1 =
         new EnumMap<>(Numbers.class);

      EnumMap<Numbers, Student> map2 =
      new EnumMap<>(Numbers.class);

      // associate values in map1
      map1.put(Numbers.ONE, new Student(1, "Julie"));
      map1.put(Numbers.TWO, new Student(2, "Robert"));
      map1.put(Numbers.THREE,new Student(3, "Adam"));

      // associate values in map2
      map2.put(Numbers.ONE, new Student(1, "Julie"));
      map2.put(Numbers.TWO, new Student(2, "Robert"));
      map2.put(Numbers.THREE,new Student(3, "Adam"));

      // print the maps
      System.out.println("map1:" + map1);
      System.out.println("map2:" + map2);

      // check for equality between maps
      boolean equal = map1.equals(map2);

      // print the result
      System.out.println("Map1 and map 2 are equal:" + equal);

      // add one more value in map2
      map2.put(Numbers.FOUR, new Student(4, "Jene"));

      // check for equality between maps
      equal = map1.equals(map2);

      // print the result
      System.out.println("Map1 and map 2 are equal:" + equal);
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   @Override
   public boolean equals(Object obj) {
      if(obj == null) return false;
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

map1:{ONE=[ 1, Julie ], TWO=[ 2, Robert ], THREE=[ 3, Adam ]}
map2:{ONE=[ 1, Julie ], TWO=[ 2, Robert ], THREE=[ 3, Adam ]}
Map1 and map 2 are equal:true
Map1 and map 2 are equal:false
java_util_enummap.htm
广告