Java EnumMap is a Java Map that uses keys of a single enum type. EnumMap utilizes the inherent associative nature of arrays and is therefore the most efficient implementation of Map. In a nutshell, EnumMap is a one-trick-pony designed to be used when the keys of your Map are constants and therefore can be made static and final.
Constructors of EnumMap
Unlike EnumSet which is abstract, EnumMap is a concrete class with three constructors. Two of the constructors follow from the special property of Maps described in the lesson on Java Map interface. The third constructor of EnumMap takes a class object to identify the enum whose constants will be used as a key. Note that a EnumMap does not have a no-args constructor because it does not make sense to create an EnumMap without specifying enum type.
- EnumMap(Class<K> keyType) -> Most frequently used constructor. Creates an empty enum map with the specified key type.
- EnumMap(EnumMap<K,? extends V> m) -> The copy constructor, copies all entries from one enum to another.
- EnumMap(Map<K,? extends V> m) -> Creates an enum map initialized from the specified map.
Salient Features of EnumMap
There are a number of distinguishing features of EnumMap. EnumMap –
- Has a range of the universe of keys. Certain optimizations are possible since the number of keys in EnumMap is finite.
- Is an ordered map. The entries are stored in their ‘natural’ order, meaning they are stored in the order the keys are declared in the enum.
- Does not allow null keys. You can have null values.
- Has fail-fast iterators. This implies that under no circumstances will ConcurrentModificationException be thrown. HashMap, TreeMap and LinkedHashMap have weakly consistent iterators.
- Is not thread safe as none of the methods are synchronized. If you need to use EnumMap in a multithreaded environment you have to use external locking constructs.
- Has much better performance than even HashMap, but can only be used if the keys are of the same enum type.
EnumMap Example
In the example below we consider a situation where we need to store and later display information about major social networks. Since the number of popular social networks is finite and each network is an entity in itself, we can model social networks as an enum.
1 2 3 4 5 6 7 8 | // this enum captures some of the popular // social networks. enum SOCIALMEDIA { TWITTER, FACEBOOK, GOOGLEPLUS, LINKEDIN; } |
Next, we put String descriptions for each social network in the EnumMap and later iterate. The complete code is shown below –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import java.util.Map; import java.util.EnumMap; import java.util.Iterator; public class EnumMapDemo { // this enum captures some of the popular // social networks. enum SOCIALMEDIA { TWITTER, FACEBOOK, GOOGLEPLUS, LINKEDIN; } ; public static void main(String... codingraptor) { // Let's create a map to store a string // description of the most important feature // of each social network. Map<SOCIALMEDIA, String> socialNetworkUses; // Initialize the enum map socialNetworkUses = new EnumMap<SOCIALMEDIA, String>(SOCIALMEDIA.class); // add some entries socialNetworkUses.put(SOCIALMEDIA.TWITTER, "The most powerful social network."); socialNetworkUses.put(SOCIALMEDIA.FACEBOOK, "The let's disturb others anti-social network."); socialNetworkUses.put(SOCIALMEDIA.GOOGLEPLUS, "The also-ran social network."); socialNetworkUses.put(SOCIALMEDIA.LINKEDIN, "Ok, let's get serious social network."); // Let's iterate and see the entries // get printed in the order they were declared in enum SOCIALMEDIA for(Map.Entry<SOCIALMEDIA, String> anEntry : socialNetworkUses.entrySet()) { System.out.println("The network " + anEntry.getKey() + " is described as " + anEntry.getValue()); } } } |
The output of the above program is shown below –
1 2 3 4 | The network TWITTER is described as The most powerful social network. The network FACEBOOK is described as The let's disturb others anti-social network. The network GOOGLEPLUS is described as The also-ran social network. The network LINKEDIN is described as Ok, let's get serious social network. |
Having looked at HashMap, TreeMap and EnumMap in this article we will now move on to examine LinkedHashMap.