java 如何在java中将函数作为参数传递

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48594418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 10:06:54  来源:igfitidea点击:

How to pass a function as a parameter in java

java

提问by eyal mazuz

I have a matrix class and I want to create a function called map in this class, that looks something like this

我有一个矩阵类,我想在这个类中创建一个名为 map 的函数,它看起来像这样

  public void map(T fn) {
      //do something
   }

the idea behind the function is that as a parameter fn, which could be any method and apply this method to each number in the matrix.

该函数背后的想法是作为参数 fn,它可以是任何方法并将此方法应用于矩阵中的每个数字。

the Idea behind it was for the coding train videos on a neural network but I tried to manipulate it in Java and it didn't work.I search lambda expressions but when I use it it's only let me pass method like Math.cos(arg) with an argument inside. but I want it to pass without any arguments the current function looks like this

它背后的想法是针对神经网络上的编码训练视频,但我尝试在 Java 中对其进行操作,但没有奏效。我搜索 lambda 表达式,但是当我使用它时,它只让我传递像 Math.cos(arg ) 里面有一个参数。但我希望它不带任何参数传递当前函数看起来像这样

interface lambda<T> {
double foo(T fn);
}

public void map(T fn) {
    for(int i = 0 ; i< this.getRow() ; i++) {
        for(int j = 0 ; j< this.getCol(); j++) {
            int a = i;
            int b = j;
            lambda mycode = (T) -> setData(matrix[a][b], fn);
            matrix[i][j] = mycode.foo(fn);
        }
    }
}
private double setData(double data, T fn) {
    data = (double) fn;
    return data;
}

when I send a call from my main class I can only call with arguments like

当我从主类发送调用时,我只能使用类似的参数调用

matrix.map(Math.cos(matrix.getData(0,0))

but it apply on each value in the matrix the cos of data at 0,0

但它适用于矩阵中的每个值数据的 cos 在 0,0

I wish it will look more like this

我希望它看起来更像这样

matrix.map(Math.cos())

or even

甚至

Matrix.map(function())

is there a way in java to pass methods as parameters without entering arguments in them?

java中有没有办法将方法作为参数传递而不在其中输入参数?

采纳答案by Shmulik Klein

Java 8 was shipped with enormous number of functional interfaces. You can use one of those to describe the expected function to be passed as parameter in your method.

Java 8 附带了大量的函数式接口。您可以使用其中之一来描述在您的方法中作为参数传递的预期函数。

In your case, for passing a function which excepts a single doubleparameter and applys a logic, use java.util.function.DoubleUnaryOperator:

在您的情况下,要传递除单个double参数之外的函数并应用逻辑,请使用java.util.function.DoubleUnaryOperator

static double foo(DoubleUnaryOperator fn){
    double aNumber = 2.0;  
    return fn.applyAsDouble(aNumber);
}

And then create a lambda (or an object which implements DoubleUnaryOperator) and pass it to the foomethod:

然后创建一个 lambda(或一个实现 的对象DoubleUnaryOperator)并将其传递给foo方法:

foo(d -> d * 3);

or

或者

public class Fn implements DoubleUnaryOperator {
    @Override
    public double applyAsDouble(double d) {
        return d * 3;
    }
}

回答by Ravi

Use :: operator, to pass method reference, such as

使用:: 运算符,传递方法引用,如

Math::cos

Also, you need to make sure, method where you are passing method reference, should accept functional interface

此外,您需要确保传递方法引用的方法应该接受功能接口

That said,

那说,

void map(Consumer<T> fn)

回答by Amit Bera

Functional programming in Java is backed by functional interfaces (interfaces with only one abstract method) and lamdas. There are several built-in functional interfaces provided by java. You can use Function/ DoubleFunctionthose interfaces has one abstract method Function#apply. You can also create your own functional interfaces but it is better to use the existing solution provided by API if it fulfills your requirements. Please refer Below is the code sample you may get clarity.

Java 中的函数式编程由函数式接口(只有一种抽象方法的接口)和 lamdas 支持。java提供了几个内置的函数式接口。你可以使用Function/ DoubleFunction这些接口有一个抽象方法 Function#apply。您也可以创建自己的功能接口,但如果满足您的要求,最好使用 API 提供的现有解决方案。请参考下面的代码示例,您可能会清楚。

public void map(DoubleFunction<Double> fn) {
  //do something
  setData(10.0, Math::cos);
 //do something
}
private double setData(double data,  DoubleFunction<Double> fn) {
    data = fn.apply(data);
    return data;
}