Java.util.Hashtable 类

java.util.Hashtable.equals() 方法用于比较指定的 Object 与此 Map 是否相等,如下根据Map接口中的定义。

语法

public boolean equals(Object obj)

参数

obj 指定要与此哈希表比较是否相等的对象。

返回值

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

异常

无。

示例:

在下面的示例中,java.util.Hashtable.equals() 方法用于将指定的 Object 与给定的哈希表进行比较。

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //创建哈希表
    Hashtable<Integer, String> Htable1 = new Hashtable<Integer, String>();
    Hashtable<Integer, String> Htable2 = new Hashtable<Integer, String>();
    Hashtable<Integer, String> Htable3 = new Hashtable<Integer, String>();
    
    //填充Htable1
    Htable1.put(101, "John");
    Htable1.put(102, "Marry");
    Htable1.put(103, "Kim");

    //填充Htable2
    Htable2.put(101, "John");
    Htable2.put(102, "Marry");
    Htable2.put(103, "Kim");

    //填充Htable3
    Htable3.put(1, "JAN");
    Htable3.put(2, "FEB");
    Htable3.put(3, "MAR");

    //检查Htable1和Htable2是否相等
    System.out.print("Are Htable1 and Htable2 equal? : ");  
    System.out.println(Htable1.equals(Htable2)); 

    //检查Htable1和Htable3是否相等
    System.out.print("Are Htable1 and Htable3 equal? : ");  
    System.out.println(Htable1.equals(Htable3));  
  }
}

上述代码的输出将是:

Are Htable1 and Htable2 equal? : true
Are Htable1 and Htable3 equal? : false