Stream Peek

During combining multiple Intermediate operations, it's useful to debug the input of an operation or output of an operation, for this purpose, the Stream class provides a Peek method.

Peek is an intermediate operation hence can be inserted after any intermediate operation and log the values, it's mostly used for debugging purposes. it takes Consumers as an input stream, prints or logs it, and then creates a new output Stream with the same elements as the input stream.

package org.wesome.java8;

import java.util.Arrays;
import java.util.List;

class Apple {
    public static void main(String args[]) {
        List<String> apples = Arrays.asList("Macintosh", "Fuji", "Gala", "Jonagold");
        System.out.println("*---------------------------Traditional method---------------------------*");
        for (String apple : apples) {
            System.out.println("initial apple is = " + apple);
            if (apple.length() > 4) {
                System.out.println("length should be greater then 4 = " + apple);
                apple = apple.toUpperCase();
                System.out.println("after upper case = " + apple);
                System.out.println("finally apple is = " + apple);
            }
        }

        System.out.println("*---------------------------Peak---------------------------*");
        apples.stream()
                .peek(apple -> System.out.println("initial apple is = " + apple))
                .filter(s -> s.length() > 4)
                .peek(apple -> System.out.println("length should be greater then 4 = " + apple))
                .map(String::toUpperCase)
                .peek(apple -> System.out.println("after upper case = " + apple))
                .forEach(apple -> System.out.println("finally apple is = " + apple));
    }
}

 

The peek is an intermediate operation hence only works with the terminal operation.

package org.wesome.java8;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

class Apple {
    public static void main(String args[]) {
        List<String> apples = Arrays.asList("Macintosh", "Fuji", "Gala", "Jonagold");

        System.out.println("*---------------------------Peak with out terminal operation---------------------------*");
        apples.stream().peek(apple -> System.out.println("initial apple is = " + apple));

        System.out.println("*---------------------------Peak with terminal operation---------------------------*");
        apples.stream().peek(apple -> System.out.println("initial apple is = " + apple)).collect(Collectors.toList());
    }
}

 

Since a sequential stream guarantees the order of elements hence peak will also print in the same sequence, but the parallel stream is handed by multiple cores of the CPU via multiple threads hence peak output order is not guaranteed.

package org.wesome.java8;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

class Apple {
    public static void main(String args[]) {
        List<String> apples = Arrays.asList("Macintosh", "Fuji", "Gala", "Jonagold");

        System.out.println("*---------------------------Peak with sequential stream---------------------------*");
        apples.stream().peek(apple -> System.out.println("initial apple is = " + apple)).collect(Collectors.toList());

        System.out.println("*---------------------------Peak with parallel stream---------------------------*");
        apples.parallelStream().peek(apple -> System.out.println("initial apple is = " + apple)).collect(Collectors.toList());
    }
}

follow us on