![]() |
VOOZH | about |
EnumMap is a specialized implementation of the Map interface for enumeration types. It extends AbstractMap and implements the Map interface in Java. It belongs to java.util package. A few important features of EnumMap are as follows:
Example: This example demonstrates adding elements to an EnumMap and printing them.
MONDAY: Start of the week FRIDAY: End of the week SUNDAY: Weekend
In Java, the declaration of EnumMap can be done as:
EnumMap<EnumType, ValueType> map = new EnumMap<>(EnumType.class);
Note: EnumType is the type of the enum that will be used as keys in the map and ValueType is the type of the values associated with the enum keys.
The EnumMap class in Java is a specialized map implementation that is designed specifically for use with enum keys. An EnumMap is a compact, efficient, and fast alternative to using a HashMap with enum keys.
Example: This example demonstrates how to use an EnumMap in Java to store and retrieve values associated with enum keys.
Work Relax
Explanation: In this example, we define an enum type Days that represents the days of the week. We then create an EnumMap and add elements to it using the put method. Finally, we retrieve elements from the EnumMap using the get method and print the results to the console.
Constructor | Description |
|---|---|
EnumMap(Class keyType) | This constructor is used to create an empty EnumMap with the specified keyType. |
EnumMap(EnumMap m) | This constructor is used to create an enum map with the same keyType as the specified enum map, with initial mappings being the same as the EnumMap |
EnumMap(Map m) | This constructor is used to create an enum map with initialization from the specified map in the parameter. |
Example: This example demonstrates how to create, perform basic operation such as size, retrieval and check for keys and values on an EnumMap.
Output:
1. Adding Elements: We can use put() and putAll() method to insert elements to a EnumMap.
Example: This example demonstrates how to add elements to an EnumMap using put() and putAll() methods.
EnumMap colors1: {RED=1, GREEN=2}
EnumMap colors2: {RED=1, GREEN=2, BLUE=3}
2. Accessing Elements: We can use entrySet(), keySet(), values() and get() to access the elements of EnumMap.
Example: This example demonstrates how to access elements of an EnumMap using methods like entrySet(), keySet(), values() and get().
EnumMap: {RED=1, GREEN=2, BLUE=3, WHITE=4}
Key-Value mappings: [RED=1, GREEN=2, BLUE=3, WHITE=4]
Keys: [RED, GREEN, BLUE, WHITE]
Values: [1, 2, 3, 4]
Value of RED : 1
3. Removing Elements: We can use the remove() method to remove elements from the EnumMap.
Example: This example demonstrates how to remove elements from an EnumMap using remove() and the conditional remove(key,value) method.
EnumMap e : {RED=1, GREEN=2, BLUE=3, WHITE=4}
Removed Value: 4
Is the entry {RED=1} removed? true
Updated EnumMap: {GREEN=2, BLUE=3}
4. Replacing Elements: Map interface provides three variations of the replace() method to change the mappings of EnumMap.
Example: This example demonstrates how to replace elements in an EnumMap using replace(), conditional replace(key, oldValue, newValue), and replaceAll() method.
EnumMap e {RED=1, GREEN=2, BLUE=3, WHITE=4}
EnumMap using replace(): {RED=11, GREEN=12, BLUE=3, WHITE=4}
EnumMap using replaceAll(): {RED=14, GREEN=15, BLUE=6, WHITE=7}
The implementation of an EnumMap is not synchronized. This means that if multiple threads access a tree set concurrently, and at least one of the threads modifies the set, it must be synchronized externally. This is typically accomplished by using the synchronizedMap() method of the Collections class. This is best done at the creation time, to prevent accidental unsynchronized access.
Map<EnumKey, V> m = Collections.synchronizedMap(new EnumMap<EnumKey, V>(...));
Method | Action Performed |
|---|---|
| clear() | Removes all mappings from this map. |
| clone() | Returns a shallow copy of this enum map. |
| containsKey?(Object key) | Returns true if this map contains a mapping for the specified key. |
| containsValue?(Object value) | Returns true if this map maps one or more keys to the specified value. |
| entrySet() | Returns a Set view of the mappings contained in this map. |
| equals?(Object o) | Compares the specified object with this map for equality. |
| get?(Object key) | Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key. |
| hashCode() | Returns the hash code value for this map. |
| keySet() | Returns a Set view of the keys contained in this map. |
| put?(K key, V value) | Associates the specified value with the specified key in this map. |
| putAll?(Map<? extends K,?? extends V> m) | Copies all of the mappings from the specified map to this map. |
| remove?(Object key) | Removes the mapping for this key from this map if present. |
| size() | Returns the number of key-value mappings in this map. |
| values() | Returns a Collection view of the values contained in this map. |
Method | Description |
|---|---|
| isEmpty() | Returns true if this map contains no key-value mappings. |
| toString() | Returns a string representation of this map. |
Method | Descriptionenter |
|---|---|
| compute?(K key, BiFunction<? super K,?? super V,?? extends V> remappingFunction) | Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping). |
| computeIfAbsent?(K key, Function<? super K,?? extends V> mappingFunction) | If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null. |
| computeIfPresent?(K key, BiFunction<? super K,?? super V,?? extends V> remappingFunction) | If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value. |
| forEach?(BiConsumer<? super K,?? super V> action) | Performs the given action for each entry in this map until all entries have been processed or the action throws an exception. |
| getOrDefault?(Object key, V defaultValue) | Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key. |
| merge?(K key, V value, BiFunction<? super V,?? super V,?? extends V> remappingFunction) | If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value. |
| putIfAbsent?(K key, V value) | If the specified key is not already associated with a value (or is mapped to null) associates it with the given value and returns null, else returns the current value. |
| remove?(Object key, Object value) | Removes the entry for the specified key only if it is currently mapped to the specified value. |
| replace?(K key, V value) | Replaces the entry for the specified key only if it is currently mapped to some value. |
| replace?(K key, V oldValue, V newValue) | Replaces the entry for the specified key only if currently mapped to the specified value. |
| replaceAll?(BiFunction<? super K,?? super V,?? extends V> function) | Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception. |
Property | EnumMap | EnumSet |
|---|---|---|
| Internal Representation | EnumMap is internally represented as arrays. The representation is compact and efficient. | EnumSet is internally represented as BitVector or sequence of bits. |
| Permits Null Elements? | Null keys are not allowed but Null values are allowed. | Null elements are not permitted. |
| Is the Abstract Class? | No | Yes |
| Instantiation | Since EnumMap is not an abstract class, it can be instantiated using the new operator. | It is an abstract class, it does not have a constructor. Enum set is created using its predefined methods like allOf(), noneOf(), of(), etc. |
| Implementation | EnumMap is a specialized Map implementation for use with enum type keys. | EnumSet is a specialized Set implementation for use with enum types. |