We Recommend

Java How to Program Java How to Program
Takes a new tools-based approach to Web application development that uses Netbeans 5.5 and Java Studio Creator 2 to create and consume Web Services. Features new AJAX-enabled, Web applications built with JavaServer Faces (JSF), Java Studio Creator 2 and the Java Blueprints AJAX Components. Includes new topics throughout, such as JDBC 4, SwingWorker for multithreaded GUIs, GroupLayout, Java Desktop Integration Components (JDIC), and much more.


Posted By

cetnar on 07/15/06


Tagged

tiger enum map


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

xaviaracil


Maps of Enums


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.

  1. public enum AntStatus {
  2.  
  3. INITIALIZING,
  4.  
  5. COMPILING,
  6.  
  7. COPYING,
  8.  
  9. JARRING,
  10.  
  11. ZIPPING,
  12.  
  13. DONE,
  14.  
  15. ERROR
  16.  
  17. }
  18.  
  19.  
  20. public void testEnumMap(PrintStream out) throws IOException {
  21.  
  22. // Create a map with the key and a String message
  23.  
  24. EnumMap<AntStatus, String> antMessages =
  25.  
  26. new EnumMap<AntStatus, String>(AntStatus.class);
  27.  
  28.  
  29.  
  30. // Initialize the map
  31.  
  32. antMessages.put(AntStatus.INITIALIZING, "Initializing Ant...");
  33.  
  34. antMessages.put(AntStatus.COMPILING, "Compiling Java classes...");
  35.  
  36. antMessages.put(AntStatus.COPYING, "Copying files...");
  37.  
  38. antMessages.put(AntStatus.JARRING, "JARring up files...");
  39.  
  40. antMessages.put(AntStatus.ZIPPING, "ZIPping up files...");
  41.  
  42. antMessages.put(AntStatus.DONE, "Build complete.");
  43.  
  44. antMessages.put(AntStatus.ERROR, "Error occurred.");
  45.  
  46.  
  47.  
  48. // Iterate and print messages
  49.  
  50. for (AntStatus status : AntStatus.values( ) ) {
  51.  
  52. out.println("For status " + status + ", message is: " +
  53.  
  54. antMessages.get(status));
  55.  
  56. }
  57.  
  58. }

Report this snippet 

You need to login to post a comment.