user1955934:
Stream.collect(Collector<? super T, A, R> collector)
<R,A> R collect(Collector<? super T,A,R> collector)
Performs a mutable reduction operation on the elements of this stream using a Collector.
Collectors.groupingBy(Function<? super T,? extends K> classifier)
public static <T,K> Collector<T,?,Map<K,List<T>>> groupingBy(Function<? super T,? extends K> classifier)
Returns a Collector implementing a "group by" operation on input elements of type T, grouping elements according to a classification function, and returning the results in a Map.
誰かがジェネリックを説明してくださいすることができT
、K
およびR
?私は本当にこのような方法は、上記の署名に適合することができますどのように混乱しています:
List<Student> studentList = ....
Map<String, List<Student>> groupByTeachersMap = studentList.stream()
.collect(Collectors.groupingBy(Student::getTeachersName));
私はどのように見ることができませんcollect
返すことができますMap<String, List<Student>>
上記の署名与えられました。誰かがこの署名を読み取る方法を説明できますか?
また:
以下の最低限のクラスを想定すると:
class Student {
String teachersName;
public String getTeachersName() {
return teachersName;
}
}
あなたは、各ステップで内外に戻り値の型を照合することによって、あなたのコードに関連付けることができます。たとえば、署名のためgroupingBy
のようにスタンド:
// <T, K> Collector<T, ?, Map<K, List<T>>> groupingBy(Function<? super T, ? extends K> classifier)
そして、あなたの特定の実装は次のように詳述されています。
Collectors.groupingBy(new Function<Student, String>() {
@Override
public String apply(Student student) {
return student.getTeachersName();
}
})
あなたのケースリターンでいます
Collector<Student, ?, Map<String, List<Student>>>
さらに、あなたがの署名を見ればcollect
、動作すなわち
// <R, A> R collect(Collector<? super T, A, R> collector)
それによって、あなたのケースで帰国R
のように:
Map<String, List<Student>>