Java Code Example: consumer

Here, we use a lambda expression (name) -> System.out.println("Hello, " + name) as a Consumer<String> functional interface. The lambda expression takes a String argument and prints a greeting message. We use the forEach method on the names list to apply the lambda expression to each element in the list.

List<String> names = Arrays.asList("Thomas", "Johann", "Sarah");
names.forEach((name) -> System.out.println("Hello, " + name));