Lambda 表达式示例在 Java 8 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23068305/
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
Lambda Expression example not working in java 8
提问by DCoder
I am trying to learn Lambda Expression in java 8. I do have installed eclipse plugin and java 8 SDK but when I am trying to execute following code eclipse is showing error.
我正在尝试在 java 8 中学习 Lambda 表达式。我确实安装了 eclipse 插件和 java 8 SDK,但是当我尝试执行以下代码时,eclipse 显示错误。
(String s) -> {
s="hello";
System.out.println(s); }
its showing "The left-hand side of an assignment must be a variable" error.
它显示“赋值的左侧必须是变量”错误。
please help.
请帮忙。
采纳答案by Sotirios Delimanolis
A lambda expression (and a method reference) only makes sense in a context that expects an instance of some functional interface. It would otherwise be impossible to determine if the lambda is valid (and it would also be useless as you don't do anything with it).
一个 lambda 表达式(和一个方法引用)只在需要某个函数接口实例的上下文中才有意义。否则将无法确定 lambda 是否有效(并且它也无用,因为您不对其进行任何操作)。
Something like
就像是
Consumer<String> c = (String s) -> {
s = "hello";
System.out.println(s);
}; // as a Consumer, it doesn't really make sense for you to change s
Note that as a Consumer
, it doesn't really make sense to reassign the value of s
.
请注意,作为Consumer
,重新分配 的值实际上没有意义s
。
回答by Felix
I know this answer is coming long after the question was asked, but may help others wondering about the same problem as I did recently, while learning about lambdas, and because your question is valid for a novice misguided by plenty of poorly presented examples on the web.
我知道这个答案是在提出这个问题很久之后才出现的,但可能会帮助其他人在学习 lambdas 时对我最近遇到的同样问题感到疑惑,并且因为您的问题对于被大量呈现不佳的示例误导的新手有效网。
The reason your compiler is complaining is - the example is incomplete. One way to understand the incomplete code, is that it is only the functioning portion of a context (that the example is missing) which typically can be an interface of your own design or one of the simple ones predefined in Java 8 - such as Function, BiFunction, Predicate, etc. Also, supplying the value "hello" to your String s inside the lambda {working section} somewhat beats the purpose of the s variable. Here is a more complete example of similar functionality demonstrating use of 3 String variables in lambda expression:
您的编译器抱怨的原因是 - 示例不完整。理解不完整代码的一种方法是,它只是上下文的功能部分(缺少示例),通常可以是您自己设计的接口或 Java 8 中预定义的简单接口之一 - 例如 Function 、BiFunction、Predicate 等。此外,在 lambda {working section} 中为您的 String s 提供值“hello”在某种程度上超出了 s 变量的目的。这是一个更完整的类似功能示例,演示了 3 个字符串变量在 lambda 表达式中的使用:
public class LambdaTest {
// define your interface
interface Use_3_Strings {
void apply(String s1, String s2, String s3);
}
// define a test method
public void lambdaTest1(String str1, String str2, String str3) {
// your lambda expression
// defining what to do inside { }
// - like a method having 3 String parameters (s1, s2, s3)
Use_3_Strings use3strings = (s1, s2, s3) -> { System.out.println("Working with 3 strings:");
System.out.println(" String 1: " + s1);
System.out.println(" String 2: " + s2);
System.out.println(" String 3: " + s3);
StringBuilder sb = new StringBuilder();
sb.append("CSV of 3 strings:\n");
sb.append(s1);
sb.append(", ");
sb.append(s2);
sb.append(", ");
sb.append(s3);
System.out.println(sb); };
// your lambda expression in action
// executing what you coded inside { } above
use3strings.apply(str1, str2, str3);
}
}
In your main() method then do:
在您的 main() 方法中,然后执行:
LambdaTest lambdaTst = new LambdaTest();
lambdaTst .lambdaTest1("Hello", "beautiful", "world");
and you should get:
你应该得到:
Working with 3 strings:
String 1: Hello
String 2: beautiful
String 3: world
CSV of 3 strings:
Hello, beautiful, world
回答by Divije Narasimhachar
The lambda expressions always expects a reference to be assigned.
lambda 表达式总是期望分配一个引用。
Java 7 Code:
Java 7 代码:
Valid:
有效的:
new Hahaha() {
@Override
public void howtolaugh(int number, String sound) {
System.out.println("say "+sound + number + " number of times");
}
};
Java 8:
爪哇 8:
Invalid:
无效的:
(int i, String sound) -> System.out.println("say "+sound + i + " number of times");
Though these two serve the same purpose, the first one is an inner class. Inner classes have a default reference to the enclosing class. When they are decompiled, a different class file is generated for them in the format <Enclosing_Class_Name>$<a_number>.class
or <Enclosing_Class_Name>$<a_number>.class
. Ex: TestClass$1.class.
尽管这两个目的相同,但第一个是内部类。内部类具有对封闭类的默认引用。当它们被反编译时,会为它们生成一个不同的类文件,格式为<Enclosing_Class_Name>$<a_number>.class
或<Enclosing_Class_Name>$<a_number>.class
. 例如:TestClass$1.class。
This is not the case in lambda expressions. They do not have an implicit reference to the enclosing class neither do they have a different class generated. They need an explicit reference either as a method parameter name, or a reference object on the left hand side or a return reference.
在 lambda 表达式中不是这种情况。它们没有对封闭类的隐式引用,也没有生成不同的类。它们需要一个显式引用作为方法参数名称,或者左侧的引用对象或返回引用。