Aiden :
So I've got a class Standard
with properties and constructor:
String studentName;
String category;
Standard(studentName, category)
I've also got a List
of Standard
objects like so:
[S1,S2,S3,S4]
where
S1 = Standard("ABC","1");
S2 = Standard("DEF","2")
S3 = Standard("IJK","1")
S4 = Standard("LMN","2")
I would like to group Standard
objects with same category
into a Map(String,List<Standard>)
with key as category
and value as List
of Standard
objects like so:
Map<String,List<Standard>> m = {1=[S1, S3], 2=[S2,S4]};
Now I would like to know whether this can be done using Java 8 streams.
Andronicus :
You can use groupingBy
collector:
Map<String, List<Standard>> map = standards.stream()
.collect(Collectors.groupingBy(Standard::getCategory));
Assuming you have getCategory
method implemented.