简体   繁体   English

如何在Java中将键添加为Integer并且值为arraylist的HashMap中添加元素?

[英]how to add elements to HashMap whose key is Integer and value is arraylist in Java?

I am a writing a class that whose constructor takes an List<String> and returns a hashmap having length of string as key(Integer) and its value as arrayList<String> that holds string. 我正在写一个类,该类的构造函数使用List<String>并返回一个哈希字符串,该哈希映射的字符串长度为key(Integer) ,其值作为包含字符串的arrayList<String> That is I am trying to map length of strings to list of strings. 那就是我试图将字符串的长度映射到字符串列表。 Here is my code. 这是我的代码。

public class Solver {
   Map<Integer,ArrayList<String>> inventoryMap;

    //constructor
    public Solver(List<String> list){
    inventoryMap=new HashMap<Integer,ArrayList<String>>();
    for (String s : list) {
         int x = s.length();
         if (inventoryMap.containsKey(x)){
            inventoryMap.put(x,inventoryMap.get(x).add(s));
         } else {
            newlist=new ArrayList<String>();
            newlist.add(s);
            inventoryMap.put(x,newlist);
         }
      }
   }

when I complile this code, I get the following error 当我编写此代码时,出现以下错误

Solver.java:12: put(java.lang.Integer,java.util.ArrayList<java.lang.String>) in java.util.Map<java.lang.Integer,java.util.ArrayList<java.lang.String>> cannot be applied to (int,boolean)
            inventoryMap.put(x,inventoryMap.get(x).add(s));

I think I am going wrong in adding String elements to my ArrayList<String> which is value of Map can you guide me with what I could possibly be going wrong? 我认为将String elements添加到my ArrayList<String>中是错误的,这是Map的value ,您可以指导我解决我可能出错的问题吗?

if (inventoryMap.containsKey(x)) {
     inventoryMap.put(x,inventoryMap.get(x).add(s));
} 

Change this with 改变这个

if (inventoryMap.containsKey(x)) {
    inventoryMap.get(x).add(s);
} 

Reason is inventoryMap.get(x).add(s) will return boolean so you cann't put boolean in place of List. 原因是inventoryMap.get(x).add(s)将返回布尔值,因此您不能将布尔值替换为List。

As map already contains list so adding any element in a list you need not to put any entry in a map. 由于地图已经包含列表,因此在列表中添加任何元素都不需要在地图中放置任何条目。 Just get the list from map and add element to it. 只需从地图中获取列表,然后向其中添加元素即可。

inventoryMap.get(x).add(s) returns boolean and you tried to put it in the map. ventoryMap.get(x).add(s)返回布尔值,并且您试图将其放入地图中。 This is the cause of the exception. 这是导致异常的原因。 Put the list in the map will resolve the issue. 将列表放在地图中将解决此问题。

Your code inventoryMap.get(x).add(s) adds the value to the list and return a boolean. 您的代码清单Map.get(x).add(s)将值添加到列表中并返回一个布尔值。 So You need to have something like. 所以你需要有类似的东西。

List<String> list =inventoryMap.get(x);
list.add(s);

You cannot chain your method calls like inventoryMap.put(x,inventoryMap.get(x).add(s)) since add returns a boolean . 由于add返回一个boolean inventoryMap.put(x,inventoryMap.get(x).add(s))因此无法链接方法类调用,例如inventoryMap.put(x,inventoryMap.get(x).add(s)) As a matter of fact, you don't even need the put statement. 实际上,您甚至不需要put语句。 Since you aren't remove ing the List , its reference will stay in the Map so any updates to the List will be visible. 由于您没有remove List ,因此其引用将保留在Map因此对List任何更新都将可见。

All you need is inventoryMap.get(x).add(s) . 您所需要的只是inventoryMap.get(x).add(s)

First of all inventoryMap.get(x).add(s) returns boolean (whether elements where successfully added or not ). 首先, inventoryMap.get(x).add(s)返回boolean (元素是否成功添加)。 So is incompatible with the type ArrayList<String> . 因此与ArrayList<String>类型不兼容。 You can simple do 你可以简单地做

inventoryMap.get(x).add(s)

No need to explicitly call pur() function. 无需显式调用pur()函数。

Secondly int x = s.length(); 其次int x = s.length(); should be Integer x = s.length(); 应该是Integer x = s.length(); . You can put int where Integer is expected(anyways you cannot use int in generics). 您可以将int放在需要Integer的地方(无论如何,您不能在泛型中使用int)。

The problem with this line 这条线的问题

        inventoryMap.put(x,inventoryMap.get(x).add(s));

is that inventoryMap.get(x).add(s) will return a boolean and map expects a List here. inventoryMap.get(x).add(s)将返回一个boolean并且map期望在此处有一个List You need to break down this statement. 您需要分解此语句。 something like this: 像这样的东西:

List<String> stirngsList = inventoryMap.get(x);
stirngsList.add(s);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM