Java lambda toMap of Map<String, Boolean>



我不明白为什么会失败;

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

错误消息:

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)

任何帮助表示赞赏。

Collectors.toMap()期望这两个参数都Function。您正在尝试传递Boolean。试试这个:

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

相关内容

最新更新