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?
ArrayList.add()...