Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Jeanne Boyarsky
Ron McLeod
Sheriffs:
Paul Clapham
Liutauras Vilda
Devaka Cooray
Saloon Keepers:
Tim Holloway
Roland Mueller
Bartenders:
Forum:
Beginning Java
Collectors.mapping method
Alexandru Puiu
Ranch Hand
Posts: 31
2
posted 7 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi guys,
Can someone please help me understand why do I get an exception here:
import java.util.*; import java.util.stream.*; public class Test { public static void main(String[] args) { Stream<String> stream = Stream.of("lions", "tigers", "bears"); Map<Integer, Optional<Character>> map = stream .collect(Collectors.groupingBy( String::length, Collectors.mapping(s -> s.charAt(0), Collectors.minBy(Comparator.naturalOrder())))); System.out.println(map); } }
My compiler doesn't find anything wrong, but I get the following exception:
Error:(33, 26) java: no suitable method found for groupingBy(String::length,java.util.stream.Collector<java.lang.Object,capture#1 of ?,java.util.Optional<T>>) method java.util.stream.Collectors.<T,K,D,A,M>groupingBy(java.util.function.Function<? super T,? extends K>,java.util.function.Supplier<M>,java.util.stream.Collector<? super T,A,D>) is not applicable (cannot infer type-variable(s) T,K,D,A,M (actual and formal argument lists differ in length)) method java.util.stream.Collectors.<T,K,A,D>groupingBy(java.util.function.Function<? super T,? extends K>,java.util.stream.Collector<? super T,A,D>) is not applicable (inferred type does not conform to upper bound(s) inferred: java.lang.Object upper bound(s): T,java.lang.Comparable<? super T>,java.lang.Object) method java.util.stream.Collectors.<T,K>groupingBy(java.util.function.Function<? super T,? extends K>) is not applicable (cannot infer type-variable(s) T,K (actual and formal argument lists differ in length))
Alexandru Puiu
Ranch Hand
Posts: 31
2
posted 7 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Already found the answer! Thank's in advance!
Campbell Ritchie
Marshal
Posts: 80267
430
posted 7 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
That isn't an exception; that is a compile time error. Please show us how you corrected it.
Piet Souris
Bartender
Posts: 5584
213
posted 7 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
What, I wonder, is unclear about the error report?
@OP
you can get rid of the Optional<Character> if you so wish, by using 'collectingAndThen'. For instance:
class Test { public static void main(String[] args) { Stream<String> stream = Stream.of("lions", "tigers", "bears"); Comparator<Character> charComparator = Comparator.naturalOrder(); Map<Integer, Character> map = stream .collect( groupingBy( String::length, mapping( s -> s.charAt(0), collectingAndThen( minBy(charComparator), Optional::get ) ) ) ) ; System.out.println(map); } }
There are three kinds of actuaries: those who can count, and those who can't.
Message for you sir! I think it is a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Errata for OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide - page 223
Funny compile error using Collectors.maxBy and Comparator.naturalOrder() (and using lambdas)
min by doesn't compile? (Sybex)
OCPJP8: Difficulties with streams and collectors
Print the keys from HashMap for specific values
More...