desislava_topuzakova

05. Reverse And Exclude

Sep 30th, 2021
1,112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Collections;
  3. import java.util.List;
  4. import java.util.Scanner;
  5. import java.util.function.*;
  6. import java.util.stream.Collectors;
  7.  
  8. public class FormulaOne_02 {
  9.     public static void main(String[] args) {
  10.         //Function<приема, връща> -> apply
  11.         //Consumer<приема> -> void -> accept
  12.         //Supplier<връща> -> get
  13.         //Predicate<приема> -> false / true -> test
  14.         //BiFunction<приема1, приема2, връща> -> apply
  15.         Scanner scanner = new Scanner(System.in);
  16.         String input = scanner.nextLine(); //"1 2 3 4 5" -> split -> ["1", "2", "3", "4", "5"]
  17.         List<Integer> numbers =  Arrays.stream(input.split("\\s+")).mapToInt(Integer::parseInt).boxed().collect(Collectors.toList());
  18.         int n = Integer.parseInt(scanner.nextLine());
  19.  
  20.         //обръщаме списъка
  21.         Collections.reverse(numbers);
  22.  
  23.         //премахваме всички числа, които се делят на n
  24.         //приема число -> true / false
  25.         Predicate<Integer> checkDivision = number -> number % n == 0;
  26.         numbers.removeIf(checkDivision);
  27.  
  28.         numbers.forEach(number -> System.out.print(number + " "));
  29.  
  30.  
  31.  
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment