Last active
March 2, 2018 19:31
-
-
Save danidiaz/c0c79c05622c1c7c84ec4a0cc1413615 to your computer and use it in GitHub Desktop.
example of Java wildcard capture helper https://docs.oracle.com/javase/tutorial/java/generics/capture.html
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.ArrayList; | |
| import java.util.List; | |
| public class Main { | |
| public static void main(String[] args) { | |
| List<OutAndIn<?>> boo = new ArrayList<>(); | |
| boo.add(new OutAndIn<String>("ffoo")); | |
| boo.add(new OutAndIn<Integer>(34)); | |
| OutAndIn<?> outin = boo.get(0); | |
| // outin.printValue(outin.getValue()); // this is not allowed | |
| wildcardCapture(outin); // but this is | |
| } | |
| private static <E> void wildcardCapture(OutAndIn<E> outin) { | |
| outin.printValue(outin.getValue()); | |
| } | |
| } |
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
| public class OutAndIn<E> { | |
| private E value; | |
| public OutAndIn(E value) { | |
| super(); | |
| this.value = value; | |
| } | |
| public E getValue() { | |
| return value; | |
| } | |
| public void printValue(E value2) { | |
| System.out.println(value2); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment