【问题标题】:Error when using lambda in java 1.8在 java 1.8 中使用 lambda 时出错
【发布时间】:2016-01-28 00:06:17
【问题描述】:
public static void main(String[] args) {
    List<Integer> list = Arrays.asList(4, 5, 6, 7);
    list.forEach(x -> {
        x = x * 9;
        System.out.println(x);
    });

    JButton btn = new JButton("aa");

    ActionListener ae = e -> {};
    btn.addActionListener(ae);

    List<Student> stds = new ArrayList<>();

    for(int i =0; i < 10; i++)
    {
        stds.add(new  Student((int)(Math.random()*100), (int)(Math.random()*100)));            
    }

    System.out.println("Original List");
    stds.forEach(x -> System.out.println(x));

    System.out.println("\nModified List"); 
    filter(stds,x->x.gpa>15).forEach(x -> System.out.println(x));

    System.out.println(convert(51,t->String.valueOf(t)));

}

这是我的讲师代码,它是 100% 正确的,但是当我在我的 PC 上运行它时它给了我错误。我认为问题是我的 IDE??

【问题讨论】:

  • 另外,学生也不见了。请添加。
  • 您确认您正在使用 Java 8 进行编译吗?

标签: java lambda


【解决方案1】:

我认为这是错误的。

System.out.println("\nModified List"); 
filter(stds,x->x.gpa>15).forEach(x -> System.out.println(x));

你过滤什么?您可以在流中使用过滤器。 例如:

list.stream().filter(your predicate).forEach(do something);

我想你想要这样的东西:

stds.stream().filter(x->x.gpa>15).forEach(x -> System.out.println(x));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-07
    • 2023-03-05
    • 2017-06-03
    • 2021-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多