JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Checkout for Promo Codes

Java 8 Stream peek method example.


The Stream.peek() method is mainly to support debugging, where you want to see the elements as they flow past a certain point in a pipeline.

The Stream.peek() method returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.

For parallel stream pipelines, the action may be called at whatever time and in whatever thread the element is made available by the upstream operation. If the action modifies shared state, it is responsible for providing the required synchronization.

package com.java2novice.streams;

import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamPeekEx {

	public static void main(String a[]) {

		Stream.of("bus", "car", "bycle", "flight", "train")
		        .filter(e -> e.length() > 3)
		        .peek(e -> System.out.println("Filtered value: " + e))
		        .map(String::toUpperCase)
		        .peek(e -> System.out.println("Mapped value: " + e))
		        .collect(Collectors.toList());
	}
}

Output:
Filtered value: bycle
Mapped value: BYCLE
Filtered value: flight
Mapped value: FLIGHT
Filtered value: train
Mapped value: TRAIN
<< Previous Program | Next Program >>

Java 8 Streams Examples

  1. How Java 8 Streams work?
  2. Java 8 Streams parallelism introduction.
  3. Explain non-interference behavior of Java 8 Streams.
  4. Create Java 8 Stream using Stream.of() method example.
  5. Create Java 8 Stream using List example.
  6. Create Java 8 Stream using Stream.generate() method.
  7. Java 8 Stream.filter() example.
  8. Java 8 Stream.map() example.
  9. Java 8 Stream flatmap method example.
  10. Java 8 Stream peek method example.
  11. Java 8 Stream distinct method example.
  12. Java 8 Stream sorted method example.
  13. Java 8 Stream limit method example.
  14. Java 8 Stream forEach method example.
  15. Java 8 Stream toArray method example.
  16. Java 8 Stream reduce method example.
  17. Java 8 Stream collect method example.
  18. Java 8 Stream concat method example.
  19. Java 8 Stream anyMatch(), allMatch() and noneMatch() example.
  20. Java 8 Stream findFirst(), findAny() example.
  21. Primitive type Stream example.
Knowledge Centre
Interface and its usage
Interface is similar to a class which may contain method's signature only but not bodies and it is a formal set of method and constant declarations that must be defined by the class that implements it. Interfaces are useful for declaring methods that one or more classes are expected to implement, capturing similarities between unrelated classes without forcing a class relationship and determining an object's programming interface without revealing the actual body of the class.
Famous Quotations
Do not confuse motion and progress. A rocking horse keeps moving but does not make any progress.
-- Alfred A. Montapert

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.