小编Rau*_*orn的帖子

使用Java 8 Predicate查找"最"正确的值

并感谢您查看我的问题!

我在使用Streams并且按特定顺序应用多个谓词时遇到了一些麻烦.

例如,请考虑以下IntPredicates:

        IntPredicate divisibleByThree = i -> i % 3 == 0;
        IntPredicate divisibleByFour = i -> i % 4 == 0;
        IntPredicate divisibleByFive = i -> i % 5 == 0;
        IntPredicate divisibleByThreeAndFour = divisibleByThree.and(divisibleByFour);
        IntPredicate divisibleByThreeAndFive = divisibleByThree.and(divisibleByFive);
        IntPredicate divisibleByThreeAndFiveAndFour = divisibleByThreeAndFour.and(divisibleByFive);
        //....arbitrary Number of predicates.
Run Code Online (Sandbox Code Playgroud)

第1部分

我已将我遇到的问题转换为"FizzBu​​zz"-esque版本,试图通过将谓词应用于特定顺序的流来找到正确的答案.像这样:

    IntStream.range(1, 100).forEach(i -> {
        //Order matters here!
        if(divisibleByThreeAndFiveAndFour.test(i)){
            System.out.println("Three and four and five");
        } else if(divisibleByThreeAndFour.test(i)){
            System.out.println("Three and four");
        } else if(divisibleByThreeAndFive.test(i)){
            System.out.println("Three and four");
        } else if(divisibleByFive.test(i)){
            System.out.println("Five"); …
Run Code Online (Sandbox Code Playgroud)

java functional-programming java-8 vavr

3
推荐指数
1
解决办法
190
查看次数

java 8闭包存储在哪里?

我编写了这个类,可以使用构建器模式构建类型为T的数组,将值存储在闭包中,直到实际构造数组.

public class ArrayBuilder<T> {

    final Class<T> type;

    public ArrayBuilder(Class<T> type){
        this.type = type;
    }

    private Supplier<Supplier<Store>> start = () -> {
        final Store element = new Store(-1, null, null);
        return () -> element;
    };

    private class Store {
        final Integer index;
        final T val;
        final Supplier<Store> getNextVal;

        public Store(Integer index, T val, Supplier<Store> getNextVal) {
            this.index = index;
            this.val = val;
            this.getNextVal = getNextVal;
        }
    }

    private Supplier<Store> queue(Integer index, T value, Supplier<Store> next) {
        final Store element = …
Run Code Online (Sandbox Code Playgroud)

java closures java-8

2
推荐指数
1
解决办法
324
查看次数

标签 统计

java ×2

java-8 ×2

closures ×1

functional-programming ×1

vavr ×1