Java IdentityHashMap、WeakHashMap和EnumMap之间的区别

Java IdentityHashMap、WeakHashMap和EnumMap之间的区别

IdentityHashMap、WeakHashMap和EnumMap都是java集合中实现Map接口的类。但是它们之间存在着一些区别。

1. 以期在迭代时进行修改。

IdentityHashMap的工作。

// 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<Integer, String> ihmap
            = new IdentityHashMap<Integer, String>();
  
        // 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<IdentityHashMap.Entry<Integer, String> >
            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<Integer, String> entry = itr.next();
            
            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=4}
Key = 10, Value = Geeks
Key = 40, Value = Welcomes
Key = 50, Value = You
Key = 30, Value = Geeks
Key = 20, Value = 4

2. WeakHashMap是Map接口的一个实现,它只存储弱键。在WeakHashMap中,我们可以只存储其键的弱引用,这使得键值对在其键不再被正常使用时可以被垃圾回收。WeakHashMap是基于HashTable的实现,但它不是同步的。它允许你同时存储空键和空值。

WeakHashMap的工作。

// 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<Number, String> whmap = new WeakHashMap<Number, String>();
        
        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是Map接口的一个专门实现,用于枚举类型。它扩展了AbstractMap并实现了Java中的Map接口。EnumMap的几个重要特征如下。

  • EnumMap类是Java集合框架的一个成员,它不是同步的。
  • EnumMap是一个有序的集合,它们按照键的自然顺序被维护(键的自然顺序是指枚举常量在枚举类型中的声明顺序)。
  • EnumMap比HashMap快很多。
  • 每个EnumMap实例的所有键必须是同一枚举类型的键。
  • EnumMap不允许插入空键,如果我们试图插入空键,它将抛出NullPointerException。
  • EnumMap在内部被表示为数组,因此它能提供更好的性能。

EnumMap的工作。

// 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<Months, Integer> 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 |
| :————- | :——————————————- | :—————————————– | :————————————— |
搜索并获得价值 | 它使用平等运算符(==)来搜索和获取数值。 | 它使用equals()方法来实现这一目的。 | 为此,它还使用了equals()方法。
Keys | 它允许存储任何类型的钥匙。 | 它还允许存储任何类型的钥匙。 | 它只允许存储枚举类型的键。
下划线的数据结构 | 它使用数组作为下划线数据结构。 | 它使用HashTable作为下划线数据结构。 | 它使用数组作为下划线数据结构。
Iterat或者 | 在IdentityHashMap中使用的迭代器是失效快速的。 | WeakHashMap中使用的迭代器是快速的。 | EnumMap中使用的迭代器是弱一致性的。
空值 | 它允许存储空值。 | 它允许存储空值。 | 它不允许存储空值

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程