Issue
I copied this code below from book to prepare for Java Certificate exam. When compile on Netbeans IDE(jdk 1.8.0_144), I got the compiler error " inferred type does not conform to upper bound(s) on Netbean IDE"
public static void main(String[] args) {
Stream<String> ohMy = Stream.of("lions", "tigers", "bears");
Map<Integer, Optional<Character>> map = ohMy.collect(
Collectors.groupingBy(
String::length,
Collectors.mapping(s -> s.charAt(0),
Collectors.minBy(Comparator.naturalOrder()))));
System.out.println(map);
}
It's still compile on Eclipse, please explain what is wrong here? I uploaded error detail here:
Solution
You may need to help the compiler here as it's a type inference issue:
You can solve the problem by explicitly specifying the type for the Comparator
or any of the Collectors
.
Collectors.<Character>minBy()
Comparator.<Character>naturalOrder()
Collectors.mapping((String s) -> s.charAt(0)
Answered By - Ousmane D.