Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ConstructorMethodReference {
- public static void main(String args[]) {
- final List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
- // Method Reference
- copyElements(null, ArrayList<Integer>::new);
- // Lambda expression
- copyElements(list, () -> new ArrayList<Integer>());
- }
- private static void copyElements(final List<Integer> list, final Supplier<Collection<Integer>> targetCollection) {
- // Method reference to a particular instance
- list.forEach(targetCollection.get()::add);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement