位置:首頁 > Java技術 > Java.util包 > java.util.EnumMap.equals()方法實例

java.util.EnumMap.equals()方法實例

java.util.EnumMap.equals(Object o) 方法將指定對象與此映射是否相等比較。這兩個對象必須是映射。

聲明

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

public boolean equals(Object o)

參數

  • o -- 要進行比較與此映射相等的對象。

返回值

如果指定的對象等於此映射,此方法返回true

異常

  • NA

例子

下麵的例子顯示java.util.EnumMap.equals()方法的用法。

package com.yiibai;

import java.util.*;

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