Java - Add to a string within a Lambda expression

Viewed 6257

I'm quite new to Java, I tried looking around on StackOverflow/Google but couldn't find an answer to my problem.

Problem: I have a String with the name of 's' that I have set to a sentence. Then, I want to use the Lambda .forEach loop to iterate over a list of objects, retrieving the toString() from the objects and adding it to this 's' String.

This is my code:

public String toString() {
    String s =  "In klas " + this.klasCode + " zitten de volgende leerlingen:\n";
    deLeerlingen.forEach(leerlingen -> {
        s += leerlingen.toString();
    });
    return s;
}
2 Answers
Related