EnumMap in Java

EnumMap in Java

EnumMap is specialized Map implementation designed and optimized for using Java Enum as key. EnumMap in Java is added on JDK  5 release. There are some similarities as well as differences between EnumMap and HashMap which you can find hereFollowings are the features of EnumMap.

  • All of the keys in an enum map must come from a single enum type that is specified at time of creation of EnumMap.
  • Enum maps are maintained in the natural order of their keys i.e the order in which the enum constants are declared.
  • Unlike HashMap, iterators of EnumMap are fail-save. they will never throw ConcurrentModificationException and they may or may not show the effects of any modifications to the map that occur while the iteration is in progress.
  • Null keys are not permitted, EnumMap throws NullPointerException while trying to enter null as key. However null values are permitted in EnumMap.
  • EnumMap is not synchronized. It should synchronize manually in case you want to use it in multithreading environment. We can use Collections.synchronizedMap() method to synchronize EnumMap.
  • Performance of EnumMap is better than their counter part HashMap.

EnumMap provides following constructors.

[table “” not found /]

Example of EnumMap

package com.code2succeed.collection;

import java.util.EnumMap;
import java.util.Iterator;

public class EnumMapExample {

    public enum UserStatus {
        ACTIVE,
        INACTIVE,
        PENDING,
        DELETED;
    }

    public static void main(String[] args) {
        EnumMap < UserStatus, String > enumMap = new EnumMap < UserStatus, String > (UserStatus.class);
        enumMap.put(UserStatus.ACTIVE, "User status is active");
        enumMap.put(UserStatus.INACTIVE, "User status is inactive");
        enumMap.put(UserStatus.PENDING, "User status is pending");
        enumMap.put(UserStatus.DELETED, "User status is deleted");

        //iterate enumMap
        Iterator < UserStatus > enumKeySet = enumMap.keySet().iterator();
        while (enumKeySet.hasNext()) {
            UserStatus userStatus = enumKeySet.next();
            System.out.println(userStatus + " : " + enumMap.get(userStatus));
        }

        //check whether a key is available or not
        System.out.println("Check if null available as key : " + enumMap.containsKey(null));
        System.out.println("Check if ACTIVE available as key : " + enumMap.containsKey(UserStatus.ACTIVE));

        //remove a key
        enumMap.remove(UserStatus.DELETED);
        System.out.println("Content of enum map after removing userStatus DELETED");
        enumKeySet = enumMap.keySet().iterator();
        while (enumKeySet.hasNext()) {
            UserStatus userStatus = enumKeySet.next();
            System.out.println(userStatus + " : " + enumMap.get(userStatus));
        }
    }
}

Output

ACTIVE : User status is active
INACTIVE : User status is inactive
PENDING : User status is pending
DELETED : User status is deleted

Check if null available as key : false
Check if ACTIVE available as key : true

Content of enum map after removing userStatus DELETED
ACTIVE : User status is active
INACTIVE : User status is inactive
PENDING : User status is pending

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *