Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have to Use the forEach method and a lambda expression to display the toString

ID: 3606576 • Letter: I

Question

I have to Use the forEach method and a lambda expression to display the toString method in the Person class.

Person class code:

public String toString() {

return firstName + " " + lastName + " age: " + age;

}

------------------------------------------------------------------------

This is what I have in TestMyArrayList but I don't how to add toString method to be displayed.

arrP.forEach(p -> System.out.println(p));

System.out.println();

Explanation / Answer

//loop through arrP arrP.forEach((person)->{ // person.toString() can be called explicitely otherwise passing person to println() will also call toString() method internally System.out.println(person.toString()); });