Java中的HashMap是一个类,它是Java集合的一部分。它实现了Java的 Map 接口。它以键值对的形式存储数据。键应该是唯一的,但值可能是重复的。如果您尝试插入重复键,它将替换相应键的元素。 HashMap 类似于哈希表,但它是不同步的。它允许存储空键和空值,但应该只有一个空键并且可以有任意数量的空值。
IdentityHashMap 实现了 Map 接口。在比较键(和值)时,它遵循引用相等而不是对象相等。当用户需要通过引用来比较对象时,将使用此类。它不是同步的,必须在外部同步。此类中的迭代器是快速失败的,抛出 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 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);
}
}
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 ihmap
= new IdentityHashMap();
// 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 >
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 entry
= itr.next();
// Print and display key and value pairs
// using getKey() method
System.out.println("Key = " + entry.getKey()
+ ", Value = "
+ entry.getValue());
}
}
}
输出
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 ihmap
= new IdentityHashMap();
// 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 >
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 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
JavaHashMap和IdentityHashMap的区别
S.NO. | HashMap | IdentityHashMap |
---|---|---|
1. | HashMap implements the Map interface but it doesn’t violate the map general contract. | IdentityHashMap also implements the Map interface but it intentionally violates the map general contract. |
2. | HashMap uses object equality to compare the key and values. | IdentityHashMap uses reference equality to compare the key and values. |
3. | HashMap uses the hashCode() method of HashMap class to find the bucket location. | IdentityHashMap doesn’t use the hashCode() method instead it uses the System.IdentityHashCode() method to find the bucket location. |
4. | HashMap uses chaining. | IdentityHashMap uses a simple liner probe hash table. |
5. | To safely store the objects in HashMap the keys need to be immutable. | IdentityHashMap doesn’t require the key to be immutable. |
6. | HashMap performs slightly less than the IdentityHashMap. | IdentityHashMap performs better than HashMap. |