Can someone please tell me if there is an equivalent for Python\'s lambda functions in Java?
Unfortunately, there are no lambdas in Java until Java 8 introduced Lambda Expressions. However, you can get almost the same effect (in a really ugly way) with anonymous classes:
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 :(