Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"

enter image description here

Are there any other opportunities to see the result of such an expression instead of storing it to a variable?

P.S.: I am using Java 8 and Eclipse neon.2

like image 982
mrbela Avatar asked Feb 20 '17 13:02

mrbela


1 Answers

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 :)

like image 189
Janos Vinceller Avatar answered Nov 14 '22 18:11

Janos Vinceller