Created
April 2, 2018 09:16
-
-
Save jnizet/437005e270822db3940f46f7ef8f7c49 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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