title: Java hardcoded switch vs hashmap
body: <p>Some Message class is able to return a tag name based on tag number<br><br>
  Since this class is instanciated many times, I am a bit reluctant to create a HashMap for each instance:</p>
  
  <pre><code>public class Message {
    private HashMap&lt;Integer,String&gt; tagMap;
  
    public Message() {
      this.tagMap = new HashMap&lt;Integer,String&gt;();
      this.tagMap.put( 1, "tag1Name");
      this.tagMap.put( 2, "tag2Name");
      this.tagMap.put( 3, "tag3Name");
    }
  
    public String getTagName( int tagNumber) {
      return this.tagMap.get( tagNumber);
    }
  }
  </code></pre>
  
  <p>In favor of hardcoding:</p>
  
  <pre><code>public class Message {
    public Message() {
    }
  
    public String getTagName( int tagNumber) {
      switch( tagNumber) {
        case 1: return "tag1Name";
        case 2: return "tag2Name";
        case 3: return "tag3Name";
        default return null;
      }
    }
  }
  </code></pre>
  
  <p>When you put everything in the mix ( Memory, Performance, GC, ...)<br><br>
  <strong>Is there any reason to stick to HashMap?</strong></p>