EnumMap in Java
September 13, 2021
On this page we will walk throw Java java.util.EnumMap
tutorial with examples.
1. Java
java.util.EnumMap
is a specialized java.util.Map
implementation whose key is of Enum
type.
2. All keys in
EnumMap
must be of a single Enum
type while creating it.
3. The elements in
EnumMap
are maintained in the natural order of keys.
4. Null keys are not permitted whereas null values are permitted in
EnumMap
.
5. The
EnumMap
is not synchronized. So in multithreading environment, if atleast one thread is trying to modify our EnumMap
, it should be externally synchronized. We can synchronize EnumMap
using Collections.synchronizedMap
method.
Contents
1. EnumMap Constructors
a.EnumMap(Class<K> keyType)
NullPointerException
will be thrown.
Suppose we have following enum.
enum Profile { DEVELOPER, MODULELEAD, TEAMLEAD, MANAGER }
EnumMap<Profile, String> team = new EnumMap<Profile, String>(Profile.class);
EnumMap(EnumMap<K,? extends V> m)
The newly created enum map will have same key type as of specified enum map.
If enum map, m, is null, then
NullPointerException
is thrown.
EnumMap<Profile, String> team1 = new EnumMap<Profile, String>(Profile.class); team1.put(Profile.DEVELOPER, "Mahesh"); EnumMap<Profile, String> team2 = new EnumMap<Profile, String>(team1); team2.put(Profile.MANAGER, "Krishn"); System.out.println(team2);
{DEVELOPER=Mahesh, MANAGER=Krishn}
EnumMap(Map<K,? extends V> m)
The newly created enum map will contain the elements of specified map.
If specified map, m, contains no mapping, then
IllegalArgumentException
is thrown.
If specified map, m, is null, then
NullPointerException
is thrown.
Map<Profile, String> map = new HashMap<Profile, String>(); map.put(Profile.DEVELOPER, "Mahesh"); EnumMap<Profile, String> team = new EnumMap<Profile, String>(map); team.put(Profile.MANAGER, "Krishn"); System.out.println(team);
{DEVELOPER=Mahesh, MANAGER=Krishn}
2. EnumMap Methods
Let us discussEnumMap
methods with examples.
2.1 put
Theput
method associates a specified value to the specified key in this enum map.
V put(K key, V value)
If specified key is new then new element is added and method returns null.
Example: Find an
enum
that we will use as key in our example.
enum Profile { DEVELOPER, MODULELEAD, TEAMLEAD, MANAGER }
put
method.
EnumMap<Profile, String> team = new EnumMap<Profile, String>(Profile.class); team.put(Profile.DEVELOPER, "Mahesh"); team.put(Profile.TEAMLEAD, "Krishn"); System.out.println(team); String oldVal = team.put(Profile.TEAMLEAD, "Shiv"); System.out.println(oldVal); // Krishn System.out.println(team);
{DEVELOPER=Mahesh, TEAMLEAD=Krishn} Krishn {DEVELOPER=Mahesh, TEAMLEAD=Shiv}
2.2 putAll
TheputAll
copies all the mappings of specified map to this enum map. If this enum map has already the keys matching to the keys of specified map, then values for those keys are replaced from specified map in this enum map.
void putAll(Map<? extends K,? extends V> m)
NullPointerException
will be thrown if specified map is null or any of its keys is null.
Example:
EnumMap<Profile, String> team1 = new EnumMap<Profile, String>(Profile.class); Map<Profile, String> map = Map.of( Profile.DEVELOPER, "Indra", Profile.TEAMLEAD, "Ganesh", Profile.MANAGER, "Ram" ); team1.putAll(map); System.out.println(team1); EnumMap<Profile, String> team2 = new EnumMap<Profile, String>(Profile.class); team2.put(Profile.DEVELOPER, "Mahesh"); team2.put(Profile.TEAMLEAD, "Krishn"); System.out.println(team2); team2.putAll(map); System.out.println(team2);
{DEVELOPER=Indra, TEAMLEAD=Ganesh, MANAGER=Ram} {DEVELOPER=Mahesh, TEAMLEAD=Krishn} {DEVELOPER=Indra, TEAMLEAD=Ganesh, MANAGER=Ram}
2.3 keySet and values
a. keySet()Set<K> keySet()
Set
view of the keys contained in this enum map.
2. The returned
Set
obeys the general contract outlined in Map.keySet()
.
3. The iterator of
Set
will return the keys in their natural order.
b. values()
Collection<V> values()
Collection
view of the values contained in this map.
2. The returned collection obeys the general contract outlined in
Map.values()
.
3. The collection iterator will return the values in the order of keys in enum map. It means iterator will return the values in the natural order of keys.
Example:
EnumMap<Profile, String> team = new EnumMap<Profile, String>(Profile.class); team.put(Profile.DEVELOPER, "Mahesh"); team.put(Profile.MODULELEAD, "Krishn"); team.put(Profile.TEAMLEAD, "Shiv"); team.put(Profile.MANAGER, "Vishnu"); System.out.println("--Using keySet()--"); Set<Profile> profileSet = team.keySet(); profileSet.forEach(s -> System.out.println(s)); System.out.println("--Using values()--"); Collection<String> teamMembers = team.values(); teamMembers.forEach(s -> System.out.println(s));
--Using keySet()-- DEVELOPER MODULELEAD TEAMLEAD MANAGER --Using values()-- Mahesh Krishn Shiv Vishnu
2.4 entrySet
1. TheentrySet
returns a Set
view of the mappings of this enum map.
2. The returned set obeys the general contract outlined in
Map.keySet()
.
3. The iterator will return the mappings in natural order of keys in enum map.
Set<Map.Entry<K,V>> entrySet()
EnumMap<Profile, String> team = new EnumMap<Profile, String>(Profile.class); team.put(Profile.DEVELOPER, "Mahesh"); team.put(Profile.MODULELEAD, "Krishn"); team.put(Profile.TEAMLEAD, "Shiv"); team.put(Profile.MANAGER, "Vishnu"); Set<Entry<Profile, String>> teamSet = team.entrySet(); teamSet.iterator() .forEachRemaining(e -> System.out.println(e.getKey() + ", " + e.getValue()));
DEVELOPER, Mahesh MODULELEAD, Krishn TEAMLEAD, Shiv MANAGER, Vishnu
2.5 containsKey and containsValue
a.boolean containsKey(Object key)
b.
boolean containsValue(Object value)
Example:
EnumMap<Profile, String> team = new EnumMap<Profile, String>(Profile.class); team.put(Profile.DEVELOPER, "Mahesh"); team.put(Profile.MODULELEAD, "Krishn"); team.put(Profile.TEAMLEAD, "Shiv"); boolean isKeyAvailable = team.containsKey(Profile.MODULELEAD); System.out.println("isKeyAvailable: " + isKeyAvailable); boolean isValueAvailable = team.containsValue("Shiv"); System.out.println("isValueAvailable: " + isValueAvailable);
isKeyAvailable: true isValueAvailable: true
2.6 remove and clear
a.V remove(Object key)
The
remove
method returns the previous value associated with the specified key and returns null if specified key is not present.
b.
void clear()
Example:
EnumMap<Profile, String> team = new EnumMap<Profile, String>(Profile.class); team.put(Profile.DEVELOPER, "Mahesh"); team.put(Profile.MODULELEAD, "Krishn"); team.put(Profile.TEAMLEAD, "Shiv"); String previousVal = team.remove(Profile.MODULELEAD); System.out.println("Previous value: " + previousVal); System.out.println(team); team.clear(); System.out.println("After clear(): " + team);
Previous value: Krishn {DEVELOPER=Mahesh, TEAMLEAD=Shiv} After clear(): {}
2.7 get
Theget
method returns the value associated with specified key.
V get(Object key)
EnumMap<Profile, String> team = new EnumMap<Profile, String>(Profile.class); team.put(Profile.DEVELOPER, "Mahesh"); team.put(Profile.MODULELEAD, "Krishn"); String name = team.remove(Profile.DEVELOPER); System.out.println("Name: " + name);
Name: Mahesh
2.8 size
Thesize
method returns the number of key-value mappings in this enum map.
int size()
EnumMap<Profile, String> team = new EnumMap<Profile, String>(Profile.class); team.put(Profile.DEVELOPER, "Mahesh"); team.put(Profile.MODULELEAD, "Krishn"); int size = team.size(); System.out.println("Size: " + size);
Size: 2
2.9 clone
Theclone
method returns a shallow copy of this enum map.
EnumMap<K,V> clone()
EnumMap<Profile, String> team = new EnumMap<Profile, String>(Profile.class); team.put(Profile.DEVELOPER, "Mahesh"); team.put(Profile.MODULELEAD, "Krishn"); EnumMap<Profile, String> cloneTeam = team.clone(); System.out.println(cloneTeam);
{DEVELOPER=Mahesh, MODULELEAD=Krishn}
3. Synchronizing EnumMap
TheEnumMap
is not synchronized. In multithreading environment, if any thread is modifying our enum map, then it must be synchronized externally. We can synchronize enum map as following.
EnumMap<Profile, String> team = new EnumMap<Profile, String>(Profile.class); Map<Profile, String> teamMap = Collections.synchronizedMap(team);