Java Utility Library

Java Hashtable - equals() Method



The java.util.Hashtable.equals() method is used to compare the specified Object with this Map for equality, as per the definition in the Map interface.

Syntax

public boolean equals(Object obj)

Parameters

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

Return Value

Returns true if the specified Object is equal to this Map.

Exception

NA.

Example:

In the example below, the java.util.Hashtable.equals() method is used to compare the specified Object with the given hashtable.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating hashtables
    Hashtable<Integer, String> Htable1 = new Hashtable<Integer, String>();
    Hashtable<Integer, String> Htable2 = new Hashtable<Integer, String>();
    Hashtable<Integer, String> Htable3 = new Hashtable<Integer, String>();
    
    //populating Htable1
    Htable1.put(101, "John");
    Htable1.put(102, "Marry");
    Htable1.put(103, "Kim");

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

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

    //checking Htable1 and Htable2 for equality
    System.out.print("Are Htable1 and Htable2 equal? : ");  
    System.out.println(Htable1.equals(Htable2)); 

    //checking Htable1 and Htable3 for equality
    System.out.print("Are Htable1 and Htable3 equal? : ");  
    System.out.println(Htable1.equals(Htable3));  
  }
}

The output of the above code will be:

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

❮ Java.util - Hashtable