IdentityHashMap、WeakHashMap 和 EnumMap 都是Java集合中实现 Map 接口的类。但它们之间几乎没有区别。
1. IdentityHashMap : IdentityHashMap 实现了 Map 接口。在比较键(和值)时,它遵循引用相等而不是对象相等。当用户需要通过引用来比较对象时,将使用此类。它不是同步的,必须在外部同步。此类中的迭代器是快速失败的,在迭代时抛出ConcurrentModificationException以尝试修改。
IdentityHashMap 的工作:
Java
// Java program to illustrate the
// working of IdentityHashmap
import java.util.*;
public class IteratingIdentityHashMap {
public static void main(String[] args)
{
// Creating an empty IdentityHashMap
IdentityHashMap ihmap
= new IdentityHashMap();
// Mapping string values to int keys
ihmap.put(10, "Geeks");
ihmap.put(20, "4");
ihmap.put(30, "Geeks");
ihmap.put(40, "Welcomes");
ihmap.put(50, "You");
// Displaying the size of IdentityHashMap
System.out.println("IdentityHashMap size : "
+ ihmap.size());
// Displaying the IdentityHashMap
System.out.println("Initial identity hash map: "
+ ihmap);
// Create an Iterator over the
// IdentityHashMap
Iterator >
itr = ihmap.entrySet().iterator();
// The hasNext() method is used to check if there is
// a next element The next() method is used to
// retrieve the next element
while (itr.hasNext())
{
IdentityHashMap.Entry entry = itr.next();
System.out.println("Key = " + entry.getKey()
+ ", Value = "
+ entry.getValue());
}
}
}
Java
// Java program to illustrate
// the WeakHashMap
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
class WeakHashMapdemo {
public static void main(String[] arg)
{
Map whmap = new WeakHashMap();
whmap.put(1, "geeks");
whmap.put(2, "4");
whmap.put(3, "geeks");
whmap.put(4, "welcomes");
whmap.put(5, "you");
// Displaying weak hash map
System.out.println("WeakHashMap is : " + whmap);
// Checking if "welcomes" exist
if (whmap.containsValue("welcomes"))
System.out.println("Yes welcomes exist");
// Checking if 3 exist as a key in map
if (whmap.containsKey(3))
System.out.println("Yes 3 exist");
// Creating set for key
Set keyset = whmap.keySet();
// Displaying key set
System.out.println("key Set : " + keyset);
Collection values = whmap.values();
// Displaying values of map
System.out.println("Values : " + values);
// Removing all data
whmap.clear();
// Checking whether map is empty or not
if (whmap.isEmpty())
System.out.println("Empty WeakHashMap: " + whmap);
}
}
Java
// Java program to illustrate working
// of EnumMap
import java.util.*;
class EnumMapExample {
public enum Months {
January,
February,
March,
April;
}
public static void main(String[] args)
{
// Creating an EnumMap of the Days enum
EnumMap enumMap = new EnumMap<>(Months.class);
// Insert using put() method
enumMap.put(Months.January, 31);
enumMap.put(Months.February, 28);
enumMap.put(Months.March, 31);
enumMap.put(Months.April, 30);
// Printing size of EnumMap
System.out.println("Size of EnumMap: "
+ enumMap.size());
// Printing the EnumMap
for (Map.Entry m : enumMap.entrySet())
{
System.out.println(m.getKey() + " "
+ m.getValue());
}
}
}
输出
IdentityHashMap size : 5
Initial identity hash map: {10=Geeks, 40=Welcomes, 50=You, 30=Geeks, 20=4}
Key = 10, Value = Geeks
Key = 40, Value = Welcomes
Key = 50, Value = You
Key = 30, Value = Geeks
Key = 20, Value = 4
2.弱HashMap: WeakHashMap 是 Map 接口的实现,它只存储弱键。在 WeakHashMap 中,我们只能存储其键的弱引用,这使得键值对在其键不再正常使用时被垃圾回收。 WeakHashMap 是基于 HashTable 的实现,但它不是同步的。它允许您存储空键和空值。
WeakHashMap 的工作:
Java
// Java program to illustrate
// the WeakHashMap
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
class WeakHashMapdemo {
public static void main(String[] arg)
{
Map whmap = new WeakHashMap();
whmap.put(1, "geeks");
whmap.put(2, "4");
whmap.put(3, "geeks");
whmap.put(4, "welcomes");
whmap.put(5, "you");
// Displaying weak hash map
System.out.println("WeakHashMap is : " + whmap);
// Checking if "welcomes" exist
if (whmap.containsValue("welcomes"))
System.out.println("Yes welcomes exist");
// Checking if 3 exist as a key in map
if (whmap.containsKey(3))
System.out.println("Yes 3 exist");
// Creating set for key
Set keyset = whmap.keySet();
// Displaying key set
System.out.println("key Set : " + keyset);
Collection values = whmap.values();
// Displaying values of map
System.out.println("Values : " + values);
// Removing all data
whmap.clear();
// Checking whether map is empty or not
if (whmap.isEmpty())
System.out.println("Empty WeakHashMap: " + whmap);
}
}
输出
WeakHashMap is : {5=you, 4=welcomes, 3=geeks, 2=4, 1=geeks}
Yes welcomes exist
Yes 3 exist
key Set : [5, 4, 3, 2, 1]
Values : [you, welcomes, geeks, 4, geeks]
Empty WeakHashMap: {}
3. EnumMap : EnumMap 是枚举类型的 Map 接口的特殊实现。它扩展了 AbstractMap 并在Java实现了 Map 接口。 EnumMap 的几个重要特性如下:
- EnumMap 类是Java Collections Framework 的成员,它不是同步的。
- EnumMap 是一个有序的集合,它们是按照键的自然顺序维护的(键的自然顺序是指在枚举类型中声明枚举常量的顺序)
- EnumMap 比 HashMap 快得多。
- 每个 EnumMap 实例的所有键必须是相同枚举类型的键。
- 如果我们尝试插入空键,EnumMap 不允许插入空键,它会抛出NullPointerException 。
- EnumMap 在内部表示为数组,因此它提供了更好的性能。
EnumMap 的工作:
Java
// Java program to illustrate working
// of EnumMap
import java.util.*;
class EnumMapExample {
public enum Months {
January,
February,
March,
April;
}
public static void main(String[] args)
{
// Creating an EnumMap of the Days enum
EnumMap enumMap = new EnumMap<>(Months.class);
// Insert using put() method
enumMap.put(Months.January, 31);
enumMap.put(Months.February, 28);
enumMap.put(Months.March, 31);
enumMap.put(Months.April, 30);
// Printing size of EnumMap
System.out.println("Size of EnumMap: "
+ enumMap.size());
// Printing the EnumMap
for (Map.Entry m : enumMap.entrySet())
{
System.out.println(m.getKey() + " "
+ m.getValue());
}
}
}
输出
Size of EnumMap: 4
January 31
February 28
March 31
April 30
IdentityHashMap、WeakHashMap 和 EnumMap 的区别:
PROPERTIES | IdentityHashMap | WeakHashMap | EnumMap |
---|---|---|---|
References | IdentityHashMap stores strong key reference. | WeakHashMap stores the weak key reference. | EnumMap stores the strong key reference. |
Search and get the values | It uses equality operator (==) to search and get the values. | It uses equals() method for that purpose. | It also uses equals() method for that purpose. |
Keys | It allows to store any type of keys. | It also allows to store any type of keys. | It allows to store only enum type keys. |
Underlined data structure | It uses the array as an underlined data structure. | It uses the HashTable as an underlined data structure. | It uses the array as an underlined data structure. |
Iterator | Iterator used in IdentityHashMap is Fail-fast. | Iterator used in WeakHashMap is Fail-fast. | Iterator used in EnumMap is weakly consistent. |
Null Values | It allows to store null values. | It allows to store null values. | It doesn’t allow to store null values |