19

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

5
  • 1
    If you use an internet search engine, the error message "Lambda expressions cannot be used in an evaluation expression" will find some information about that. Basic answer is no, but maybe in the future yes. Commented Feb 20, 2017 at 14:22
  • 2
    Also: why not just use count() instead of collect(...).size() ? Commented Feb 20, 2017 at 14:28
  • Just use receipt.getPositions().size(). This can be evaluted by the debugger without problems.
    – Holger
    Commented Feb 21, 2017 at 19:18
  • 1
    Just for reference and later use: Seems to be this bug in jdt: bugs.eclipse.org/bugs/show_bug.cgi?id=448473
    – timguy
    Commented Jan 17, 2019 at 12:58
  • 1
    you need to upgrade you version of eclipse to IntelliJ :p
    – ihebiheb
    Commented Jan 17, 2020 at 17:00

1 Answer 1

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

1
  • This works for the original question but doesn't help you if you tried to write a lambda inside a detail formatter or the debug view. You can't create your own predicate because you'll get "Anonymous type declaration cannot be used in an evaluation expression/Constructor of a local type cannot be used in an evaluation expression". You just have to use regular Java logic, I'm afraid.
    – Noumenon
    Commented Feb 18, 2020 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.