Skip to content

Instantly share code, notes, and snippets.

@goodGid
Last active Sep 15, 2020
Embed
What would you like to do?
How to use `peek()` method
public class Goodgid {

    public void printEven() {
        List<Integer> numbers = Arrays.asList(2, 3, 4, 5);

        numbers.stream()
               .map(x -> x + 17)
               .filter(x -> x % 2 == 0)
               .limit(3)
               .forEach(System.out::println);
    }

    public void printEvenWithDebug() {
        List<Integer> numbers = Arrays.asList(2, 3, 4, 5);

        numbers.stream()
               .peek(x -> System.out.println("\nStart Debug"))
               .peek(x -> System.out.println("from stream : " + x))
               .map(x -> x + 17)
               .peek(x -> System.out.println("after stream : " + x))
               .filter(x -> x % 2 == 0)
               .peek(x -> System.out.println("after filter : " + x))
               .limit(3)
               .peek(x -> System.out.println("after limit : " + x))
               .forEach(System.out::println);
    }

    public static void main(String[] args) {
        Goodgid goodgid = new Goodgid();
//        goodgid.printEven();
        goodgid.printEvenWithDebug();
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment