Java EnumMap class

Java EnumMap class is the specialized Map implementation for enum keys. It inherits Enum and AbstractMap classes.

EnumMap class hierarchy

The hierarchy of EnumMap class is given in the figure given below.

EnumMap class hierarchy

EnumMap class declaration

Let's see the declaration for java.util.EnumMap class.

EnumMap class Parameters

Let's see the Parameters for java.util.EnumMap class.

  • K: It is the type of keys maintained by this map.
  • V: It is the type of mapped values.

Constructors of Java EnumMap class

ConstructorDescription
EnumMap(Class<K> keyType)It is used to create an empty enum map with the specified key type.
EnumMap(EnumMap<K,? extends V> m)It is used to create an enum map with the same key type as the specified enum map.
EnumMap(Map<K,? extends V> m)It is used to create an enum map initialized from the specified map.

Methods of Java EnumMap class

SNMethodDescription
1clear()It is used to clear all the mapping from the map.
2clone()It is used to copy the mapped value of one map to another map.
3containsKey()It is used to check whether a specified key is present in this map or not.
4containsValue()It is used to check whether one or more key is associated with a given value or not.
5entrySet()It is used to create a set of elements contained in the EnumMap.
6equals()It is used to compare two maps for equality.
7get()It is used to get the mapped value of the specified key.
8hashCode()It is used to get the hashcode value of the EnumMap.
9keySet()It is used to get the set view of the keys contained in the map.
10size()It is used to get the size of the EnumMap.
11Values()It is used to create a collection view of the values contained in this map.
12put()It is used to associate the given value with the given key in this EnumMap.
13putAll()It is used to copy all the mappings from one EnumMap to a new EnumMap.
14remove()It is used to remove the mapping for the given key from EnumMap if the given key is present.

Java EnumMap Example

Output:

Monday 1
Tuesday 2
Wednesday 3
Thursday 4

Java EnumMap Example: Book

Output:

101 Let us C Yashwant Kanetkar BPB 8
102 Data Communications & Networking Forouzan Mc Graw Hill 4
103 Operating System Galvin Wiley 6




Latest Courses