K.Nicholas
K.Nicholas

Reputation: 11551

Java lambda toMap of Map<String, Boolean>

I can't see why this fails;

Map<String, Boolean> iMap = Arrays.asList("1","2","3","4","5").stream()
  .collect(Collectors.toMap(k->k, Boolean.TRUE));

The error Message:

Multiple markers at this line
    - Type mismatch: cannot convert from T to K
    - The method toMap(Function<? super T,? extends K>, Function<? super T,? extends U>) 
      in the type Collectors is not applicable for the arguments ((<no type> k) -> {}, Boolean)

Any help appreciated.

Upvotes: 1

Views: 2611

Answers (1)

shmosel
shmosel

Reputation: 50732

Collectors.toMap() expects a Function for both parameters. You're trying to pass a Boolean instead. Try this:

Collectors.toMap(k->k, k->Boolean.TRUE)

Upvotes: 7

Related Questions