當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java AbstractMap equals()用法及代碼示例


Java 中的 AbstractMap.equals() 方法用於檢查兩個映射之間的相等性。它驗證作為參數傳遞的一個映射的元素是否等於該映射的元素。

用法:

AbstractMap1.equals(AbstractMap2)

參數:該方法接受一個抽象哈希映射類型的參數ihashmap2,表示要與該抽象映射檢查其相等性的映射。

返回值:如果兩個對象映射都相等,則該方法返回 true,否則返回 false。

以下示例程序旨在說明 AbstractMap.equals() 方法的用法:



程序1:


// Java code to illustrate the equals() method
import java.util.*;
  
public class Abstract_Map_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty AbstractMap
        AbstractMap<Integer, String>
            abstract_hash1 = new IdentityHashMap<Integer, String>();
  
        AbstractMap<Integer, String>
            abstract_hash2 = new IdentityHashMap<Integer, String>();
  
        // Mapping string values to int keys
        abstract_hash1.put(10, "Geeks");
        abstract_hash1.put(15, "4");
        abstract_hash1.put(20, "Geeks");
        abstract_hash1.put(25, "Welcomes");
        abstract_hash1.put(30, "You");
  
        // Mapping string values to int keys
        abstract_hash2.put(10, "Geeks");
        abstract_hash2.put(15, "4");
        abstract_hash2.put(20, "Geeks");
        abstract_hash2.put(25, "Welcomes");
        abstract_hash2.put(30, "You");
  
        // Displaying the AsbstractMap
        System.out.println("First Map:"
                           + abstract_hash1);
  
        // Displaying the final AbstractMap
        System.out.println("Second Map:"
                           + abstract_hash2);
  
        // Displaying the equality
        System.out.println("Equality:"
                           + abstract_hash1.equals(abstract_hash2));
    }
}
輸出:
First Map:{10=Geeks, 30=You, 20=Geeks, 25=Welcomes, 15=4}
Second Map:{10=Geeks, 30=You, 20=Geeks, 25=Welcomes, 15=4}
Equality:true

程序2:


// Java code to illustrate the equals() method
  
import java.util.*;
  
public class Abstract_Map_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty AbstractMap
        AbstractMap<Integer, String>
            abstract_hash1 = new IdentityHashMap<Integer, String>();
  
        AbstractMap<Integer, String>
            abstract_hash2 = new IdentityHashMap<Integer, String>();
  
        // Mapping string values to int keys
        abstract_hash1.put(10, "Geeks");
        abstract_hash1.put(15, "4");
        abstract_hash1.put(20, "Geeks");
        abstract_hash1.put(25, "Welcomes");
        abstract_hash1.put(30, "You");
  
        // Mapping string values to int keys
        abstract_hash2.put(10, "Geeks");
        abstract_hash2.put(15, "4");
        abstract_hash2.put(20, "Geek");
        abstract_hash2.put(25, "Welcomes");
        abstract_hash2.put(30, "You");
  
        // Displaying the IdentityHashMap
        System.out.println("First Map:"
                           + abstract_hash1);
  
        // Displaying the final IdentityHashMap
        System.out.println("Second Map:"
                           + abstract_hash2);
  
        // Displaying the equality
        System.out.println("Equality:"
                           + abstract_hash1.equals(abstract_hash2));
    }
}
輸出:
First Map:{10=Geeks, 30=You, 20=Geeks, 25=Welcomes, 15=4}
Second Map:{10=Geeks, 30=You, 20=Geek, 25=Welcomes, 15=4}
Equality:false




相關用法


注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 AbstractMap equals() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。