Published in: Java
EnumMap protects you from mis-ordering when initializing values, reordering in the enumerated type, and just about any other strange situation that can arise from more than one person working on code at the same time.
public enum AntStatus { INITIALIZING, COMPILING, COPYING, JARRING, ZIPPING, DONE, ERROR } // Create a map with the key and a String message EnumMap<AntStatus, String> antMessages = new EnumMap<AntStatus, String>(AntStatus.class); // Initialize the map antMessages.put(AntStatus.INITIALIZING, "Initializing Ant..."); antMessages.put(AntStatus.COMPILING, "Compiling Java classes..."); antMessages.put(AntStatus.COPYING, "Copying files..."); antMessages.put(AntStatus.JARRING, "JARring up files..."); antMessages.put(AntStatus.ZIPPING, "ZIPping up files..."); antMessages.put(AntStatus.DONE, "Build complete."); antMessages.put(AntStatus.ERROR, "Error occurred."); // Iterate and print messages for (AntStatus status : AntStatus.values( ) ) { out.println("For status " + status + ", message is: " + antMessages.get(status)); } }
You need to login to post a comment.
