Java Collections – EnumMap Example

A specialized Map implementation for use with enum type keys. All of the keys in an EnumMap must come from a single enum type that is specified when the map is created. EnumMaps are represented internally as arrays. It is for this reason that they are extremely compact and efficient. In my example, you can see the Enums in the Protocol class detailed below.

Big-O Notation

According to the Javadocs, this implementation provides constant-time O(1) performance for the basic operations (get and put).

Creating an EnumMap

EnumMap<Protocol, String> internetProtocols = 
         new EnumMap<Protocol, String>(Protocol.class);

Adding elements

Adding elements to the internetProtocols EnumMap is done by using the put(Protocol key, String value) method. The key of the EnumMap will be the Protocol class enum value and the value will be an String object.

internetProtocols.put(Protocol.TCP, "Transmission Control Protocol");
internetProtocols.put(Protocol.UDP, "User Datagram Protocol");
internetProtocols.put(Protocol.ICMP, "Internet Control Message Protocol");
...

Removing elements

Removes the mapping for this key from this map, if present. Removing elements is just a matter of calling the remove(Object key) method.

// Remove old and relatively unused protocol
internetProtocols.remove(Protocol.GOPHER);

Size of Collection

Returning the number of elements in a EnumMap is as easy as calling the size() method.

internetProtocols.size();

Full Program Listing (EnumMapExample.java)

package com.avaldes.tutorials;

import java.util.EnumMap;

public class EnumMapExample {

  public static void main(String[] args) {
    // Instantiate the EnumMap of Internet Protocols
    EnumMap<Protocol, String> internetProtocols = 
                              new EnumMap<Protocol, String>(Protocol.class);
      
    System.out.println("Setting up the EnumMap of Internet Protocols...");
    
    // Set up the EnumMap and add in the descriptions into the map
    internetProtocols.put(Protocol.TCP, "Transmission Control Protocol");
    internetProtocols.put(Protocol.UDP, "User Datagram Protocol");
    internetProtocols.put(Protocol.ICMP, "Internet Control Message Protocol");
    internetProtocols.put(Protocol.HTTP, "Hypertext Transfer Protocol");
    internetProtocols.put(Protocol.FTP, "File Transmission Protocol");
    internetProtocols.put(Protocol.SSH, "Secure Shell Protocol");
    internetProtocols.put(Protocol.SMTP, "Simple Mail Transfer Protocol");
    internetProtocols.put(Protocol.SNMP, "Simple Network Management Protocol");
    internetProtocols.put(Protocol.POP3, "Post Office Protocol");
    internetProtocols.put(Protocol.IRC, "Internet Relay Chat");
    internetProtocols.put(Protocol.TELNET, "Telnet Protocol");
    internetProtocols.put(Protocol.GOPHER, "Gopher Protocol");
    
    // Remove old and relatively unused protocol
    internetProtocols.remove(Protocol.GOPHER);

    System.out.println("Displaying the list of Common Internet Protocols");
    
    // Let's iterate over the entire EnumMap 
    for (Protocol p : internetProtocols.keySet()) {
      // Format the output nicely
      System.out.println("Internet Protocol => " + p.name() 
                         + ", Description => " + internetProtocols.get(p));
    }
  }
}

Full Program Listing (Protocol.java)

package com.avaldes.tutorials;

public enum Protocol {
	TCP, 
	UDP, 
	ICMP,
	HTTP, 
	FTP, 
	SSH,
	SMTP, 
	SNMP, 
	POP3,
	GOPHER, 
	TELNET, 
	IRC
}

Output

Setting up the EnumMap of Internet Protocols...
Displaying the list of Common Internet Protocols

Internet Protocol => TCP, Description => Transmission Control Protocol
Internet Protocol => UDP, Description => User Datagram Protocol
Internet Protocol => ICMP, Description => Internet Control Message Protocol
Internet Protocol => HTTP, Description => Hypertext Transfer Protocol
Internet Protocol => FTP, Description => File Transmission Protocol
Internet Protocol => SSH, Description => Secure Shell Protocol
Internet Protocol => SMTP, Description => Simple Mail Transfer Protocol
Internet Protocol => SNMP, Description => Simple Network Management Protocol
Internet Protocol => POP3, Description => Post Office Protocol
Internet Protocol => TELNET, Description => Telnet Protocol
Internet Protocol => IRC, Description => Internet Relay Chat

Other Related Posts

Map Examples

List Examples

Set Examples

Please Share Us on Social Media

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

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