What is the peek() method in Java Stream API?
The peek() method in Java 8 Stream API is an intermediate operation. We can perform an action on each element of the stream without modifying the elements.
What is the use of peek() method in Java Stream API?
It peak() method is usually used for debugging or logging purposes.
The peek() method takes a Consumer as an argument, and the provided consumer is applied to each element in the stream.
Examples of peak()
import java.util.Arrays;
import java.util.List;
public class PeekExample {
public static void main(String[] args) {
List<String> namesList = Arrays.asList("Mike","Sam","Alan");
List<String> result = namesList.stream()
.peek(name -> System.out.println("Original name: " + name))
.map(name -> name.toUpperCase())
.peek(uppercasedName -> System.out.println("Uppercase name: " + uppercasedName))
.map(name -> "Hello " + name)
.peek(nameWithGreeting -> System.out.println("Name with Greeting" + nameWithGreeting))
.toList();
}
}
Output:
You can download this article in various formats for your convenience. Choose from the options below:
Facing issues? Have Questions? Post them here! I am happy to answer!
- Java Jackson ObjectMapper Class with Examples
- How to add a Delay of a Few Seconds in Java Program
- Java SE JDBC Select Statement Example
- How to Word-Warp Console logs in IntelliJ
- Convert Java Array to ArrayList Code Example
- [Java] Error: Unmappable character for encoding UTF-8. Save could not be completed.
- Maven BUILD FAILURE: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin
- How to extract Java Jar/War/Ear files in Linux
- Java: Convert String to Binary
- Java Generics: Type parameter cannot be instantiated directly
- How to Create and Run a Command Line Application using Spring Boot
- How to Convert a Java Object into an Optional with Example
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to Set JAVA_HOME PATH to Mac .zshrc profile file
- Formatting Double in Java [Examples]
- Java 8: Stream map with Code Examples
- Fix: SyntaxError: ( was never closed [Python]
- Java Split String by Spaces
- [fix] Java JDBC SQLSyntaxErrorException: Unknown database
- Fix: SpringFramework BeanDefinitionValidationException: Could not find an init method named
- Java Date Time API: LocalDateTime get(TemporalField field) examples
- How to create Date in yyyy-MM-dd format in Java
- Java: Create Temporary Directory and File and Delete when application terminates
- Fix RabbitMQ: AuthenticationFailureException: ACCESS_REFUSED
- Run Java Code Every Second
- Online Free SQL Query Formatter (Prettifier) Tool - Tools
- Fetch as Google Crawl Error or Redirected Status - Google
- Spring Boot: Transactions Management with JDBCTemplate Example - Java
- How to know the Serial Number of MacBook on macOS Ventura 13.0 - MacOS
- apt-get list --installed packages in Ubuntu Linux - Ubuntu
- Quick way to install AWS CLI for macOS Terminal - AWS
- What is ValueError: math domain error and how to fix it - Python
- How to disable SharePoint subsite creation option for owners - SharePoint