简体   繁体   English

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

[英]How to pass a function as a parameter in java

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. 它背后的想法是在神经网络上编码火车视频,但我试图用Java操作它,但没有用。我搜索了lambda表达式,但是当我使用它时,只让我传递了Math.cos(arg ),里面有一个参数。 but I want it to pass without any arguments the current function looks like this 但我希望它传递时不带任何参数,当前函数如下所示

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 但它适用于矩阵中每个值的数据余弦为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中有没有一种方法可以将方法作为参数传递而无需在其中输入参数?

Java 8 was shipped with enormous number of functional interfaces . Java 8附带了大量的功能接口 You can use one of those to describe the expected function to be passed as parameter in your method. 您可以使用其中之一来描述要在方法中作为参数传递的预期函数。

In your case, for passing a function which excepts a single double parameter 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 foo method: 然后创建一个lambda(或实现DoubleUnaryOperator的对象)并将其传递给foo方法:

foo(d -> d * 3);

or 要么

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

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)

Functional programming in Java is backed by functional interfaces (interfaces with only one abstract method) and lamdas. Java中的函数式编程由函数接口(仅具有一种抽象方法的接口)和lamda支持。 There are several built-in functional interfaces provided by java. java提供了一些内置的功能接口。 You can use Function / DoubleFunction those interfaces has one abstract method Function#apply. 您可以使用Function / DoubleFunction,这些接口具有一种抽象方法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. 您也可以创建自己的功能接口,但是如果满足您的要求,最好使用API​​提供的现有解决方案。 Please refer Below is the code sample you may get clarity. 请参考下面的代码示例,您可能会清楚。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM