Java.util.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
 
转载请保留页面地址:https://www.breakyizhan.com/java/5338.html
扫描二维码添加微信 
  • ,每次淘宝领取淘宝优惠券,淘宝购物更优惠。现在添加微信,还可以领取机械键盘50元优惠券!添加微信后回复机械键盘即可领取!
    支持我们,就用微信淘宝!