SlideShare a Scribd company logo
JAVA Streams
Murali D
Stream
In Java, a Stream is a sequence of elements that can be processed in parallel or sequentially. It is part of the java.util.stream
package and provides a high-level abstraction for performing operations on collections, arrays, or other data sources in a functional
style.
Streams allow for functional-style operations such as filtering, mapping, and reducing on data, and they help avoid the need for
explicit loops. A stream doesn’t store data; it simply conveys elements from a data source, such as a collection, array, or I/O channel,
through a pipeline of computational operations.
Key Characteristics of a Stream
1. No Storage: A stream does not store elements. Instead, it operates on data from a source (such as a collection, array, or I/O
channel) and processes them on-demand.
2. Functional in Nature: Streams support functional-style operations. You can chain operations using method calls to process
data in a more declarative manner, avoiding traditional loops and conditionals.
3. Laziness: Most operations on streams are lazy. This means the operations are not executed until a terminal operation is
invoked. This allows for optimizations, such as short-circuiting (stopping early) or parallelism.
4. Possibility of Parallelism: Streams can be processed in parallel with minimal effort by invoking parallelStream() or using the
parallel() method on a stream.
5. Once-Only: A stream can be traversed only once. After it has been consumed or processed (e.g., using a terminal operation
like collect(), forEach(), etc.), it cannot be reused.
Stream Creation
Streams can be created from various data sources such as collections, arrays, or
I/O channels.
From a Collection:
List<String> names = Arrays.asList("John", "Jane", "Adam", "Eve");
Stream<String> nameStream = names.stream();
From an Array:
String[] array = {"a", "b", "c", "d"};
Stream<String> arrayStream = Arrays.stream(array);
From a Range of Numbers:
IntStream rangeStream = IntStream.range(1, 10); // 1 to 9
Types of Streams
1.Sequential Stream.
2.Parallel Stream.
Parallel Stream
A stream that processes data elements in parallel using multiple threads, which can improve performance for large datasets.
● Created by invoking the parallelStream() method on a collection, or by calling the parallel() method on a stream.
Example:
java
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
● Stream<Integer> parallelStream = numbers.parallelStream();
Sequential Stream
A stream that processes data elements sequentially in the order they are encountered.
● Created by invoking the stream() method on a collection.
Example:
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
● Stream<Integer> sequentialStream = numbers.stream();
Stream Operations
A Stream represents a sequence of elements and supports a variety of operations that can be chained together. There are two main
types of operations in a stream:
Stream operations are classified into two types:
1. Intermediate Operations: These return a new stream and are lazy, meaning they are not executed until a terminal operation
is invoked.
○ Examples: filter(), map(), sorted(), distinct()
○ These operations create a new stream, which can be further processed.
2. Terminal Operations: These trigger the processing of the stream and produce a result or a side-effect. Once a terminal
operation is invoked, the stream is considered consumed and cannot be reused.
○ Examples: collect(), forEach(), reduce(), count()
Explanation of Stream Operations
Intermediate operations (e.g., filter(), map(), sorted(), distinct(), peek()):
● filter(): Filters elements based on a condition (e.g., keep numbers greater than 5).
● map(): Transforms elements (e.g., multiplies each element by 2).
● sorted(): Sorts the elements.
● distinct(): Removes duplicates from the stream.
● peek(): Allows you to perform an action on elements while still passing them through the stream, typically used for debugging or
logging.
Terminal operations (e.g., collect(), forEach(), count(), anyMatch(), reduce()):
● collect(): Collects the results into a container (e.g., a List, Set, etc.).
● forEach(): Iterates over elements and performs an action (e.g., printing them).
● count(): Returns the number of elements in the stream.
● anyMatch(): Checks if any element satisfies a given condition.
● reduce(): Combines elements into a single result (e.g., summing up numbers).
Example of a Stream Pipeline
A stream pipeline consists of both intermediate and terminal operations.
Here’s an example:
import java.util.*;
import java.util.stream.*;
public class StreamExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// Stream pipeline: filter even numbers, multiply them by 2, collect results into a list
List<Integer> result = numbers.stream() // Create stream
.filter(n -> n % 2 == 0) // Intermediate operation: filter even numbers
.map(n -> n * 2) // Intermediate operation: multiply each by 2
.collect(Collectors.toList()); // Terminal operation: collect to a list
System.out.println(result); // Output: [4, 8, 12, 16, 20]
}
}
import java.util.*;
import java.util.stream.*;
public class StreamOperationsExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); // Creating a list of integers
Stream<Integer> filteredStream = numbers.stream().filter(n -> n > 5); // filter() - filters out numbers gt than 5
Stream<Integer> mappedStream = filteredStream.map(n -> n * 2); // map() - multiplies each number by 2
Stream<Integer> sortedStream = mappedStream.sorted(); // Sort the stream
List<Integer> resultList = sortedStream.collect(Collectors.toList()); // Collect results into a List
System.out.println("Result after applying stream operations: " + resultList);
List<Integer> numbersWithDuplicates = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 5);
numbersWithDuplicates.stream().distinct().forEach(System.out::println); // Remove duplicates
long count = numbers.stream().peek(n -> System.out.println("Processing: " + n)).count(); // count()
System.out.println("Total count: " + count);
boolean hasEven = numbers.stream().anyMatch(n -> n % 2 == 0); // Check if there are any even numbers
System.out.println("Contains even numbers: " + hasEven);
Optional<Integer> sum = numbers.stream().reduce((a, b) -> a + b); // combines elements using an associative accumulation
function
sum.ifPresent(s -> System.out.println("Sum of numbers: " + s));
}
}
THANK YOU

