Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package com.mycompany.app9;
- import java.util.function.Predicate;
- import java.util.stream.Stream;
- public class Main2 {
- public static boolean testCondition(String t) {
- return t.startsWith("F");
- }
- //
- public static void main(String[] args) {
- //
- Stream<String> s5 = Stream.of("ONE", "TWO", "THREE", "FOUR");
- // кол-во элементов потока
- long count = s5.count();
- System.out.println("count=" + count);
- // skip - пропуск n элементов
- System.out.println("skip:");
- Stream.of("ONE", "TWO", "THREE", "FOUR").skip(2).forEach(System.out::println);
- // limit - выборка n элементов
- System.out.println("limit:");
- Stream.of("ONE", "TWO", "THREE", "FOUR").limit(1).limit(3).forEach(System.out::println);
- // метод filter
- System.out.println("метод filter:");
- Stream.of("ONE", "TWO", "THREE", "FOUR").filter(s -> s.startsWith("O")).forEach(System.out::println);
- //
- Predicate<String> p1 = s -> s.startsWith("O");
- Predicate<String> p2 = new MyCheck();
- //
- System.out.println("метод filter2:");
- Stream.of("ONE", "TWO", "THREE", "FOUR").filter(p2).forEach(System.out::println);
- // ссылка на метод - используем оператор ::
- System.out.println("метод filter3:");
- // Main2::testCondition - ссылка на статический метод testCondition
- Stream.of("ONE", "TWO", "THREE", "FOUR").filter(Main2::testCondition).forEach(System.out::println);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement