2

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());
}
1
  • Not related to your problem, but if same destination should appear only once, you could also use a Set to guarantee this: TreeSet if you want destinations always sorted, LinkedHashSet if you want to iterate them in original order, HashSet otherwise. Commented Oct 26, 2012 at 19:21

3 Answers 3

1

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

1

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());
}
2
  • I prefer the way in my answer, as it will do only one map lookup for existing items, and also works if map would happen to contain null values (though that is not the case here). Commented Oct 26, 2012 at 19:40
  • @hyde :yes you are correct. Your way will result in lesser map lookups... Thanks! Commented Oct 26, 2012 at 20:12
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.