Skip to content

Instantly share code, notes, and snippets.

@goodGid
Last active September 15, 2020 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goodGid/f971fa5225238281a930c3d8d1c78437 to your computer and use it in GitHub Desktop.
Save goodGid/f971fa5225238281a930c3d8d1c78437 to your computer and use it in GitHub Desktop.
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