Criteria | EnumMap | HashMap |
---|---|---|
Key Type | Only accepts keys of a single enum type. | Can accept any object as a key. |
Performance | Typically faster than HashMap for enum keys due to optimized array-based implementation. | General-purpose performance, based on a hash table. |
Ordering | Maintains keys in their natural order (i.e., the order in which enum constants are declared). | Does not guarantee any specific order. |
Null Keys/Values | Does not support null keys but allows null values. | Allows one null key and multiple null values. |
Thread Safety | No | No |
Memory Footprint | Typically consumes less memory than HashMap when used with enum keys due to its array-based implementation. | More general-purpose and potentially uses more memory, especially with a larger initial capacity or load factor. |
Use-case | Best suited for key types that are enums, offering high performance and reduced memory footprint. | General-purpose map implementation suitable for any key type. |
Internal Implementation | Array-based. It uses an array of the enum type’s ordinal values. | Hash table. Uses an array of nodes (linked list) which can turn into a balanced tree in case of high hash collisions. |
Example: Difference Between EnumMap and HashMap in Java
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;
public class EnumMapVsHashMapDemo {
// Define an example enum for demonstration
public enum Colors {
RED, GREEN, BLUE
}
public static void main(String[] args) {
// Using an EnumMap
Map<Colors, String> colorEnumMap = new EnumMap<>(Colors.class);
colorEnumMap.put(Colors.RED, "Hex #FF0000");
colorEnumMap.put(Colors.GREEN, "Hex #00FF00");
colorEnumMap.put(Colors.BLUE, "Hex #0000FF");
// Using a HashMap
Map<Colors, String> colorHashMap = new HashMap<>();
colorHashMap.put(Colors.RED, "Hex #FF0000");
colorHashMap.put(Colors.GREEN, "Hex #00FF00");
colorHashMap.put(Colors.BLUE, "Hex #0000FF");
System.out.println("EnumMap contents: " + colorEnumMap);
System.out.println("HashMap contents: " + colorHashMap);
}
}
Output:
EnumMap contents: {RED=Hex #FF0000, GREEN=Hex #00FF00, BLUE=Hex #0000FF} HashMap contents: {RED=Hex #FF0000, GREEN=Hex #00FF00, BLUE=Hex #0000FF}
Explanation:
1. EnumMap: It is a specialized map implementation for use with enum type keys. EnumMap is represented internally as an array. All basic operations execute in constant time. It is extremely compact and efficient.
2. HashMap: It is a general-purpose map implementation that uses a hash table to store the key-value pairs. It allows any type of key, whereas EnumMap is strictly for enum keys.
3. In the provided example:
– For EnumMap: We first created an empty EnumMap for the Colors enum, then added color-to-hex mappings. The printed order is the natural order of the enum constants.
– For HashMap: We added color-to-hex mappings in the same way. The printed order is not guaranteed, but in this small example, it might appear the same as the insertion order.
4. When to use:
– Use EnumMap when the keys are of enum type and you want an efficient map implementation.
– Use HashMap for a general-purpose map implementation when keys can be of various types.
The main difference between EnumMap and HashMap is the type of keys they support and their internal implementation. For operations on enum keys, EnumMap is typically faster and more memory-efficient than HashMap.