title: Hashmap vs Array performance
body: <p>Is it (performance-wise) better to use Arrays or HashMaps when the indexes of the Array are known? Keep in mind that the 'objects array/map' in the example is just an example, in my real project it is generated by another class so I cant use individual variables.</p>
  
  <p>ArrayExample:</p>
  
  <pre><code>SomeObject[] objects = new SomeObject[2];
  objects[0] = new SomeObject("Obj1");
  objects[1] = new SomeObject("Obj2");
  
  void doSomethingToObject(String Identifier){
      SomeObject object;
      if(Identifier.equals("Obj1")){
          object=objects[0];
      }else if(){
          object=objects[1];
      }
      //do stuff
  }
  </code></pre>
  
  <p>HashMapExample:</p>
  
  <pre><code>HashMap objects = HashMap();
  objects.put("Obj1",new SomeObject());
  objects.put("Obj2",new SomeObject());
  
  void doSomethingToObject(String Identifier){
      SomeObject object = (SomeObject) objects.get(Identifier);
      //do stuff
  }
  </code></pre>
  
  <p>The HashMap one looks much much better but I really need performance on this so that has priority.</p>
  
  <p><strong>EDIT:</strong> Well Array's it is then, suggestions are still welcome</p>
  
  <p><strong>EDIT:</strong> I forgot to mention, the size of the Array/HashMap is always the same (6)</p>
  
  <p><strong>EDIT:</strong> It appears that HashMaps are faster
  Array: 128ms
  Hash: 103ms</p>
  
  <p>When using less cycles the HashMaps was even twice as fast</p>
  
  <p>test code:</p>
  
  <pre><code>import java.util.HashMap;
  import java.util.Random;
  
  public class Optimizationsest {
  private static Random r = new Random();
  
  private static HashMap&lt;String,SomeObject&gt; hm = new HashMap&lt;String,SomeObject&gt;();
  private static SomeObject[] o = new SomeObject[6];
  
  private static String[] Indentifiers = {"Obj1","Obj2","Obj3","Obj4","Obj5","Obj6"};
  
  private static int t = 1000000;
  
  public static void main(String[] args){
      CreateHash();
      CreateArray();
      long loopTime = ProcessArray();
      long hashTime = ProcessHash();
      System.out.println("Array: " + loopTime + "ms");
      System.out.println("Hash: " + hashTime + "ms");
  }
  
  public static void CreateHash(){
      for(int i=0; i &lt;= 5; i++){
          hm.put("Obj"+(i+1), new SomeObject());
      }
  }
  
  public static void CreateArray(){
      for(int i=0; i &lt;= 5; i++){
          o[i]=new SomeObject();
      }
  }
  
  public static long ProcessArray(){
      StopWatch sw = new StopWatch();
      sw.start();
      for(int i = 1;i&lt;=t;i++){
          checkArray(Indentifiers[r.nextInt(6)]);
      }
      sw.stop();
      return sw.getElapsedTime();
  }
  
  
  
  private static void checkArray(String Identifier) {
      SomeObject object;
      if(Identifier.equals("Obj1")){
          object=o[0];
      }else if(Identifier.equals("Obj2")){
          object=o[1];
      }else if(Identifier.equals("Obj3")){
          object=o[2];
      }else if(Identifier.equals("Obj4")){
          object=o[3];
      }else if(Identifier.equals("Obj5")){
          object=o[4];
      }else if(Identifier.equals("Obj6")){
          object=o[5];
      }else{
          object = new SomeObject();
      }
      object.kill();
  }
  
  public static long ProcessHash(){
      StopWatch sw = new StopWatch();
      sw.start();
      for(int i = 1;i&lt;=t;i++){
          checkHash(Indentifiers[r.nextInt(6)]);
      }
      sw.stop();
      return sw.getElapsedTime();
  }
  
  private static void checkHash(String Identifier) {
      SomeObject object = (SomeObject) hm.get(Identifier);
      object.kill();
  }
  </code></pre>
  
  <p>}</p>