Advertisement
sergAccount

Untitled

Mar 28th, 2021
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package com.mycompany.app21;
  7.  
  8. import java.util.ArrayList;
  9. import java.util.stream.Stream;
  10.  
  11. /**
  12.  *
  13.  * @author Admin
  14.  */
  15. public class Main2 {
  16.     //
  17.     public static void main(String[] args) {
  18.         ArrayList<String> list = new ArrayList<>();
  19.         list.add("ONE");
  20.         list.add("TWO");
  21.         // forEach        
  22.         Stream<String> s = list.stream();
  23.         // используем лямбда выражение для вывода элементов потока на экран
  24.         s.forEach(x -> System.out.println(x));
  25.         //          
  26.         list.stream().forEach(x -> System.out.println(x));
  27.         // используем метод filter
  28.         System.out.println("\nFILTER:");
  29.         list.stream().filter(x -> x.startsWith("ONE")).forEach(x -> System.out.println(x));
  30.        
  31.         System.out.println("\nFILTER2:");
  32.         // цепочка фильтров
  33.         list.stream().filter(x -> x.startsWith("ONE"))
  34.                      .filter(x -> x.startsWith("ONE")).forEach(x -> System.out.println(x));
  35.     }
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement