我在HashMap中使用HashMap,但我也需要使用ArrayList作为值。我很难给予价值

时间:2017-04-07 13:02:18

标签: java arraylist hashmap

我在HashMap中使用HashMap,但我也需要使用ArrayList作为值。我无法给予价值

private HashMap<String, HashMap<String, ArrayList<String>>> languages_hashmap = new HashMap<String, HashMap<String, ArrayList<String>>>();

public void addvalue (String language, String code , String value, boolean isStringArray)
{

    HashMap<String,ArrayList<String>> dictionary = languages_hashmap.get(language);
    if (dictionary == null)
    {
        dictionary = new HashMap<String,ArrayList<String>>();
    }

    dictionary.put(code, value );
    languages_hashmap.put(code, dictionary);

}

5 个答案:

答案 0 :(得分:2)

首先,你需要生活中的对象,嵌套很多地图和集合是一种代码气味。

您的代码中有两个不同的概念:

  • Dictionaryvalues
  • 保留code
  • Dictionary与语言相关联的另一件事。我称之为Translator,但您应该找到一个更合适的名称

分离职责将使您的代码更具可读性,并允许您分别测试每个案例:

public class Translator {

    // Reference interface instead of concrete types whenever you can
    // (i.e. use Map instead of HashMap)
    private Map<String, Dictionary> dictionaries = new HashMap<>();

    public void addValue(String lang, String code, String value) {
        dictionaries.get(lang).add(code, value);
    }

    public List<String> getValue(String lang, String code) {
        return dictionaries.get(lang).get(code);
    }

    public void addDictionary(String lang, Dictionary dictionary) {
        dictionaries.put(lang, dictionary);
    }

}


public class Dictionary {

    // Same here, use interfaces like List or Map instead of their implementations
    Map<String, List<String>> values = new HashMap<>();

    public void add(String code, String value) {
        List<String> list = values.get(code);
        // if there is no previous value for this code, 
        // create a new list of values
        if (list == null) {
            list = new ArrayList<>();
            values.put(code, list);
        }
        list.add(value);
    }

    public List<String> get(String code) {
        return values.get(code);
    }

}

答案 1 :(得分:0)

public void addvalue (String language, String code , String value, boolean isStringArray)
{

HashMap<String,ArrayList<String>> dictionary = languages_hashmap.get(language);
if (dictionary == null)
{
    dictionary = new HashMap<String,ArrayList<String>>();
    languages_hashmap.put(code, dictionary);
}

List<String> list = dictionary.get(code);
if (list == null) {
   list = new ArrayList<>();
   dictionary.put(code, list);
}
list.add(value);
}

答案 2 :(得分:0)

您的代码无法编译。 dictionary.put(code, value );

需要ArrayList<String>,并且您正在传递String

ArrayList<String> list = new ArrayList<>(value);
dictionary.put (code, list);

答案 3 :(得分:0)

参数的类型是String,它应该是ArrayList&lt;串GT;因为字典变量是一种HashMap&lt;串,的ArrayList&GT;字典

    dictionary.put (code, value);

其中代码是字符串,应该是ArrayList类型

public void addvalue (String language, String code , ArrayList<String> value, boolean isStringArray)

如果要将值作为String参数传递给addvalue函数,则创建ArrayList&lt;串GT;在函数中为它添加值然后传递给字典。

     private HashMap<String, HashMap<String, ArrayList<String>>>languages_hashmap = new HashMap<String, HashMap<String, 
     ArrayList<String>>>();

     ArrayList<String> tempValue=new ArrayList<>();

    public void addvalue (String language, String code , String value, 
    boolean isStringArray)
       {
         tempValue.add("value");
         HashMap<String,ArrayList<String>> dictionary = languages_hashmap.get(language);

      if (dictionary == null)
      {
          dictionary = new HashMap<String,ArrayList<String>>();
      }

      dictionary.put(code,  tempValue );
      languages_hashmap.put(code, dictionary);

  }

答案 4 :(得分:0)

如其他人所述,您无法将String传递给ArrayList

将方法签名中的值类型更改为ArrayList

private HashMap<String, HashMap<String, ArrayList<String>>> languages_hashmap = new HashMap<String, HashMap<String, ArrayList<String>>>();

public void addvalue(String language, String code , ArrayList<String> value, boolean isStringArray)
{

    HashMap<String,ArrayList<String>> dictionary = languages_hashmap.get(language);
    if (dictionary == null)
    {
        dictionary = new HashMap<String,ArrayList<String>>();
    }

    dictionary.put(code, value);
    languages_hashmap.put(code, dictionary);

}

或者将String的ArrayList传递给它。