title: Associative arrays and Java
body: <p>I've run into the same problem a lot of people seem to face coming from PHP, the lack of a decent and easy to use associative array solution.
  I've read quesions here which basically all suggested to use a HashMap, like this Q: <a href="https://stackoverflow.com/questions/5122913/java-associative-array">Java associative-array</a></p>
  
  <p>However, I don't think the solutions mentioned will solve my problem. I'll explain.</p>
  
  <p>I have a list with 250 items (countries) for which I want to store data. The data is of undefined length, meaning it can hold multiple entries per "column", sometimes no entry, sometimes 4, etcetera.</p>
  
  <p>in PHP I could just do this:</p>
  
  <pre><code>$country_data = new array();
  $country_data["nl"]["currency"] = "euro";
  $country_data["nl"]["languages"] = "Dutch";
  ...
  $country_data["us"]["currency"] = "US dollar";
  $country_data["us"]["languages"] = array("English", "Spanish");
  </code></pre>
  
  <p>So sometimes I want to store an array, sometimes not. Ofcourse it could also be a array with only one entry instead of a string, but I'm just saying.</p>
  
  <p>So, the question is, how do I store and fetch arrays in arrays in a HashMap? I understand I am pretty much stuck with the ugly HashMap solution, but I still can't see how this will let me store arrays, I'm sure I'm overlooking something simple. An example based on mine would be great!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I opted to go for HashMaps of HashMaps
  The reason for this I need to be able to oversee everything easily, and change a few lines of values when needed. And this is flexible, I can easily just get a country name based on country code, a language, or I can get the country_data HashMap when I need it, or all country names, etcetera.</p>
  
  <pre><code>public class iso_countries {  
      Map&lt;String, Object&gt; country_data        = new HashMap&lt;String, Object&gt;();
      Map&lt;String, String&gt; country_name        = new HashMap&lt;String, String&gt;();
      Map&lt;String, String[]&gt; country_idd       = new HashMap&lt;String, String[]&gt;();
      Map&lt;String, String[]&gt; country_cid       = new HashMap&lt;String, String[]&gt;();
  
      public iso_countries(){      
           country_name.put("nl",         "Netherlands");      
           country_idd.put("nl",      new String[]{"+31"});
           country_cid.put("nl",      new String[]{"#31#", "*31#"});
           setData(country_name, country_cid, country_idd);
           // 249 * 4 lines more and later...
      }//end method
  
      public void setData(Map country_name, Map country_cid, Map country_idd){
           country_data.put("name",   country_name);
           country_data.put("idd",    country_idd);
           country_data.put("cid",    country_cid);
      }//end method   
  
      public String getCountryName(String countryCode){
          String name     = country_name.get(countryCode);
          return name;
      }//end method
  
      public String[] getCountryIdd(String countryCode){
          String prefix[] = country_idd.get(countryCode);
          return prefix;
      }//end method
  
      public String[] getCountryCid(String countryCode){
          String cid[]    = country_cid.get(countryCode);
          return cid;
      }//end method
  }//end class
  </code></pre>