title: Easiest way to get Enum in to Key Value pair
body: <p>I have defined my Enums like this.</p>
  
  <pre><code>public enum UserType {
  
      RESELLER("Reseller"),
  
      SERVICE_MANAGER("Manager"),
  
      HOST("Host");
  
      private String name;
  
      private UserType(String name) {
          this.name = name;
      }
  
      public String getName() {
          return name;
      }
  
  }
  </code></pre>
  
  <p>What should be the easiest way to get a key-value pair form the enum values?</p>
  
  <p>The output map i want to create should be like this</p>
  
  <pre><code>key   = Enum(example:- HOST)
  value = Host
  </code></pre>
  
  <p>The map I want do define is </p>
  
  <pre><code>Map&lt;String, String&gt; constansts = new HashMap&lt;String, String&gt;();
  </code></pre>
  
  <p>Ans: What I Did</p>
  
  <p>I have created a Generic Method to access any enum and change values from that to a Map. I got this IDEA, form a code fragment found at here in any other thread.</p>
  
  <pre><code>public static &lt;T extends Enum&lt;T&gt;&gt; Map&lt;String, String&gt; getConstantMap(
          Class&lt;T&gt; klass) {
  
      Map&lt;String, String&gt; vals = new HashMap&lt;String, String&gt;(0);
  
      try {
          Method m = klass.getMethod("values", null);
          Object obj = m.invoke(null, null);
  
          for (Enum&lt;T&gt; enumval : (Enum&lt;T&gt;[]) obj) {
              vals.put(enumval.name(), enumval.toString());
          }
  
      } catch (Exception ex) {
          // shouldn't happen...
      }
  
      return vals;
  }
  </code></pre>
  
  <p>Now, After this all I have to do is call this method with all the enum constructs and i am Done.</p>
  
  <p>One More thing</p>
  
  <p>To get This done i have to orverride the toString Method like this</p>
  
  <pre><code>    public String toString() {
          return name;
      }
  </code></pre>
  
  <p>Thanks.</p>