More Related Content

PPT
Java 8 Streams
PDF
Streams in Java 8
PDF
Charles Sharp: Java 8 Streams
PDF
Java Lambda internals with invoke dynamic
PDF
Functional programming with streams
PPTX
Java 8 Lambda and Streams
PDF
Lambda.pdf
PPTX
JDK8 Streams
Java 8 Streams
Streams in Java 8
Charles Sharp: Java 8 Streams
Java Lambda internals with invoke dynamic
Functional programming with streams
Java 8 Lambda and Streams
Lambda.pdf
JDK8 Streams

Similar to Java Advanced Topic - Streams Presentations (20)

PPTX
Java 8 streams
PDF
Harnessing the Power of Java 8 Streams
PDF
Concept of Stream API Java 1.8
PDF
cb streams - gavin pickin
PDF
Java 8 Streams - in Depth
PDF
CBStreams - Java Streams for ColdFusion (CFML)
PDF
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...
PPTX
Introduction to java 8 stream api
PPTX
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
PDF
Java 8 Stream API (Valdas Zigas)
PPTX
Java se 8 streams pt1
PDF
Going reactive in java
PPTX
ADT02 - Java 8 Lambdas and the Streaming API
PPTX
Stream processing - Apache flink
PPTX
Java8 training - Class 1
PPTX
Lambdas And Streams Hands On Lab, JavaOne 2014
PPTX
Java8 training - class 3
PPTX
Java 8
PDF
Java Streams Interview short reminder with examples
PPTX
JDK8 Stream api
Java 8 streams
Harnessing the Power of Java 8 Streams
Concept of Stream API Java 1.8
cb streams - gavin pickin
Java 8 Streams - in Depth
CBStreams - Java Streams for ColdFusion (CFML)
ITB2019 CBStreams : Accelerate your Functional Programming with the power of ...
Introduction to java 8 stream api
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Stream API (Valdas Zigas)
Java se 8 streams pt1
Going reactive in java
ADT02 - Java 8 Lambdas and the Streaming API
Stream processing - Apache flink
Java8 training - Class 1
Lambdas And Streams Hands On Lab, JavaOne 2014
Java8 training - class 3
Java 8
Java Streams Interview short reminder with examples
JDK8 Stream api
Ad

Recently uploaded (20)

PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Lesson notes of climatology university.
PPTX
master seminar digital applications in india
PDF
Complications of Minimal Access Surgery at WLH
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Classroom Observation Tools for Teachers
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Pharma ospi slides which help in ospi learning
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Lesson notes of climatology university.
master seminar digital applications in india
Complications of Minimal Access Surgery at WLH
VCE English Exam - Section C Student Revision Booklet
Supply Chain Operations Speaking Notes -ICLT Program
2.FourierTransform-ShortQuestionswithAnswers.pdf
Classroom Observation Tools for Teachers
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
102 student loan defaulters named and shamed – Is someone you know on the list?
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Final Presentation General Medicine 03-08-2024.pptx
Pharma ospi slides which help in ospi learning
O5-L3 Freight Transport Ops (International) V1.pdf
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Final Presentation General Medicine 03-08-2024.pptx
Microbial disease of the cardiovascular and lymphatic systems
Ad

