Java 简单教程示例(lambda 表达式)无法运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18406268/
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
Simple tutorial example (lambda expression) doesn't run
提问by kleopatra
Finally decided to start a bit of experimentation on the new features of jdk8, namely the lambda expressions following the tutorial. For convenience, I stripped-down the example, see SSCCE below.
最后决定开始对 jdk8 的新特性进行一些实验,即教程之后的 lambda 表达式。为方便起见,我精简了示例,请参阅下面的 SSCCE。
Typing out the predicate just runs fine, refactoring it to a lambda expression as suggested (and actually done) by Netbeans compiles (?) without errors, but doesn't run. The laconic console printout is
键入谓词运行良好,按照 Netbeans 的建议(并实际完成)将其重构为 lambda 表达式,编译 (?) 没有错误,但无法运行。简洁的控制台打印输出是
Fehler: Hauptklasse simple.Simple konnte nicht gefunden oder geladen werden
Fehler: Hauptklasse simple.Simple konnte nicht gefunden oder geladen werden
("Error: main class couldn't be found or loaded")
(“错误:无法找到或加载主类”)
Environment:
环境:
- jdk: jdk-8-ea-bin-b102-windows-i586-08_aug_2013.exe
- Netbeans 7.4 beta, bundle from 14.7.2013. Not sure if that's the latest, couldn't download from the Netbeans site (got a "content encoding error" when clicking on its download link)
- jdk:jdk-8-ea-bin-b102-windows-i586-08_aug_2013.exe
- Netbeans 7.4 测试版,从 14.7.2013 开始捆绑。不确定这是否是最新的,无法从 Netbeans 站点下载(单击其下载链接时出现“内容编码错误”)
BTW, thought of using Netbeans only because it already has support for jdk8 (if not netbeans, who else ;-) - the eclipse beta preview from efxclipse has a similar issue (compiling but not running the example). So being definitely out off my comfort zone, possibly some very stupid mistake on my part... ?
顺便说一句,考虑使用 Netbeans 只是因为它已经支持 jdk8(如果不是 netbeans,还有谁;-) - efxclipse 的 eclipse beta 预览版也有类似的问题(编译但不运行示例)。所以肯定是在我的舒适区之外,我可能会犯一些非常愚蠢的错误......?
package simple;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
public class Simple {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
List<Member> members = createMembers();
// implement predicate directly, runs fine
// Predicate<Member> predicate = new Predicate<Member>() {
// public boolean test(Member member) {
// return member.getAge() >= 18;
// }
// };
// predicate converted to lambda, fails to run
// "class couldn't be found"
Predicate<Member> predicate = (Member member) -> member.getAge() >= 18;
for (Member member : members) {
if (predicate.test(member)) {
member.printMember();;
}
}
}
public static class Member {
private String name;
private int age;
public Member(String name, int age) {
this.name = name;
this.age = age;
}
public int getAge() {
return age;
}
public void printMember() {
System.out.println(name + ", " + getAge());
}
}
private static List<Member> createMembers() {
List<Member> members = new ArrayList<>();
members.add(new Member("Mathilda", 45));
members.add(new Member("Clara", 15));
members.add(new Member("Gloria", 18));
return members;
}
}
采纳答案by Holger
Your code example lacks the printMembers() method, however this seems not to be the real problem. You were running into a Netbeans bug I ran into today as well. It seems that the Netbeans developer marked this over four years old bug as “resolved” but the solution is still the same: you have to turn off the “Compile on Save” options in the settings (Project Properties/Build/Compiling) and then compile manually (e.g. pressing F9). Then it works.
您的代码示例缺少 printMembers() 方法,但这似乎不是真正的问题。您遇到了我今天也遇到的 Netbeans 错误。似乎 Netbeans 开发人员将这个超过四年的错误标记为“已解决”,但解决方案仍然相同:您必须关闭设置(项目属性/构建/编译)中的“编译时保存”选项,然后手动编译(例如按 F9)。然后它起作用了。
With the option not disabled you will see the class files being created when saying “Clean and Build” but suddenly disappear when telling Netbeans to run the code.
如果未禁用该选项,您将在说“清理并构建”时看到正在创建的类文件,但在告诉 Netbeans 运行代码时突然消失。
As a next step I suggest the code improvement by Josh M which is how you really ought to use lambdas.
作为下一步,我建议 Josh M 改进代码,这就是您真正应该使用 lambda 的方式。
回答by Josh M
That is not necessary, you should try doing something like this:
那不是必需的,您应该尝试执行以下操作:
members.stream().filter(m -> m.getAge() >= 18).forEach(Member::printMember);
members.stream().filter(m -> m.getAge() >= 18).forEach(Member::printMember);