简体   繁体   English

Java / Eclipse:“Lambda表达式不能用于评估表达式”

[英]Java/Eclipse: “Lambda expressions cannot be used in an evaluation expression”

I wrote the following lambda expression 我写了下面的lambda表达式

int size = ((List<?>) receipt.getPositions().stream().filter(item -> true).collect(Collectors.toList())).size()

The variable size is computed correctly! 可变size正确计算!

But when I try to inspect it ( Ctrl + Shift + I ) or try to see the result of the expression in Eclipse expressions view, I get the following error: 但是当我尝试检查它( Ctrl + Shift + I )或尝试在Eclipse表达式视图中查看表达式的结果时,我收到以下错误:

"Lambda expressions cannot be used in an evaluation expression" “Lambda表达式不能用于评估表达式”

在此输入图像描述

Are there any other opportunities to see the result of such an expression instead of storing it to a variable? 有没有其他机会看到这样的表达式的结果而不是将其存储到变量?

PS: I am using Java 8 and Eclipse neon.2 PS:我使用的是Java 8和Eclipse neon.2

A simple solution for this, that works is to convert your lambda in an anonymous class creation. 一个简单的解决方案,可以在匿名类创建中转换lambda。 Creating a variable of the interface needed, you'll get a debuggable expression. 创建所需接口的变量,您将获得可调试的表达式。

    Predicate<Object> predicate = new Predicate<Object>() {
        @Override
        public boolean test(Object item) {
            return true;
        }
    };
    int size = ((List<?>) receipt.getPositions().stream().filter(predicate).collect(Collectors.toList())).size();

Now, if you're standing at the line "int size = ..." while debugging, you can view the result of following: 现在,如果您在调试时站在“int size = ...”行,您可以查看以下结果:

((List<?>) receipt.getPositions().stream().filter(predicate).collect(Collectors.toList())).size()

I hope it helps :) 我希望它有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM