Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Associative arrays and Java

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: Java associative-array

However, I don't think the solutions mentioned will solve my problem. I'll explain.

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.

in PHP I could just do this:

$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");

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.

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!

UPDATE

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.

public class iso_countries {  
    Map<String, Object> country_data        = new HashMap<String, Object>();
    Map<String, String> country_name        = new HashMap<String, String>();
    Map<String, String[]> country_idd       = new HashMap<String, String[]>();
    Map<String, String[]> country_cid       = new HashMap<String, String[]>();

    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
like image 530
slinden77 Avatar asked Jun 01 '12 19:06

slinden77


3 Answers

Maybe I'm misunderstanding your issue, but why not create your own object to hold the data, then store those in the HashMap?

Java is an object-oriented language, so why not use its most famous facility: objects!

like image 61
kevin628 Avatar answered Oct 31 '22 14:10

kevin628


An array in PHP is actually a hash, not an array.

The correct thing to use in java is a HashMap. You can also store multpile values in a Hashmap by storing arrays or lists in it. So HashMap<String, HashMap<String, Object> or Hashmap<String, List<Object>> might help you.

When you use multi-dimensional arrays, you have to use integers as key.

like image 37
StandByUkraine Avatar answered Oct 31 '22 13:10

StandByUkraine


You can store arrays as values of a HashMap:

HashMap<Integer,String[]> hm = new HashMap<Integer,String[]>();
hm.put( 1, new String[]{ "a", "b" } );

As for as having "multi-dimensional" keys, you can always wrap them together with a class. Another, albeit ugly, solution would be to have HashMaps of HashMaps.

like image 8
tskuzzy Avatar answered Oct 31 '22 12:10

tskuzzy