I was playing around with examples from http://www.concretepage.com/java/jdk-8/java-8-unaryoperator-binaryoperator-example.
What I find really confusing is that when I mistakenly put a wrong type into one of generics when forming Collectors, java compiler gives me a very misleading message:
Non-static method cannot be referenced from a static context
My error has nothing to do with static vs instance context in reality:
Map<String, Map<Integer, Integer>> mapOfStudents = list.stream().collect(Collectors.groupingBy(Student::getClassName,
Collectors.toMap(Student::getName, Student::getAge)));
My mistake is in generic return type. When I correct it and put:
Map<String, Map<String, Integer>> mapOfStudents
everything goes back to normal.
Can someone explain the reason behind such a confusing error message? I'm sure the is a good one, but I fail to grasp it.
EDIT:
~$ java -version
openjdk version "1.8.0_121"
OpenJDK Runtime Environment (build 1.8.0_121-8u121-b13-0ubuntu1.16.04.2-b13)
OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)