Java Advanced Topic - Streams Presentations

  • 2. Stream In Java, a Stream is a sequence of elements that can be processed in parallel or sequentially. It is part of the java.util.stream package and provides a high-level abstraction for performing operations on collections, arrays, or other data sources in a functional style. Streams allow for functional-style operations such as filtering, mapping, and reducing on data, and they help avoid the need for explicit loops. A stream doesn’t store data; it simply conveys elements from a data source, such as a collection, array, or I/O channel, through a pipeline of computational operations.
  • 3. Key Characteristics of a Stream 1. No Storage: A stream does not store elements. Instead, it operates on data from a source (such as a collection, array, or I/O channel) and processes them on-demand. 2. Functional in Nature: Streams support functional-style operations. You can chain operations using method calls to process data in a more declarative manner, avoiding traditional loops and conditionals. 3. Laziness: Most operations on streams are lazy. This means the operations are not executed until a terminal operation is invoked. This allows for optimizations, such as short-circuiting (stopping early) or parallelism. 4. Possibility of Parallelism: Streams can be processed in parallel with minimal effort by invoking parallelStream() or using the parallel() method on a stream. 5. Once-Only: A stream can be traversed only once. After it has been consumed or processed (e.g., using a terminal operation like collect(), forEach(), etc.), it cannot be reused.
  • 4. Stream Creation Streams can be created from various data sources such as collections, arrays, or I/O channels. From a Collection: List<String> names = Arrays.asList("John", "Jane", "Adam", "Eve"); Stream<String> nameStream = names.stream(); From an Array: String[] array = {"a", "b", "c", "d"}; Stream<String> arrayStream = Arrays.stream(array); From a Range of Numbers: IntStream rangeStream = IntStream.range(1, 10); // 1 to 9
  • 5. Types of Streams 1.Sequential Stream. 2.Parallel Stream.
  • 6. Parallel Stream A stream that processes data elements in parallel using multiple threads, which can improve performance for large datasets. ● Created by invoking the parallelStream() method on a collection, or by calling the parallel() method on a stream. Example: java List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); ● Stream<Integer> parallelStream = numbers.parallelStream();
  • 7. Sequential Stream A stream that processes data elements sequentially in the order they are encountered. ● Created by invoking the stream() method on a collection. Example: List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); ● Stream<Integer> sequentialStream = numbers.stream();
  • 8. Stream Operations A Stream represents a sequence of elements and supports a variety of operations that can be chained together. There are two main types of operations in a stream: Stream operations are classified into two types: 1. Intermediate Operations: These return a new stream and are lazy, meaning they are not executed until a terminal operation is invoked. ○ Examples: filter(), map(), sorted(), distinct() ○ These operations create a new stream, which can be further processed. 2. Terminal Operations: These trigger the processing of the stream and produce a result or a side-effect. Once a terminal operation is invoked, the stream is considered consumed and cannot be reused. ○ Examples: collect(), forEach(), reduce(), count()
  • 9. Explanation of Stream Operations Intermediate operations (e.g., filter(), map(), sorted(), distinct(), peek()): ● filter(): Filters elements based on a condition (e.g., keep numbers greater than 5). ● map(): Transforms elements (e.g., multiplies each element by 2). ● sorted(): Sorts the elements. ● distinct(): Removes duplicates from the stream. ● peek(): Allows you to perform an action on elements while still passing them through the stream, typically used for debugging or logging. Terminal operations (e.g., collect(), forEach(), count(), anyMatch(), reduce()): ● collect(): Collects the results into a container (e.g., a List, Set, etc.). ● forEach(): Iterates over elements and performs an action (e.g., printing them). ● count(): Returns the number of elements in the stream. ● anyMatch(): Checks if any element satisfies a given condition. ● reduce(): Combines elements into a single result (e.g., summing up numbers).
  • 10. Example of a Stream Pipeline A stream pipeline consists of both intermediate and terminal operations. Here’s an example: import java.util.*; import java.util.stream.*; public class StreamExample { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); // Stream pipeline: filter even numbers, multiply them by 2, collect results into a list List<Integer> result = numbers.stream() // Create stream .filter(n -> n % 2 == 0) // Intermediate operation: filter even numbers .map(n -> n * 2) // Intermediate operation: multiply each by 2 .collect(Collectors.toList()); // Terminal operation: collect to a list System.out.println(result); // Output: [4, 8, 12, 16, 20] } }
  • 11. import java.util.*; import java.util.stream.*; public class StreamOperationsExample { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); // Creating a list of integers Stream<Integer> filteredStream = numbers.stream().filter(n -> n > 5); // filter() - filters out numbers gt than 5 Stream<Integer> mappedStream = filteredStream.map(n -> n * 2); // map() - multiplies each number by 2 Stream<Integer> sortedStream = mappedStream.sorted(); // Sort the stream List<Integer> resultList = sortedStream.collect(Collectors.toList()); // Collect results into a List System.out.println("Result after applying stream operations: " + resultList); List<Integer> numbersWithDuplicates = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 5); numbersWithDuplicates.stream().distinct().forEach(System.out::println); // Remove duplicates long count = numbers.stream().peek(n -> System.out.println("Processing: " + n)).count(); // count() System.out.println("Total count: " + count); boolean hasEven = numbers.stream().anyMatch(n -> n % 2 == 0); // Check if there are any even numbers System.out.println("Contains even numbers: " + hasEven); Optional<Integer> sum = numbers.stream().reduce((a, b) -> a + b); // combines elements using an associative accumulation function sum.ifPresent(s -> System.out.println("Sum of numbers: " + s)); } }