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


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


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

用法:

ihashmap1.equals(ihashmap2)

參數:該方法接受一個身份哈希映射類型的參數ihashmap2,表示要使用此哈希映射檢查其相等性的映射。


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

以下程序說明了java.util.IdentityHashMap.equals()方法的用法:
示例1:

// Java code to illustrate the equals() method 
import java.util.*; 
  
public class Identity_Hash_Map_Demo { 
    public static void main(String[] args) 
    { 
  
        // Creating an empty IdentityHashMap 
        IdentityHashMap<Integer, String> identity_hash1 =  
                   new IdentityHashMap<Integer, String>(); 
        IdentityHashMap<Integer, String> identity_hash2 =  
                   new IdentityHashMap<Integer, String>(); 
  
        // Mapping string values to int keys 
        identity_hash1.put(10, "Geeks"); 
        identity_hash1.put(15, "4"); 
        identity_hash1.put(20, "Geeks"); 
        identity_hash1.put(25, "Welcomes"); 
        identity_hash1.put(30, "You"); 
          
        // Mapping string values to int keys 
        identity_hash2.put(10, "Geeks"); 
        identity_hash2.put(15, "4"); 
        identity_hash2.put(20, "Geeks"); 
        identity_hash2.put(25, "Welcomes"); 
        identity_hash2.put(30, "You"); 
  
        // Displaying the IdentityHashMap 
        System.out.println("First Map: "
                        + identity_hash1); 
  
        // Displaying the final IdentityHashMap 
        System.out.println("Second Map: "
                        + identity_hash2); 
                          
        // Displaying the equality 
        System.out.println("Equality: "+ 
                  identity_hash1.equals(identity_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 Identity_Hash_Map_Demo { 
    public static void main(String[] args) 
    { 
  
        // Creating an empty IdentityHashMap 
        IdentityHashMap<Integer, String> identity_hash1 =  
                   new IdentityHashMap<Integer, String>(); 
        IdentityHashMap<Integer, String> identity_hash2 =  
                   new IdentityHashMap<Integer, String>(); 
  
        // Mapping string values to int keys 
        identity_hash1.put(10, "Geeks"); 
        identity_hash1.put(15, "4"); 
        identity_hash1.put(20, "Geeks"); 
        identity_hash1.put(25, "Welcomes"); 
        identity_hash1.put(30, "You"); 
          
        // Mapping string values to int keys 
        identity_hash2.put(10, "Geeks"); 
        identity_hash2.put(15, "4"); 
        identity_hash2.put(20, "Geek"); 
        identity_hash2.put(25, "Welcomes"); 
        identity_hash2.put(30, "You"); 
  
        // Displaying the IdentityHashMap 
        System.out.println("First Map: "
                        + identity_hash1); 
  
        // Displaying the final IdentityHashMap 
        System.out.println("Second Map: "
                        + identity_hash2); 
                          
        // Displaying the equality 
        System.out.println("Equality: "+ 
                  identity_hash1.equals(identity_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大神的英文原創作品 IdentityHashMap equals() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。