在Java中用Python的lambda函数等效?

时间:2021-06-17 20:30:28

Can someone please tell me if there is an equivalent for Python's lambda functions in Java?

有人可以告诉我,如果在Java中有Python的lambda函数吗?

6 个解决方案

#1


28  

Unfortunately, there are no lambdas in Java. However, you can get almost the same effect (in a really ugly way) with anonymous classes:

不幸的是,Java中没有lambdas。但是,您可以使用匿名类获得几乎相同的效果(以非常难看的方式):

interface MyLambda {
    void theFunc(); // here we define the interface for the function
}

public class Something {
    static void execute(MyLambda l) {
        l.theFunc(); // this class just wants to use the lambda for something
    }
}

public class Test {
    static void main(String[] args) {
        Something.execute(new MyLambda() { // here we create an anonymous class
            void theFunc() {               // implementing MyLambda
                System.out.println("Hello world!");
            }
        });
    }
}

Obviously these would have to be in separate files :(

显然这些必须在单独的文件中:(

#2


9  

I don't think there is an exact equivalent, however there are anonymous classes that are about as close as you can get. But still pretty different. Joel Spolsky wrote an article about how the students taught only Java are missing out on these beauties of functional style programming: Can Your Programming Language Do This?.

我不认为有一个确切的等价物,但是有一些匿名类可以尽可能接近。但还是差别很大。乔尔·斯波尔斯基(Joel Spolsky)写了一篇关于学生如何只教Java的文章,他们错过了这些功能风格编程的优点:你的编程语言可以做到吗?

#3


6  

One idea is based on a generic public interface Lambda<T> -- see http://www.javalobby.org/java/forums/t75427.html .

一个想法是基于通用公共接口Lambda - 请参阅http://www.javalobby.org/java/forums/t75427.html。

#4


1  

Somewhat similarly to Zifre's, you could create an interface thus

有点类似于Zifre的,你可以创建一个界面

public interface myLambda<In, Out> {
    Out call(In i);
}

to enable you to write, say

说,让你写

Function<MyObj, Boolean> func = new Function<MyObj, Boolean>() {
    public Boolean callFor(myObj obj) {
        return obj.canDoStuff();
    };

MyObj thing = getThing;

if (func.callFor(thing)) {
    doSomeStuff();
} else {
    doOtherStuff();
}

It's still a bit kludgy, yeah, but it has input/output at least.

它仍然有点kludgy,是的,但它至少有输入/输出。

#5


0  

Yes,

是,

Lambda expressions are introduced in java from java8.

Lambda表达式是从java8中引入的。

Basic syntax for lambda expressions are:

lambda表达式的基本语法是:

(parameters)->
{
  statements;
}

Example

(String s)->
{
System.out.println(s);
}

Check this link:

检查此链接:

http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html

http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html

#6


0  

With the release of Java 8, lambda-expression is now available. And the lambda function in java is actually "more powerful" than the python ones.

随着Java 8的发布,lambda-expression现在可用。并且java中的lambda函数实际上比python函数“更强大”。

In Python, lambda-expression may only have a single expression for its body, and no return statement is permitted. In Java, you can do something like this: (int a, int b) -> { return a * b; }; and other optional things as well.

在Python中,lambda-expression可能只有一个表达式,并且不允许使用return语句。在Java中,你可以这样做:(int a,int b) - > {return a * b; };和其他可选的东西。

Java 8 also introduces another interface called the Function Interface. You might want to check that out as well.

Java 8还引入了另一个称为函数接口的接口。您可能也想检查一下。

#1


28  

Unfortunately, there are no lambdas in Java. However, you can get almost the same effect (in a really ugly way) with anonymous classes:

不幸的是,Java中没有lambdas。但是,您可以使用匿名类获得几乎相同的效果(以非常难看的方式):

interface MyLambda {
    void theFunc(); // here we define the interface for the function
}

public class Something {
    static void execute(MyLambda l) {
        l.theFunc(); // this class just wants to use the lambda for something
    }
}

public class Test {
    static void main(String[] args) {
        Something.execute(new MyLambda() { // here we create an anonymous class
            void theFunc() {               // implementing MyLambda
                System.out.println("Hello world!");
            }
        });
    }
}

Obviously these would have to be in separate files :(

显然这些必须在单独的文件中:(

#2


9  

I don't think there is an exact equivalent, however there are anonymous classes that are about as close as you can get. But still pretty different. Joel Spolsky wrote an article about how the students taught only Java are missing out on these beauties of functional style programming: Can Your Programming Language Do This?.

我不认为有一个确切的等价物,但是有一些匿名类可以尽可能接近。但还是差别很大。乔尔·斯波尔斯基(Joel Spolsky)写了一篇关于学生如何只教Java的文章,他们错过了这些功能风格编程的优点:你的编程语言可以做到吗?

#3


6  

One idea is based on a generic public interface Lambda<T> -- see http://www.javalobby.org/java/forums/t75427.html .

一个想法是基于通用公共接口Lambda - 请参阅http://www.javalobby.org/java/forums/t75427.html。

#4


1  

Somewhat similarly to Zifre's, you could create an interface thus

有点类似于Zifre的,你可以创建一个界面

public interface myLambda<In, Out> {
    Out call(In i);
}

to enable you to write, say

说,让你写

Function<MyObj, Boolean> func = new Function<MyObj, Boolean>() {
    public Boolean callFor(myObj obj) {
        return obj.canDoStuff();
    };

MyObj thing = getThing;

if (func.callFor(thing)) {
    doSomeStuff();
} else {
    doOtherStuff();
}

It's still a bit kludgy, yeah, but it has input/output at least.

它仍然有点kludgy,是的,但它至少有输入/输出。

#5


0  

Yes,

是,

Lambda expressions are introduced in java from java8.

Lambda表达式是从java8中引入的。

Basic syntax for lambda expressions are:

lambda表达式的基本语法是:

(parameters)->
{
  statements;
}

Example

(String s)->
{
System.out.println(s);
}

Check this link:

检查此链接:

http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html

http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html

#6


0  

With the release of Java 8, lambda-expression is now available. And the lambda function in java is actually "more powerful" than the python ones.

随着Java 8的发布,lambda-expression现在可用。并且java中的lambda函数实际上比python函数“更强大”。

In Python, lambda-expression may only have a single expression for its body, and no return statement is permitted. In Java, you can do something like this: (int a, int b) -> { return a * b; }; and other optional things as well.

在Python中,lambda-expression可能只有一个表达式,并且不允许使用return语句。在Java中,你可以这样做:(int a,int b) - > {return a * b; };和其他可选的东西。

Java 8 also introduces another interface called the Function Interface. You might want to check that out as well.

Java 8还引入了另一个称为函数接口的接口。您可能也想检查一下。