Java HashMap和IdentityHashMap的区别

Java HashMap和IdentityHashMap的区别

java中的HashMap是一个类,是java集合的一部分。它实现了java的Map接口。它以键和值对的形式存储数据。键应该是唯一的,但值可能是重复的。如果你试图插入重复的键,它将替换相应键的元素。HashMap类似于哈希表,但它是不同步的。它允许存储空键和空值,但应该只有一个空键,可以有任意数量的空值。

IdentityHashMap实现了Map接口。在比较键(和值)时,它遵循引用平等原则,而不是对象平等原则。当用户要求通过引用来比较对象时,该类就会被使用。它是不同步的,必须在外部进行同步。这个类中的迭代器是快速失败的,当试图在迭代时进行修改时抛出ConcurrentModificationException。

HashMap和IdentityHashMap都是实现Map接口的类。但是它们之间存在着一些区别。

Java中HashMap和IdentityHashMap的区别

示例 1: HashMap

// Java program to illustrate
// the working of Java HashMap
// to demonstrate
// internal working difference between them
  
// Importing HashMap class from
// java.util package
import java.util.HashMap;
  
// Class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an empty HashMap object
        HashMap<Integer, String> map = new HashMap<>();
  
        // Add elements to the map
        // Custom inputs
        map.put(10, "Geeks");
        map.put(20, "for");
        map.put(30, "geeks");
        map.put(40, "welcome");
        map.put(50, "you");
  
        // Printing the size of map
        System.out.println("Size of map is:- "
                           + map.size());
  
        // Printing the HashMap content
        System.out.println("HashMap content: " + map);
  
        // Removing a key 50
        map.remove(50);
  
        // Printing the HashMap after the removal
        System.out.println("HashMap after removal : "
                           + map);
    }
}

输出

Size of map is:- 5
HashMap content: {50=you, 20=for, 40=welcome, 10=Geeks, 30=geeks}
HashMap after removal : {20=for, 40=welcome, 10=Geeks, 30=geeks}

示例 2: IdentityHashMap

// Java program to illustrate
// working of IdentityHashmap
// to demonstrate
// internal working difference between them
  
// Importing all classes of
// java.util package
import java.util.*;
  
// Class for iterating IdentityHashMap
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Creating an empty IdentityHashMap object
        IdentityHashMap<Integer, String> ihmap
            = new IdentityHashMap<Integer, String>();
  
        // Mapping string values to int keys
        // Custom inputs --> Custom mappings
        ihmap.put(10, "Geeks");
        ihmap.put(20, "for");
        ihmap.put(30, "Geeks");
        ihmap.put(40, "Welcomes");
        ihmap.put(50, "You");
  
        // Display the size of IdentityHashMap
        System.out.println("IdentityHashMap size : "
                           + ihmap.size());
  
        // Display the IdentityHashMap
        System.out.println("Initial identity hash map: "
                           + ihmap);
  
        // Create an Iterator over the IdentityHashMap
        Iterator<IdentityHashMap.Entry<Integer, String> >
            itr = ihmap.entrySet().iterator();
  
        // Condition check using hasNext() method()
  
        // Condition check using hasNext() method holding
        // true if there is any next element remaining
        while (itr.hasNext()) {
  
            // next() method which is used to
            // retrieve the next element
            IdentityHashMap.Entry<Integer, String> entry
                = itr.next();
  
            // Print and display key and value pairs
            // using getKey() method
            System.out.println("Key = " + entry.getKey()
                               + ", Value = "
                               + entry.getValue());
        }
    }
}

输出

IdentityHashMap size : 5
Initial identity hash map: {10=Geeks, 40=Welcomes, 50=You, 30=Geeks, 20=for}
Key = 10, Value = Geeks
Key = 40, Value = Welcomes
Key = 50, Value = You
Key = 30, Value = Geeks
Key = 20, Value = for

Java中HashMap和IdentityHashMap的区别

S.NO . HashMap IdentityHashMap
1. HashMap实现了Map接口,但它并没有违反地图的一般契约。 IdentityHashMap也实现了Map接口,但是它故意违反了地图的一般契约。
2. HashMap使用对象平等来比较键和值。 IdentityHashMap使用引用平等来比较键和值。
3. HashMap使用HashMap类的hashCode()方法来寻找桶的位置。 IdentityHashMap不使用hashCode()方法,而是使用System.IdentityHashCode()方法来寻找桶的位置。
4. HashMap使用链式。 IdentityHashMap使用一个简单的衬垫探测哈希表。
5. 为了安全地在HashMap中存储对象,键需要是不可变的。 IdentityHashMap并不要求密钥是不可变的。
6. HashMap的表现比IdentityHashMap略差。 IdentityHashMap比HashMap表现得更好。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程