Hello.
In
OCP Oracle Certified Professional
Java SE 8 Programmer II STUDY GUIDE
EXAM 1Z0-809
Jeanne Boyarsky and Scott Selikoff
SYBEX A Wiley Brand
Copyright 2016
ISBN: 978-1-119-06790-0
p. 223: example of mapping collector adding a lower level collector:
As said in errata, does not compile with some versions of Java 8:
error: no suitable method found for groupingBy(
String::length,Collector<Object,CAP#1,Optional<T#1>>)
Map<Integer, Optional<Character>> map = ohMy.collect(Collectors.groupingBy(
^
method Collectors.<T#3,K#1>groupingBy(Function<? super T#3,? extends K#1>) is not applicable
(cannot infer type-variable(s) T#3,K#1
(actual and formal argument lists differ in length))
method Collectors.<T#4,K#2,A#1,D#1>groupingBy(Function<? super T#4,? extends K#2>,Collector<? super T#4,A#1,D#1>) is not applicable
(inferred type does not conform to upper bound(s)
inferred: Object
upper bound(s): T#5,Comparable<? super T#5>,Object)
method Collectors.<T#6,K#3,D#2,A#2,M>groupingBy(Function<? super T#6,? extends K#3>,Supplier<M>,Collector<? super T#6,A#2,D#2>) is not applicable
(cannot infer type-variable(s) T#6,K#3,D#2,A#2,M
(actual and formal argument lists differ in length))
where T#1,T#2,T#3,K#1,T#4,K#2,A#1,D#1,T#5,T#6,K#3,D#2,A#2,M are type-variables:
T#1 extends T#2
T#2 extends Comparable<? super T#2>
T#3 extends Object declared in method <T#3,K#1>groupingBy(Function<? super T#3,? extends K#1>)
K#1 extends Object declared in method <T#3,K#1>groupingBy(Function<? super T#3,? extends K#1>)
T#4 extends Object declared in method <T#4,K#2,A#1,D#1>groupingBy(Function<? super T#4,? extends K#2>,Collector<? super T#4,A#1,D#1>)
K#2 extends Object declared in method <T#4,K#2,A#1,D#1>groupingBy(Function<? super T#4,? extends K#2>,Collector<? super T#4,A#1,D#1>)
A#1 extends Object declared in method <T#4,K#2,A#1,D#1>groupingBy(Function<? super T#4,? extends K#2>,Collector<? super T#4,A#1,D#1>)
D#1 extends Object declared in method <T#4,K#2,A#1,D#1>groupingBy(Function<? super T#4,? extends K#2>,Collector<? super T#4,A#1,D#1>)
T#5 extends Comparable<? super T#5> declared in method <T#5>naturalOrder()
T#6 extends Object declared in method <T#6,K#3,D#2,A#2,M>groupingBy(Function<? super T#6,? extends K#3>,Supplier<M>,Collector<? super T#6,A#2,D#2>)
K#3 extends Object declared in method <T#6,K#3,D#2,A#2,M>groupingBy(Function<? super T#6,? extends K#3>,Supplier<M>,Collector<? super T#6,A#2,D#2>)
D#2 extends Object declared in method <T#6,K#3,D#2,A#2,M>groupingBy(Function<? super T#6,? extends K#3>,Supplier<M>,Collector<? super T#6,A#2,D#2>)
A#2 extends Object declared in method <T#6,K#3,D#2,A#2,M>groupingBy(Function<? super T#6,? extends K#3>,Supplier<M>,Collector<? super T#6,A#2,D#2>)
M extends Map<K#3,D#2> declared in method <T#6,K#3,D#2,A#2,M>groupingBy(Function<? super T#6,? extends K#3>,Supplier<M>,Collector<? super T#6,A#2,D#2>)
where CAP#1 is a fresh type-variable:
CAP#1 extends Object from capture of ?
com\ictjob\clep\MyMain.java:168: error: cannot find symbol
Collectors.mapping(s -> s.charAt(0), Collectors.minBy(Comparator.naturalOrder()))));
^
symbol: method charAt(int)
location: variable s of type Object
com\ictjob\clep\MyMain.java:167: error: invalid method reference
String::length,
^
non-static method length() cannot be referenced from a static context
3 errors
========
I am working with the following version:
javac -version
javac 1.8.0_121
Here, with help of
https://stackoverflow.com/questions/24565278/java-util-comparator-naturalorder-takes-a-t-extends-comparable-super-t-and, 2 codes that compile and run:
The problem is that the missing type variable "Character" but I do not clearly understand the syntax in the second code "Comparator.<Character>naturalOrder(). Could you please explain? Thank you.