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))