当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Java HashMap和IdentityHashMap的区别用法及代码示例

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

IdentityHashMap实施Map接口。比较键(和值)时,它遵循 reference-equality 代替 object-equality。当用户需要通过引用比较对象时使用该类。它不同步,必须外部同步。这个类中的迭代器是fail-fast,抛出ConcurrentModificationException 尝试在迭代时进行修改。

HashMap 和IdentityHashMap 都是实现Map 接口的类。但它们之间几乎没有什么区别。

示例 1:HashMap

Java


// 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


// 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

Difference between HashMap and IdentityHashMap in Java

S.NO. HashMap 身份哈希映射
1. HashMap实现了Map接口,但并不违反Map通用契约。 IdentityHashMap也实现了Map接口,但它故意违反了Map通用契约。
2. HashMap 使用对象相等性来比较键和值。 IdentityHashMap 使用引用相等来比较键和值。
3. HashMap使用HashMap类的hashCode()方法来查找存储桶位置。 IdentityHashMap 不使用 hashCode() 方法,而是使用 System.IdentityHashCode() 方法来查找存储桶位置。
4. HashMap 使用链接。 IdentityHashMap 使用a 简单线性探测哈希表。
5. 为了安全地将对象存储在HashMap中, key 需要是不可变的。 IdentityHashMap 不要求 key 不可变。
6. HashMap 的性能略低于 IdentityHashMap。 IdentityHashMap 比 HashMap 性能更好。


相关用法


注:本文由纯净天空筛选整理自prashant_srivastava大神的英文原创作品 Difference between HashMap and IdentityHashMap in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。