EnumMap class in Java
EnumMap is a specialized implementation of the Map interface for enumeration types. It extends AbstractMap and implements the Map interface in Java. Few important features of EnumMap are as follows:
- EnumMap class is a member of the Java Collections Framework & is not synchronized.
- EnumMap is ordered collection and they are maintained in the natural order of their keys(the natural order of keys means the order on which enum constant are declared inside enum type )
- It’s a high-performance map implementation, much faster than HashMap.
- All keys of each EnumMap instance must be keys of a single enum type.
- EnumMap doesn’t allow null key and throws NullPointerException, at same time null values are permitted.
Declaration:
public class EnumMap<K extends Enum<K>,V>
- K: specifies the keys
- V: specifies values
K must extend Enum, which enforces the requirement that the keys must be of the specified enum type.
Constructors in EnumMap:
- EnumMap(Class key type): The constructor is used to create an empty EnumMap with the specified keyType.
- EnumMap(EnumMap m): The constructor is used to create an enum map with the same key type as the specified enum map, with initial mappings being the same as the enum map.
- EnumMap(Map m): The constructor is used to create an enum map with initialization from the specified map in the parameter.
Example:
filter_none
edit
play_arrow
brightness_4
|
Output:
Size of EnumMap in java: 4 EnumMap: {CODE=Start Coding with gfg, CONTRIBUTE=Contribute for others, QUIZ=Practice Quizes, MCQ=Test Speed with Mcqs} Key : CODE Value: Start Coding with gfg Does gfgMap has CONTRIBUTE: true Does gfgMap has :QUIZ : true Does gfgMap has :QUIZ : false
Methods in EnumMap:
- put(K key, V value): Associates the specified value with the specified key in this map.
- put all(M map): Used to copy one map into another.
- values(): Returns the collection view of the values in the map.
- remove(Object key): Used to remove a specific key from the map.
- clone(): Returns a shallow copy of the map.
- entrySet(): Returns the set view of the mappings.
- clear(): Used to remove all the mappings from the map.
- equals(Object obj): Used to compare one map with another.
- size(): Returns the number of key-value mappings in this map.
- get(Object key): Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
- containsKey(Object key): Returns true if this map contains a mapping for the specified key.
- contains the value(Object value): Returns true if this map maps one or more keys to the specified value.
- keyset(): Returns the set view of the keys/
post a comment