Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.stream.Collectors;
- class Main {
- public static void main(String argv[]) {
- ArrayList<Integer> list = new ArrayList<Integer>();
- list.add(1);
- list.add(2);
- list.add(4);
- // Filter - adapter, count - consumer.
- long test = list.stream().filter((num) -> num > 1).count();
- // ForEach - adapter.
- list.stream().forEach((num) -> {
- System.out.println(num);
- });
- // The same.
- for (int num : list) {
- System.out.println(num);
- };
- // Map - adapter, collect - consumer.
- List<String> newList = list
- .stream()
- .map((el) -> {
- return el.toString().concat(" Hello");
- })
- .collect(Collectors.toList());
- // The same.
- List<String> newList1 = new ArrayList<String>();
- for (Integer elem : list) {
- String tmp = elem.toString().concat(" Hello");
- newList1.add(tmp);
- }
- System.out.println(newList);
- System.out.println(newList1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement