Advertisement
sergAccount

Untitled

Jan 30th, 2021
585
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 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.app6;
  7.  
  8. import java.util.Arrays;
  9. import java.util.stream.DoubleStream;
  10. import java.util.stream.IntStream;
  11.  
  12. public class Main {
  13.     //
  14.     public static void doSomething(int value){
  15.         System.out.println("doSomething=" + value);
  16.     }
  17.     //
  18.     public static void main(String[] args) {
  19.        
  20.         System.out.println("EX1:");
  21.         int[] arr = new int[]{1, 2, 3};
  22.         IntStream si = Arrays.stream(arr);
  23.         si.forEach(x -> {System.out.println(x);}); // лябда-выражение в качестве параметра метода
  24.         //
  25.         System.out.println("EX2:");
  26.         Arrays.stream(new int[]{1, 2, 3}).forEach(x -> {System.out.println(x);});        
  27.         // использование ссылки на метод - оператор ::
  28.         System.out.println("EX3:");
  29.         Arrays.stream(new int[]{1, 2, 3}).forEach(System.out::println); // System.out::println - ссылка на метод println
  30.         //
  31.         System.out.println("EX4:");
  32.         DoubleStream si2 = Arrays.stream(new double[]{1.0, 2, 3});
  33.         // filter
  34.         si2.filter(x -> x>2).filter(x -> x>3).forEach(System.out::println);
  35.         System.out.println("EX5:");
  36.         Arrays.stream(new int[]{1, 2, 3}).forEach(Main::doSomething);
  37.  
  38.        
  39.     }
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement