Javaでメソッド参照を使用してコレクターtoMap方法でストリームオブジェクトを取得する8

Saurav Ojha:

私が使用してリストを反復処理しようとしていますstream()とキーが蒸気要素そのものであり、かつ値がtrue AtomicBoolean、あるマップに置きます。

List<String> streamDetails = Arrays.asList("One","Two");
toReplay = streamDetails.stream().collect(Collectors.toMap(x -> x.toString(), new AtomicBoolean(true)));

私は、コンパイル時にエラーの下に取得します。

Type mismatch: cannot convert from String 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> x) -> {}, 
     AtomicBoolean)

私が間違って何をしていることができ、私は何を交換私のものx -> x.toString()に?

ernest_k:

new AtomicBoolean(true)2番目のパラメータには有効ではありません式がありますCollectors.toMap

toMapここのでしょうFunction<? super String, ? extends AtomicBoolean>(自分の意図したタイプ、AtomicBooleanのマップ値に)ストリーム要素を変換(または文字列を入力するためのもの)、および正しい引数は次のようになります。

Collectors.toMap(x -> x.toString(), x -> new AtomicBoolean(true))

どちらも使用して書き込むことができますFunction.identity

Collectors.toMap(Function.identity(), x -> new AtomicBoolean(true))

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=410444&siteId=1