Search⌘ K
AI Features

Peek, Limit, Sort

Explore how to utilize Java 8 Stream methods peek, limit, and sorted to process and manipulate data streams. Understand their roles in debugging, restricting element counts, and sorting results. This lesson helps you write efficient and readable stream operations with practical examples.

We'll cover the following...

Peek

The Peek method in Java is used to fetch the first element in a stack. We can peek into a stream to do some action without interrupting the stream.

For example, we could print out elements to debug code:

Files.list(Paths.get("."))
     .map(Path::getFileName)
     .peek(System.out::println)
     .forEach(p -> doSomething(p));

We can use any ...