Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.stream.Collectors;
  4.  
  5. class Main {
  6. public static void main(String argv[]) {
  7.  
  8. ArrayList<Integer> list = new ArrayList<Integer>();
  9. list.add(1);
  10. list.add(2);
  11. list.add(4);
  12.  
  13. // Filter - adapter, count - consumer.
  14. long test = list.stream().filter((num) -> num > 1).count();
  15.  
  16.  
  17.  
  18. // ForEach - adapter.
  19. list.stream().forEach((num) -> {
  20. System.out.println(num);
  21. });
  22.  
  23. // The same.
  24. for (int num : list) {
  25. System.out.println(num);
  26. };
  27.  
  28.  
  29. // Map - adapter, collect - consumer.
  30. List<String> newList = list
  31. .stream()
  32. .map((el) -> {
  33. return el.toString().concat(" Hello");
  34. })
  35. .collect(Collectors.toList());
  36.  
  37.  
  38. // The same.
  39. List<String> newList1 = new ArrayList<String>();
  40. for (Integer elem : list) {
  41. String tmp = elem.toString().concat(" Hello");
  42. newList1.add(tmp);
  43. }
  44.  
  45.  
  46. System.out.println(newList);
  47. System.out.println(newList1);
  48.  
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement