SHARE
TWEET

Untitled

a guest Feb 17th, 2020 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.*;
  2. import java.util.stream.*;
  3.  
  4. public class Main
  5. {
  6.   public static void main(String[] args)
  7.   {
  8.     double[] sample = {20, 12, 34, 45, 11, 23, 45};
  9.     DoubleStream ds = Arrays.stream(sample);
  10.     System.out.println(ds.count());
  11.  
  12.     // Another example
  13.     ds = Arrays.stream(sample);
  14.     ds.forEach(System.out::println);
  15.  
  16.     System.out.println("In one statement ...");
  17.     Arrays.stream(sample).forEach(System.out::println);
  18.  
  19.     // Selection
  20.     long count =
  21.       Arrays.stream(sample).filter(value -> value >= 30).count();
  22.     System.out.println(count);
  23.  
  24.     Arrays.stream(sample)
  25.           .filter(value -> value >= 30)
  26.           .forEach(System.out::println);
  27.  
  28.     Arrays.stream(sample)
  29.           .filter(value -> value >= 30)
  30.           .forEach(Main::printOneLine);
  31.  
  32.     // Saving to array
  33.     double[] above30 =
  34.       Arrays.stream(sample)
  35.             .filter(v -> v > 30)
  36.             .toArray();
  37.  
  38.     System.out.println(above30.length);
  39.     for (int i = 0; i < above30.length; i++)
  40.       System.out.println(above30[i]);
  41.  
  42.   }
  43.   public static void printOneLine(double value)
  44.   {
  45.     System.out.print(value + " ");
  46.   }
  47. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top