Advertisement
Guest User

Untitled

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