Java EnumMap Example

By Arvind Rai, 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.

1. EnumMap Constructors

a.
EnumMap(Class<K> keyType) 
It creates an empty enum map with the specified key type. If keyType is null, then NullPointerException will be thrown.
Suppose we have following enum.
enum Profile {
  DEVELOPER, MODULELEAD, TEAMLEAD, MANAGER
} 
Use constructor as following.
EnumMap<Profile, String> team = new EnumMap<Profile, String>(Profile.class); 
b.
EnumMap(EnumMap<K,? extends V> m) 
The newly created enum map will contain all the elements of the enum map, m, from which we initialize our enum map.
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); 
Output
{DEVELOPER=Mahesh, MANAGER=Krishn} 
c.
EnumMap(Map<K,? extends V> m) 
The specified map must contain at least one mapping to decide key type of new enum map.
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); 
Output
{DEVELOPER=Mahesh, MANAGER=Krishn} 

2. EnumMap Methods

Let us discuss EnumMap methods with examples.

2.1 put

The put method associates a specified value to the specified key in this enum map.
V put(K key, V value) 
If the enum map has already the specified key then old value is replaced by new one and the old value is returned by the method.
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
} 
Now find the code for 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); 
Output
{DEVELOPER=Mahesh, TEAMLEAD=Krishn}
Krishn
{DEVELOPER=Mahesh, TEAMLEAD=Shiv} 

2.2 putAll

The putAll 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) 
The 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); 
Output
{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() 
1. It returns a 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() 
1. It returns the 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)); 
Output
--Using keySet()--
DEVELOPER
MODULELEAD
TEAMLEAD
MANAGER
--Using values()--
Mahesh
Krishn
Shiv
Vishnu 

2.4 entrySet

1. The entrySet 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() 
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");
Set<Entry<Profile, String>> teamSet = team.entrySet();
teamSet.iterator()
 .forEachRemaining(e -> System.out.println(e.getKey() + ", " + e.getValue())); 
Output
DEVELOPER, Mahesh
MODULELEAD, Krishn
TEAMLEAD, Shiv
MANAGER, Vishnu 

2.5 containsKey and containsValue

a.
boolean containsKey(Object key) 
Returns true if this enum map contains a mapping for the specified key.
b.
boolean containsValue(Object value) 
Returns true if this enum map contains one or more keys associated with the specified 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); 
Output
isKeyAvailable: true
isValueAvailable: true 

2.6 remove and clear

a.
V remove(Object key) 
It removes the mapping for the specified key from this enum map.
The remove method returns the previous value associated with the specified key and returns null if specified key is not present.
b.
void clear() 
It removes all mappings from this enum map.

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); 
Output
Previous value: Krishn
{DEVELOPER=Mahesh, TEAMLEAD=Shiv}
After clear(): {} 

2.7 get

The get method returns the value associated with specified key.
V get(Object key) 
Example:
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); 
Output
Name: Mahesh 

2.8 size

The size method returns the number of key-value mappings in this enum map.
int size() 
Example:
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); 
Output
Size: 2 

2.9 clone

The clone method returns a shallow copy of this enum map.
EnumMap<K,V> clone() 
Example:
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); 
Output
{DEVELOPER=Mahesh, MODULELEAD=Krishn} 

3. Synchronizing EnumMap

The EnumMap 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); 

4. Reference

Class EnumMap
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us