Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java 8: inferred type does not conform to upper bound(s) on Netbean IDE

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:

error deatail

like image 723
hoapham Avatar asked Dec 04 '25 14:12

hoapham


1 Answers

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)
like image 152
Ousmane D. Avatar answered Dec 07 '25 14:12

Ousmane D.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!