Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Generics solve as one function but not two

  1. #1
    Junior Member
    Join Date
    Apr 2022
    Thanked 1 Time in 1 Post

    Question Generics solve as one function but not two

    So I am trying to write an internal DSL and having a hard time to get generics work the way I want. I know I can write one method call like

    collectingAndThen(toSet(), Set::size);

    but I want to write


    which works OK here where I am passing the result into a method parameter, like so

    var grouped = collect(groupingBy(i->i % 3, toSet().andThen(Set::size)), that);

    but here where I try to return it

        public static <X> Collector<X,Set<X>,Integer> countDistinct() {
            // return collectingAndThen(toSet(), Set::size);
            return Collectors.<X>toSet().andThen(Set::size);

    I have to write


    to tell the compiler what the type parameter is. In my mind it should be able to figure it out, but the compiler tells me

    incompatible types: no instance(s) of type variable(s) FinalResult exist so that com.ontology2.pidove.checked.Collector<java.lang.Object,java.util.Set<java.lang.Object>,FinalResult> conforms to com.ontology2.pidove.checked.Collector<X,java.util.Set<X>,java.lang.Integer>

    Here are the definitions:

        public static <X> SimpleCollector<X, Set<X>> toSet() {
            return toCollection(HashSet::new);
    public record SimpleCollector<Input, Result> (
        Supplier<Result> supplier,
        BiConsumer<Input, Result> accumulator
    ) implements AnyCollector<Input, Result> {
        public <FinalResult> Collector<Input,Result,FinalResult> andThen(Function<Result,FinalResult> finisher) {
            return Collector.of(supplier(), accumulator(), finisher);

    if I don't put in the <X>. I like the way Function.andThen() works and I'd like to make my andThen() work the same way. Am I missing something or I am butting my head against type erasure or both?

    Can I make this work or should I just settle for writing static functions that always get applied on the left?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Generics solve as one function but not two

    Try asking on this forum: http://www.coderanch.com/forums
    there are many moderators there that know generics.

    Posted here: https://coderanch.com/t/750885/java/...eter-returning
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Generics
    By stewbond in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 17th, 2013, 04:31 PM
  2. Need some help with Generics
    By bankston13 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 5th, 2012, 03:06 PM
  3. Generics.
    By Kumarrrr in forum Java Theory & Questions
    Replies: 1
    Last Post: December 6th, 2011, 06:53 PM
  4. Generics
    By Kerr in forum Collections and Generics
    Replies: 2
    Last Post: May 19th, 2011, 06:44 PM
  5. Generics
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 21
    Last Post: December 6th, 2010, 07:08 PM

Tags for this Thread