deyanmalinov

01. Take Two --- Stream API ---

Apr 21st, 2020
138
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package DPM;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import java.util.Scanner;
  5. import java.util.stream.Collectors;
  6.  
  7. public class Main {
  8.     public static void main(String[] args) {
  9.         Scanner scan = new Scanner(System.in);
  10.         List<Integer> nums = Arrays.stream(scan.nextLine()
  11.                 .split(" "))
  12.                 .map(x->Integer.parseInt(x))
  13.                 .collect(Collectors.toList());
  14.         nums.stream().
  15.                 filter(num -> 10 <= num && num <= 20)
  16.                 .distinct()
  17.                 .limit(2)
  18.                 .forEach(x -> System.out.print(x + " "));
  19. ------------------------------------------------------------
  20. Arrays.stream(scan.nextLine()
  21.                 .split(" "))
  22.                 .map(x->Integer.parseInt(x))
  23.                 .collect(Collectors.toList()).stream().
  24.                 filter(num -> 10 <= num && num <= 20)
  25.                 .distinct()
  26.                 .limit(2)
  27.                 .forEach(x -> System.out.print(x + " "));
  28. --------------------------------------------------------------
  29.     }
  30. }
RAW Paste Data