Java compilation error with method reference, but works with lambda

We are trying to add an overloaded method collectto our extended Java Stream API, which we currently define as:

interface ExtendedStream<R> extends Stream<R> {

   <R1> R1 collect(SerializableSupplier<Collector<? super R, ?, R1>> supplier);

}

SerializableSupplier defined as simple:

interface SerializableSupplier<T> extends Serializable, Supplier<T> {
}

Calling this collection method using lambda works fine, but calling it using a method reference has not compiled with errors such as:

Error:(50, 72) java: incompatible types: cannot infer type-variable(s) R1,capture#1 of ?,T
    (argument mismatch; bad return type in method reference
      java.util.stream.Collector<T,capture#1 of ?,java.util.List<T>> cannot be converted to java.util.stream.Collector<? super java.util.Map.Entry<java.lang.Integer,java.lang.String>,?,R1>)

I created a standalone Java class here , which you can load into your compiler to try it out.

I am currently using the Java version:

java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

Is this a mistake or some mistake in how we define things?

NOTE . I get this compilation error both from IntelliJ and when compiling with Maven. I do not use Eclipse.

Cheers, Galder

+4
1

javac 8, javac-9 ( ) - , .

, ... , Comparator Comparator.comparingInt...:

 return entrySet.stream().sorted(
            Comparator.comparingInt(Entry::getKey)).collect(
                    Collectors::<Map.Entry<Integer, String>> toList); 
+4

Source: https://habr.com/ru/post/1685857/


All Articles