Skip to content

Instantly share code, notes, and snippets.

@jnizet
Created April 2, 2018 09:16
Show Gist options
  • Select an option

  • Save jnizet/437005e270822db3940f46f7ef8f7c49 to your computer and use it in GitHub Desktop.

Select an option

Save jnizet/437005e270822db3940f46f7ef8f7c49 to your computer and use it in GitHub Desktop.
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
public class Repro {
public static void main(String[] args) {
Holder someHolder = new Holder();
List<C> Cs = new LinkedList<>();
// works fine
someHolder.getAs()
.stream()
.map(Converter::convertAToC)
.forEach(Cs ::add);
someHolder.getBs()
.stream()
.map(Converter::convertBToC) // bad return type in method reference: cannot convert C to R
.filter(Objects::nonNull)
.forEach(Cs ::add); // Cannot resolve method 'add'
}
}
class A {}
class B {}
class C {}
class Converter {
public static C convertAToC(A a) { return new C(); }
public static C convertBToC(B b) { return new C(); }
}
class Holder {
List<A> getAs() {
return Collections.emptyList();
}
List<B> getBs() {
return Collections.emptyList();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment