Java程序 实现IdentityHashMap API

Java程序 实现IdentityHashMap API

IdentityHashMap使用Hashtable实现了Map接口,在比较键(和值)的时候使用引用-平等来代替对象-平等。这个类不是一个通用的Map实现。虽然该类实现了Map接口,但它故意违反了Map的一般契约,该契约规定在比较对象时必须使用equals()方法。当用户需要通过引用来比较对象时,就会用到这个类。它属于java.util包。它后来在Java 1.4中被加入

在java中,IdentityHashMap和HashMap的主要区别在于,IdentityHashMap是Map接口的一个特殊实现,它不像其他Map的实现,例如HashMap,使用equals(obj)和hashCode()方法来比较对象。相反,IdentityHashMap使用平等运算符”==”来比较Java中的键和值,这使得它比HashMap更快,并且适合需要引用平等检查而不是逻辑平等的情况。

IdentityHashMap和HashMap的区别如下。

HashMap与IdentityHashMap的主要区别在于,IdentityHashMap使用平等运算符”==”来比较Map内部的键和值,而HashMap使用equals(obj)方法来比较键和值。
由于IdentityHashMap不使用equals(obj),对于具有昂贵的equals(obj)和hashCode()的对象,其速度相对来说比HashMap快。
3.HashMap和IdentityHashMap的另一个区别是键的不可变性。当我们在HashMap中存储对象时,安全的基本要求之一是键需要是不可变的,IdentityHashMap不要求键是不可变的,因为它不依赖equals(obj)和hashCode()。

示例 1:

// Java Program to illustrate IdentityHashMap by
// Differentiating between HashMap and IdentityHashMap
 
// Importing input output classes
import java.io.*;
// Importing HashMap, IdentityHashMap and Map classes
// from java.util package
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;
 
// Main Class
public class GFG {
 
    // Main driver method
    public static void main (String[] args) {
 
        // Creating objects of HashMap and IdentityHashMap objects
        // Declaring objects of type String and Integer type
        Map<String, Integer> hashMap = new HashMap<String, Integer>();
        Map<String, Integer> identityHashMap = new IdentityHashMap<String, Integer>();
 
        // Adding elements to objects of Maps created above
        // Elements are of type key-value pairs using put() method
        // Custom entries
        hashMap.put("Scala", 100);
        hashMap.put(new String("Scala"), 101);
        hashMap.put("Groovy", 56);
        hashMap.put(new String("Groovy"), 57);
 
        identityHashMap.put("Ruby", 25);
        identityHashMap.put(new String("Ruby"), 27);
        identityHashMap.put("Swift", 12);
        identityHashMap.put(new String("Swift"), 13);
 
 
        // hashMap.size() will print 2 since
        // it compares the objects logically and both the keys are same
        System.out.println("Size of HashMap is : " + hashMap.size());
 
        // identityHashMap.size() will print 4 since
        // it compares the objects by reference
        System.out.println("Size of IdentityHashMap is : " + identityHashMap.size());
    }
}
Java

输出

Size of HashMap is : 2
Size of IdentityHashMap is : 4
Java

Implementation: HashMap使用hashCode()来查找桶的位置。IdentityHashMap不使用hashCode(),而是使用System.identityHashCode(object)。

示例 2:

// Java Program to illustrate IdentityHashMap by
// Differentiating between HashMap and IdentityHashMap
 
// Importing input output classes
import java.io.*;
// Importing Map,HashMap and IdentityHashMap classes
// from java.util package
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;
 
// Class 1
// Helper Class
class ProgrammingLanguage {
 
    // Member variables of this class
    private String language;
    private Integer value;
 
    // Constructor of this class
    public ProgrammingLanguage(String language,
                               Integer value)
    {
 
        // this keyword refers to current object itself
        this.language = language;
        this.value = value;
    }
 
    // Method 1
    public String getlanguage()
    {
 
        // Returning language
        return language;
    }
 
    // Method 2
    public Integer getValue()
    {
 
        // Returning value associated with a language
        return value;
    }
 
    // Method 3
    // Sets a new value for a language
    public void setValue(Integer value)
    {
        this.value = value;
    }
 
    // Method 4
    // @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result
            = prime * result
              + ((language == null) ? 0 : value.hashCode());
        result
            = prime * result
              + ((language == null) ? null
                                    : language.hashCode());
        return result;
    }
 
    // Method 5
    // @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        ProgrammingLanguage other
            = (ProgrammingLanguage)obj;
        if (value == null) {
            if (other.value != null)
                return false;
        }
        else if (!value.equals(other.value))
            return false;
        if (language == null) {
            if (other.language != null)
                return false;
        }
        else if (!language.equals(other.language))
            return false;
        return true;
    }
}
 
// Class 2
// Main Class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        ProgrammingLanguage groovy
            = new ProgrammingLanguage("Groovy", 1000);
        ProgrammingLanguage scala
            = new ProgrammingLanguage("Scala", 2000);
        ProgrammingLanguage ruby
            = new ProgrammingLanguage("Ruby", 3000);
 
        Map<ProgrammingLanguage, Integer> languageValue
            = new HashMap<>();
        Map<ProgrammingLanguage, Integer>
            languageValueIdentity = new IdentityHashMap<>();
 
        // Inserting elements to object of HashMap created
        // Custom entries
        languageValue.put(groovy, groovy.getValue());
        languageValue.put(scala, scala.getValue());
        languageValue.put(ruby, ruby.getValue());
 
        // Inserting elements to object of IdentityHashMap
        // created Custom entries
        languageValueIdentity.put(groovy,
                                  groovy.getValue());
        languageValueIdentity.put(scala, scala.getValue());
        languageValueIdentity.put(ruby, ruby.getValue());
 
        // Display message only
        System.out.println("Before modifying keys  :  ");
 
        String result = languageValue.get(groovy) != null
                            ? "Yes"
                            : "No";
 
        // Print commands to Display the result
        System.out.println(
            "Does Groovy language exists in HashMap? "
            + result);
 
        result = languageValueIdentity.get(groovy) != null
                     ? "Yes"
                     : "No";
 
        System.out.println(
            "Does Groovy language in IdentityHashMap? "
            + result);
 
        // Now, modifying the value object
        groovy.setValue(5000);
 
        // Display message only
        System.out.println("After modifying keys  :  ");
 
        // Print commands to Display the result
        result = languageValue.get(groovy) != null ? " Yes "
                                                   : " No ";
 
        System.out.println(
            "Does Groovy language exists in HashMap? "
            + result);
 
        result = languageValueIdentity.get(groovy) != null
                     ? " Yes "
                     : " No ";
 
        System.out.println(
            "Does Groovy language exists in IdentityHashMap? "
            + result);
    }
}
Java

输出

Before modifying keys  :  
Does Groovy language exists in HashMap? Yes
Does Groovy language in IdentityHashMap? Yes
After modifying keys  :  
Does Groovy language exists in HashMap?  No 
Does Groovy language exists in IdentityHashMap?  Yes 
Java

输出解释:

从输出结果可以看出,一旦编程语言的对象被改变,也就是HashMap和IdentityHashMap中的关键对象,在HashMap中我们无法检索到一个对象,但是当你使用IdentityHashMap时却能够检索到,因为前者使用equals()方法,一旦值被改变就返回错误,而后者使用”==”操作符,返回真实,因为在两种情况下对象在堆中是一样的。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册