有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为一个非常基本的类编写lambda表达式

有两个功能接口

public interface BoolBiFunction {
boolean apply( int p1, int p2 );
}


public interface IntFunction {
int apply(int p);
}

接下来的课呢

public class IntData {
private int[] intValues;

public IntData(int[] p) {
    intValues = p;
}

public int compute(BoolBiFunction f, IntFunction g, int x) {
    int result = 0;
    for (int value : intValues) {
        if (f.apply(value, x)) {
            result = result + g.apply(value);
        }
    }
    return result;
}

任务是使用参数,因此compute会告诉一个数字在字段intValues中出现的频率。 但说实话,我真的不明白给定的计算方法到底做了什么

if ( f.apply( value, x ) )
{
result = result + g.apply( value );
 }

f似乎比较了这两个整数,但g.apply到底做了什么呢? 用于检查一个数字在字段intValues中出现的频率的lambda表达式会是怎样的呢


共 (3) 个答案

  1. # 1 楼答案

    由于您没有提供两个给定lambda的具体实现,我想用一个简单的例子来解释:

    要调用compute()方法,我们需要BoolBiFunctionIntFunction的实现

    BoolBiFunction f = (p1, p2) -> p1>p2;
    IntFunction g = p -> p*2;
    

    然后我们初始化xintValues

    x = 1;
    intValues= {1, 3, 5};
    

    调用compute()方法

    int result = compute(f, g, x);
    System.out.println(result);
    

    输出:

    16
    

    for循环的每个步骤中的值:

    value       x       f.apply(value,x)        g.apply(value)      result
    1           1       false                   -                   0
    3           1       true                    6                   0+6 = 6
    5           1       true                    10                  6+10 = 16
    

    在这个例子中g.apply(value)返回value*2

    因为Intfunction的实现是p -> p*2

  2. # 2 楼答案

    这个问题在我看来有点奇怪。但是,这里有一个解决方案

    查看代码注释以了解发生了什么。我提供了一个额外的例子,说明如何使用相同的计算(…)函数来确定给定数字在该数组中出现的次数

    public class Temp {
        static int [] testData = {2, 5, 11, 2, 2, 7, 6, 3, 1, 9, 10, 6};
    
        public static void main(String [] args){
            answerToQuestion();
            //extraQuestionForYou();
        }
    
        public static void answerToQuestion(){
            //How many times does 2 occur in testData ? Answer is 3. Confirm it!
    
            IntData intFinder = new IntData(testData);
            int desiredNumber = 2;
    
            int times = intFinder
                    //Find out how many times desiredNumber occurs in an array.
                    .compute(
                    //A function which tells me if one number is equal to another.
                    (number1, number2) -> number1 == number2,
                    //A function which says that it saw a number one more time, every time it sees that number.
                    someNumber -> 1,
                    //The number whose occurrence we want to find in an array.
                    desiredNumber
                    );
    
            //Testing - Runs only when assertions are enabled for your JVM. Set VM args = -ea for your IDE.
            assert times == 3 : "Expected " + desiredNumber + " to occur 3 times, but got " + times;
        }
    
    }
    

    计算函数内部发生了什么

    f.应用(值,x):

    f = (number1, number2) -> number1 == number2.
    f.apply(value, x) means number1 = value and number2 = x.
    

    result=result+g.apply(值):

    g = someNumber -> 1.
    g.apply(value) means return 1 whenever you see value.
    

    我们可以重用compute()方法来做其他事情,比如除了最初的问题之外,还可以找到给定数字的三元组在数组中出现的次数。有两种方法可以做到这一点——使用上述代码并搜索6,或者使用bi函数检查一个数字是否是另一个数字的三倍。我们在下面的代码中使用后一种方法。如果你愿意,你可以想出一个更好的例子

    public static void extraQuestionForYou(){
        //How many times does triple of 2 occur in testData ? Answer is 2. Confirm it!
    
        IntData intFinder = new IntData(testData);
        int desiredNumber = 2;
    
        int times = intFinder
                //Find out how many times triple of desiredNumber occurs in an array.
                .compute(
                        //A function which tells me if one number is equal to triple of another.
                        (number1, number2) -> number1 * 3 == number2,
                        //A function which says that it saw a number one more time, every time it sees that number.
                        someNumber -> 1,
                        //The number whose "triples" we want to find in an array.
                        desiredNumber
                );
    
        //Testing - Runs only when assertions are enabled for your JVM. Set VM args = -ea for your IDE.
        assert times == 2 : "Expected " + desiredNumber + " to occur 2 times, but got " + times;
    }
    
  3. # 3 楼答案

    你的compute方法接受一个BoolBiFunction测试两个int值是否满足某些条件。如果它们满足条件(即如果f.apply(value, x)true),则对当前值应用IntFunction的结果被添加到总数中

    例如,如果想知道intValues数组中有多少个元素等于5,必须调用:

    IntData someObject = ...
    int numberOf5s = someObject.compute((x,y) -> x == y,
                                        x -> 1,
                                        5);
    

    这意味着当且仅当传递给它的两个int相等时,BoolBiFunction将返回true

    IntFunction将始终返回1,因此它将计算在intValues数组中找到第三个参数-5的次数

    给定传递给compute()的上述参数,如果我们用相应lambda表达式的实际体替换对f.applyg.apply的调用,我们将得到:

    public int compute(BoolBiFunction f, IntFunction g, int x) {
        int result = 0;
        for (int value : intValues) {
            if (value == 5) { // which is equivalent to if (f.apply(value, x)) when x == 5
                result = result + 1; // which is equivalent to result = result + g.apply(value);
            }
        }
        return result;
    }