Advertisement
desislava_topuzakova

05. Reverse And Exclude

Jan 25th, 2022
1,031
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package FunctionalProgramming_Exercise;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Collections;
  5. import java.util.List;
  6. import java.util.Scanner;
  7. import java.util.function.Predicate;
  8. import java.util.stream.Collectors;
  9.  
  10. public class ReverseAndExclude_05 {
  11.     public static void main(String[] args) {
  12.         Scanner scanner = new Scanner(System.in);
  13.         //Function<приема, връща> -> apply
  14.         //Consumer<приема> -> void -> accept
  15.         //Supplier<връща> -> get
  16.         //Predicate<приема> -> връща true / false -> test
  17.         //BiFunction<приема1, приема2, връща> -> apply
  18.         List<Integer> numbers = Arrays.stream(scanner.nextLine().split("\\s+"))
  19.                 .map(Integer::parseInt)
  20.                 .collect(Collectors.toList()); //{1, 2, 3, 4, 5, 6}
  21.         int n = Integer.parseInt(scanner.nextLine());
  22.         //reverse on list
  23.         Collections.reverse(numbers); //{1, 2, 3, 4, 5, 6} -> {6, 5, 4, 3, 2, 1}
  24.  
  25.         //remove all numbers which are divisible on n
  26.         //приема число -> true / false
  27.         Predicate<Integer> checkDivision = number -> number % n == 0;
  28.         numbers.removeIf(checkDivision);
  29.  
  30.         numbers.forEach(number -> System.out.print(number + " "));
  31.     }
  32. }
  33.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement