EnumSet and EnumMap
EnumSet
EnumSet is a Set implementation specifically designed for use with enum types. It is a highly optimized collection that is more efficient than HashSet when dealing with enums.
Features of EnumSet
EnumSetSpecialized for Enums: It can only store enum values.
Efficient: It uses bit vectors internally, making it much faster and more memory efficient than
HashSetwhen working with enums.No Null Values: It does not allow
nullelements. Attempting to addnullwill throw aNullPointerException.Order: The order of the enum constants in the
EnumSetis determined by their natural order (the order in which the constants are declared in the enum class). We can also use a specific ordering by providing a comparator.
import java.util.EnumSet;
public enum LogLevel {
ERROR, WARNING, INFO, DEBUG;
}
public class EnumSetExample {
public static void main(String[] args) {
// Create an EnumSet containing some enum constants
EnumSet<LogLevel> logLevels = EnumSet.of(LogLevel.ERROR, LogLevel.WARNING);
// Print the EnumSet
System.out.println("Log levels: " + logLevels);
// Add more values
logLevels.add(LogLevel.INFO);
System.out.println("After adding INFO: " + logLevels);
// Remove a value
logLevels.remove(LogLevel.WARNING);
System.out.println("After removing WARNING: " + logLevels);
// Create an EnumSet with all values
EnumSet<LogLevel> allLogLevels = EnumSet.allOf(LogLevel.class);
System.out.println("All log levels: " + allLogLevels);
}
}Log levels: [ERROR, WARNING]
After adding INFO: [ERROR, WARNING, INFO]
After removing WARNING: [ERROR, INFO]
All log levels: [ERROR, WARNING, INFO, DEBUG]Methods in EnumSet:
EnumSet:EnumSet.of(E... elements): Creates anEnumSetwith the specified enum constants.EnumSet.allOf(Class<E> elementType): Creates anEnumSetcontaining all constants of the specified enum class.EnumSet.noneOf(Class<E> elementType): Creates an emptyEnumSet.EnumSet.copyOf(Collection<? extends E> c): Creates anEnumSetcontaining all of the elements in the specified collection that are also enum constants of the same type.
EnumMap
EnumMap is a Map implementation that uses enum constants as keys. It is optimized for use with enums as keys and provides superior performance over regular HashMap when the keys are enums.
Features of EnumMap:
EnumMap:Specialized for Enums: The keys in an
EnumMapmust be enum constants.Efficient: It is faster and uses less memory than a
HashMapwhen the keys are enums, because it internally stores the mappings in an array, making lookups and insertions much faster.Ordering: The map is ordered based on the natural order of the enum constants (the order in which the constants are defined).
Null Keys Not Allowed: It does not allow
nullkeys. However,nullvalues are allowed.
import java.util.EnumMap;
public enum LogLevel {
ERROR, WARNING, INFO, DEBUG;
}
public class EnumMapExample {
public static void main(String[] args) {
// Create an EnumMap where the key is LogLevel, and the value is a description
EnumMap<LogLevel, String> logLevelDescriptions = new EnumMap<>(LogLevel.class);
// Add entries to the EnumMap
logLevelDescriptions.put(LogLevel.ERROR, "Critical error");
logLevelDescriptions.put(LogLevel.WARNING, "Warning message");
logLevelDescriptions.put(LogLevel.INFO, "Informational message");
// Print the EnumMap
System.out.println("Log level descriptions: " + logLevelDescriptions);
// Retrieve a value
System.out.println("INFO level description: " + logLevelDescriptions.get(LogLevel.INFO));
}
}Log level descriptions: {ERROR=Critical error, WARNING=Warning message, INFO=Informational message}
INFO level description: Informational messageMethods in EnumMap:
EnumMap:EnumMap(Class<K> keyType): Creates an emptyEnumMapwith the specified enum type as the key.EnumMap.put(K key, V value): Adds a key-value pair to the map.EnumMap.get(Object key): Retrieves the value associated with the given key.EnumMap.remove(Object key): Removes the entry for the specified key.
Comparison of EnumSet vs EnumMap:
EnumSet vs EnumMap:Feature
EnumSet
EnumMap
Type
A Set of enum values
A Map where keys are enum values
Null Handling
Does not allow null elements
Does not allow null keys, but allows null values
Usage
Used when you need a collection of enum values without duplicates
Used when you need to associate values with enum constants
Efficiency
More memory-efficient and faster than HashSet
More memory-efficient and faster than HashMap for enums as keys
Ordering
Follows the natural order of enum constants
Follows the natural order of enum constants
Last updated