📜  EnumMap 和 HashMap 的区别

📅  最后修改于: 2021-09-13 02:15:42             🧑  作者: Mango

EnumMap 和 HashMap 都是实现 Map 接口的类。但是它们之间存在一些差异。所以我们试图列出 EnumMap 和 HashMap 之间的区别。

1. EnumMap的:EnumMap的是一个专门的Map接口枚举类型。它扩展了AbstractMap在Java实现了Map接口。 EnumMap 的几个要点如下:

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

枚举映射演示:

Java
// Java program to illustrate working
// of EnumMap
  
import java.util.*;
  
class EnumMapExample {
  
    public enum Days {
        Sunday,
        Monday,
        Tuesday,
        Wendensday;
    }
  
    public static void main(String[] args)
    {
        // Creating an EnumMap of the Days enum
        EnumMap enumMap
            = new EnumMap<>(Days.class);
  
        // Insert using put() method
        enumMap.put(Days.Sunday, 1);
        enumMap.put(Days.Monday, 2);
        enumMap.put(Days.Tuesday, 3);
        enumMap.put(Days.Wendensday, 4);
  
        // 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());
        }
    }
}


Java
// Java program to illustrate
// the working of HashMap
  
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // Create an empty hash map
        HashMap map = new HashMap<>();
  
        // Adding elements to the map
        map.put(10, "Geeks");
        map.put(20, "Ram");
        map.put(30, "Shyam");
  
        // Printing size of the map
        System.out.println("Size of map is: " + map.size());
  
        // Iterating the map
        for (Map.Entry m : map.entrySet()) {
            System.out.println(m.getKey() + " "
                               + m.getValue());
        }
    }
}


输出
Size of EnumMap: 4
Sunday 1
Monday 2
Tuesday 3
Wendensday 4

2. HashMap : HashMap是一个实现 Map 接口的类。它将数据存储在键值对中,其中键应该是唯一的。如果您尝试插入重复键,它将替换相应键的元素。它允许存储空键和空值,但应该只有一个空键和多个空值。 Java HashMap 类似于HashTable ,但它是同步的。 HashMap 不维护顺序,这意味着它不保证元素的任何特定顺序。

哈希映射演示:

Java

// Java program to illustrate
// the working of HashMap
  
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // Create an empty hash map
        HashMap map = new HashMap<>();
  
        // Adding elements to the map
        map.put(10, "Geeks");
        map.put(20, "Ram");
        map.put(30, "Shyam");
  
        // Printing size of the map
        System.out.println("Size of map is: " + map.size());
  
        // Iterating the map
        for (Map.Entry m : map.entrySet()) {
            System.out.println(m.getKey() + " "
                               + m.getValue());
        }
    }
}
输出
Size of map is: 3
20 Ram
10 Geeks
30 Shyam

EnumMap 和 HashMap 的区别

                              EnumMap                             HashMap
EnumMap is a specialized map implementation that uses only Enum type key. In HashMap, we can use Enum as well as any other object as a key.
It doesn’t allow storing null key. It allows to store the null keys as well values, but there should be only one null key object and there can be any number of null values.
EnumMap internally uses the array HashMap internally uses the HashTable.
EnumMap is a specialized map implementation that uses only enum type keys. Due to this, EnumMap is performed better than HashMap. Performance of HashMap is slightly less than compared to EnumMap.
EnumMap stores the keys in the natural order of their keys (order in which the enum constant are declared). In HashMap, no ordering of the keys.
Since EnumMap internally uses the array and stores the key in their natural ordering using ordinal(), so there is no probability of collision. HashMap uses the hashCode() to store key and values, so there is probability of collision.