Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug lambda expression in Java 8 using Eclipse?

I am trying to debug a simple Java application which is using Lambda Expression. I am not able to debug Lambda Expression using normal Eclipse debugger.

like image 945
IamVickyAV Avatar asked Sep 22 '16 10:09

IamVickyAV


People also ask

How do you debug a lambda expression?

We can use different IDE's like Netbeans, IntelliJ, and Eclipse to debug the lambda expressions in Java. It is always possible to create multi-line lambda expressions and use print statements to display the values of a variable. The debugger can also provide additional information about the state of a java program.

Is Java 8 lambda expression correct?

Q 6 - Which of the following is correct about Java 8 lambda expression? A - Using lambda expression, you can refer to final variable or effectively final variable (which is assigned only once).


2 Answers

It's late answer but hope it is useful for someone. I use this https://stackoverflow.com/a/24542150/10605477 but sometimes when code is a bit messy or I can't get data I just break the code and insert peek.

protected Optional<Name> get(String username) {
    return profileDao.getProfiles()             
            .stream()
            .filter(profile -> 
                    profile.getUserName().equals(username))
            .peek(data -> System.out.println(data))
            .findFirst();
}
like image 170
José Domingo Balderas Avatar answered Sep 21 '22 13:09

José Domingo Balderas


You can transform the expressions into statements.

List<String> list = new ArrayList<>();

// expression
boolean allMatch1 = list.stream().allMatch(s -> s.contains("Hello"));
// statement
boolean allMatch2 = list.stream().allMatch(s -> {
  return s.contains("Hello");
});

You can now set the break-point on the return s.contains("Hello"); line

like image 24
mrt181 Avatar answered Sep 24 '22 13:09

mrt181