desislava_topuzakova

09. List Of Predicates

Sep 30th, 2021
1,031
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.60 KB | None | 0 0
  1. import javax.print.attribute.IntegerSyntax;
  2. import java.util.*;
  3. import java.util.function.*;
  4. import java.util.stream.Collectors;
  5.  
  6. public class FormulaOne_02 {
  7.     public static void main(String[] args) {
  8.         //Function<приема, връща> -> apply
  9.         //Consumer<приема> -> void -> accept
  10.         //Supplier<връща> -> get
  11.         //Predicate<приема> -> false / true -> test
  12.         //BiFunction<приема1, приема2, връща> -> apply
  13.         Scanner scanner = new Scanner(System.in);
  14.         int n = Integer.parseInt(scanner.nextLine());
  15.         List<Integer> numbersForDivision = Arrays.stream(scanner.nextLine().split("\\s+")).map(Integer::parseInt).collect(Collectors.toList());
  16.  
  17.         //приема лист с числа и число -> връща true/ false
  18.         //true -> числото се дели на всички числа
  19.         //false -> числото не се дели на всички числа
  20.         BiFunction<List<Integer>, Integer, Boolean> isDivisible = ((list, number) -> {
  21.             for (int numberInList : list) {
  22.                 if (number % numberInList != 0) {
  23.                     return false;
  24.                 }
  25.             }
  26.             return true;
  27.         });
  28.  
  29.  
  30.         //всички числа от 1 до n
  31.         for (int number = 1; number <= n; number++) {
  32.             //проверка дали се дели на всички дадени числа
  33.             if(isDivisible.apply(numbersForDivision, number)) {
  34.                 System.out.print(number + " ");
  35.             }
  36.  
  37.         }
  38.  
  39.     }
  40.  
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment