I have a group of students. First I want to group them by tags. Then I want to further group these groups into the same name together.
Map<Integer,Map<String,List<String>>> groupping = students.stream() .collect(Collectors.groupingBy(Student::getMarks, Collectors.mapping(Student::getName,Collectors.toList())));
I get an error message
A non-static method cannot refer to a static context.
Yes. I pretty much understand that I cannot reference a non-static method without an instance. But with all these streaming operations, I'm a little confused about what went wrong.
Instead of fixing it; I really want to know what is going on here. Any of your entries are appreciated!
Because if I write, the grouping below is completely correct;
Map<Integer,List<Student>> m = students.stream(). collect(Collectors.groupingBy(Student::getMarks));
Here is my Student.java class (if you need it)
public class Student { private String name; private int marks; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getMarks() { return marks; } public void setMarks(int marks) { this.marks = marks; } public Student(String name, int marks) { this.name = name; this.marks = marks; } @Override public String toString() { return name + ':' + marks ; } }
java-8 java-stream method-reference collect
Jude niroshan
source share