Goal
Using Java 8 and its new feature Lambdas lets print each element of List to screen.
Setup
We will start with following code and fill in to a piece of logic to push elements on console.
@Test
    public void SimpleCollection() {
        List values = Arrays.asList(1, 2, 3, 4, 5, 6); 
        values.forEach(/* fill with lambda expression */);
    }
Goal
We will a lambda expression e -> {/** code block **/} to manage the output logic. Something like this
value -> {
            System.out.println(value);
        }
The complete solution looks like following
@Test
    public void SimpleCollection() {
        List values = Arrays.asList(1, 2, 3, 4, 5, 6); 
        values.forEach(value -> {
            System.out.println(value);
        });
    }
 
No comments:
Post a Comment