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());
}
4

3 回答 3

1

这是另一个值得关注的值:

我想您可以检查“keyValueDatabase”映射以查看该键是否已在映射中,如果它不存在,则添加一个新列表作为值,如果它确实抓取该列表并向其中添加一个项目。

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());
}
于 2012-10-26T19:27:21.677 回答
1

除非您无法使用外部库,否则您可能需要考虑MultiValueMap

于 2012-10-26T19:07:51.667 回答
0

您的这段代码是错误的:

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

而不是上面的部分,你需要做这样的事情:

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

...并删除tempValues.

于 2012-10-26T19:13:02.567 回答