Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement