简体   繁体   中英

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. 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?

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.

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. 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. 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 . As a matter of fact, you don't even need the put statement. Since you aren't remove ing the List , its reference will stay in the Map so any updates to the List will be visible.

All you need is inventoryMap.get(x).add(s) .

First of all inventoryMap.get(x).add(s) returns boolean (whether elements where successfully added or not ). So is incompatible with the type ArrayList<String> . You can simple do

inventoryMap.get(x).add(s)

No need to explicitly call pur() function.

Secondly int x = s.length(); should be Integer x = s.length(); . You can put int where Integer is expected(anyways you cannot use int in generics).

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. You need to break down this statement. something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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