あなたは明らかにMap
の特定の実装を必要として、あなたはmapSupplier
代わりのtoMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper)
も、シーンの背後にある場合、それはまだなるために提供できるようにする方法Collectors.toMap
を使用する必要がありますHashMap
を返します。javadocには明示的に指定されていないため、次のバージョンのJavaでそれが本当であるかどうかを確認する方法がありません。
だからあなたのコードは次のようなものでなければなりません:あなたは、単に次のようコレクターとしてtoMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper)
を使用し、Map
の実装を気にしない場合は
HashMap<Integer,String> studentHash = studentList.stream().collect(
Collectors.toMap(
s -> s.studentId, s -> s.studentName,
(u, v) -> {
throw new IllegalStateException(
String.format("Cannot have 2 values (%s, %s) for the same key", u, v)
);
}, HashMap::new
)
);
:
Map<Integer,String> studentHash = studentList.stream().collect(
Collectors.toMap(s -> s.studentId, s -> s.studentName)
);
がどうあるべきかマップキーと値? – Saravana
何を試しましたか?これはコード作成サービスではありません。ヒントは次のとおりです: 'Collectors'の' toMap'メソッドを見てください。 – marstran