How to fill HashMap object only once for program?

By mlm

Given the following portion of my wider program:

public class AnimalNames {
  private static String[] animalNames = {dog, cat, horse, cow, donkey, elephant};
  private static String[] animalNameAbbreviations = {d, c, h, co, d, e};
  public static HashMap<String, String> getAnimalNameTranslations() {
    HashMap<String, String> animalNameTranslations = new HashMap<String, String>();
    for (int i = 0; i < animalNames.length; i++) {
      animalNameTranslations.put(animalNameAbbreviations[i], animalNames[i])
    }
    return animalNameTranslations;
  }
}

I’m able to access the filled animalNameTranslations (using the static keyword) without instantiating the AnimalNames class, which is what I want. However, my program still has to fill animalNameTranslations every time I want to access it (using the for loop). Is there a way to fill the HashMap object only once for my program?

Source: Stack Overflow

    

Share it with your friends!

    Fatal error: Uncaught Exception: 12: REST API is deprecated for versions v2.1 and higher (12) thrown in /home/content/19/9652219/html/wp-content/plugins/seo-facebook-comments/facebook/base_facebook.php on line 1273