简体   繁体   中英

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

I wrote the following lambda expression

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

The variable size is computed correctly!

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:

"Lambda expressions cannot be used in an evaluation expression"

在此输入图像描述

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

A simple solution for this, that works is to convert your lambda in an anonymous class creation. 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:

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

I hope it helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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