3

I have a map of < string, List< String type>>, What I am trying to do is add a string to map value if list is present, else create a list and add it to the list and insert in map. s1, s2 are strings.

Code:

Map<String, List<String>> map = new HashMap<>();

map.put(s1,(map.getOrDefault(s1, new LinkedList<String>())).add(s2));

Error:

error: incompatible types: boolean cannot be converted to List<String>

What's wrong with this !!!

Sazzadur Rahaman
  • 6,938
  • 1
  • 30
  • 52
RobinHood
  • 33
  • 1
  • 8

3 Answers3

10

add method of list 'map.getOrDefault(s1, new LinkedList())).add(s2)' is return boolean so you have to do it in separate line

so try like this

    Map< String, List< String>> map = new HashMap<>();

    List<String> list = map.get(s1);
    if(list == null){
      list = new LinkedList<>();
      map.put(s1,list);
    }
    list.add(s2);

If use java 8 and need to do in single line do like this

    map.computeIfAbsent(s1, k -> new LinkedList<>()).add(s2); 
janith1024
  • 1,042
  • 3
  • 12
  • 25
  • Since [`getOrDefault`](https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#getOrDefault-java.lang.Object-V-) was added in Java 8, it's a fair assumption that OP has Java 8. – Andreas Jun 25 '18 at 03:33
  • @Andreas Thanks for your input I didn't use 'getOrDefault' method before. thanks again for your valuable input. – janith1024 Jun 25 '18 at 04:23
  • @Holger yep true I use LinkedList because question asked for LinkedList otherwise ArrayList is better. Thanks for your input – janith1024 Jun 25 '18 at 10:56
  • 1
    Had an error in my previous comment. The Op’s original approach would work with `List list = map.getOrDefault(s1, new LinkedList<>()); list.add(s2); map.putIfAbsent(s1, list);`, but, of course, `map.computeIfAbsent(s1, k -> new LinkedList<>()).add(s2);` still is straight forward and more efficient. And as said, [even better would be `map.computeIfAbsent(s1, k -> new ArrayList<>()).add(s2);`](https://stackoverflow.com/q/322715/2711488)… – Holger Jun 25 '18 at 10:59
  • this really helps, thanks @janith1024 and I see I missed on usage of lambda or anonymous functions in Java 8. I was able to do it in other ways, but yes this computeIfAbsent is really a one line saviour – RobinHood Jun 26 '18 at 05:33
1

This call,

(map.getOrDefault(s1, new LinkedList())).add(s2)

Returns a boolean primitive, which can not be casted to a List. That why you are getting this error.

You can solve it like so,

map.compute(s1, (k, v) -> v == null ? new LinkedList<>() : v).add(s2);

The trick here is, map.compute() returns the new value associated with the specified key, and then you can add the s2 String literal into that afterward.

Ravindra Ranwala
  • 20,744
  • 6
  • 45
  • 63
  • 3
    [`computeIfAbsent`](https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#computeIfAbsent-K-java.util.function.Function-) would be better than `compute` in this case. – Andreas Jun 25 '18 at 03:32
0
Map<String, List<String>> map = new HashMap<>();

// gets the value if it is present in the map else initialze the list
List<String> li = map.getOrDefault(s1,new LinkedList<>());

li.add(s2);

map.put(s1,li); 
Supp
  • 1