Tom Lilletveit
Tom Lilletveit

Reputation: 2002

Java: using ArrayList in HashMap with String as key

I don´t see where my bug is here, what I got is an array of Strings containing the following values, line by line:

San Jose -> San Francisco
San Jose -> Anchorage
New York -> Anchorage
New York -> San Jose
New York -> San Francisco
New York -> Honolulu
Anchorage -> New York
Anchorage -> San Jose
Honolulu -> New York
Honolulu -> San Francisco
Denver -> San Jose
San Francisco -> New York
San Francisco -> Honolulu
San Francisco -> Denver

I want to put these values in a HashMap using the string on the right as the key, and have the value be an ArrayList, so that if I ask for destinations for 'San Jose', it will iterate through the list and return 'San Francisco' and 'Anchorage'. I haven´t managed to get the Key-Value relationship working correctly. Cause when the method is done running. For any city the value will be that of it´s last iteration, (San Francisco) returning New York, Honolulu and Denver for all keys.

    private void createDataBase()
{   
    ArrayList<String> tempValues = new ArrayList <String>();
    for (int i = 0; i < database.size();i++) 
    {
        String line = database.get(i);
        //String value = "";  
        if (line.length() > 1)
        {
            int keyStart = 0;
            int keyEnd = line.indexOf("-");

            int valueStart = keyEnd+3;
            int valueEnd = line.length();

            String key = line.substring(keyStart, keyEnd);
            String value = line.substring(valueStart, valueEnd);
            tempValues.add(value);
            System.out.println(keyValueDatabase.toString());
            keyValueDatabase.put(key, tempValues);

        }
        else 
        {
            tempValues.clear();
        }

    }
    System.out.println(keyValueDatabase.toString());
}

Upvotes: 2

Views: 2006

Answers (3)

sunsin1985
sunsin1985

Reputation: 2607

here is another value to look at it:

I guess you can check the "keyValueDatabase" map to see if the key is already in the map and if it doesnt exist add a new list as the value, if it does grab the list and add one more item to it.

private void createDataBase()
{
    for (int i = 0; i < database.size();i++) 
    {
    String line = database.get(i);
    //String value = ""; 
    if (line.length() > 1)
    {
        int keyStart = 0;
        int keyEnd = line.indexOf("-");

        int valueStart = keyEnd+3;
        int valueEnd = line.length();

        String key = line.substring(keyStart, keyEnd);
        String value = line.substring(valueStart, valueEnd);
        tempValues.add(value);
        System.out.println(keyValueDatabase.toString());

        //Check if the map already contains an entry for the key
        if(keyValueDatabase.containsKey(key))
        {
        //If yes, grab the value and add one to the list
        ArrayList<String> tempValues = keyValueDatabase.get(key);
        tempValues.add(value); //this should get updated in the same list due to reference
        }
        else
        {   ArrayList<String> tempValues = new ArrayList <String>();
        tempValues.add(value);
        keyValueDatabase.put(key, tempValues);
        }                        
    }
    }
    System.out.println(keyValueDatabase.toString());
}

Upvotes: 1

hyde
hyde

Reputation: 62908

This piece of your code is wrong:

tempValues.add(value);
System.out.println(keyValueDatabase.toString());
keyValueDatabase.put(key, tempValues);

Instead of above piece, you need to do something like this:

List<String> values = keyValueDatabase.get(key);
if (values == null) {
   values = new ArrayList <String>();
   keyValueDatabase.put(key, values);
}
values.add(value);

...and remove tempValues.

Upvotes: 0

Arun Manivannan
Arun Manivannan

Reputation: 4313

Unless you aren't able to use external libraries, you might want to consider MultiValueMap

Upvotes: 1

Related Questions