How to create Stream?
1. From a Collection: We create a Stream from a list using the stream() method. 2. From an Array: We create a Stream from an array using the Stream.of() method. 3. Of Integers: We create a Stream of integers using the IntStream.range() method. 4. Of Strings: We create a Stream of strings using the Stream.of() method. 5. Infinite Stream: We create an infinite Stream using the Stream.generate() method. 6. Empty Stream: We create an empty Stream using the Stream.empty() method. 7. From a File: We create a Stream from a file using the Files.lines() method. 8. From a Stream Builder: We create a Stream using a Stream Builder by adding elements using the Stream.Builder interface.
var numbers = [1, 2, 3, 4, 5];
// Creating a stream
var stream = numbers.stream();
// Create a Stream from a Collection
var list = [1, 2, 3, 4, 5];
var streamFromCollection = list.stream();
// Create a Stream from an Array
var array = [1, 2, 3, 4, 5];
var streamFromArray = Stream.of(array);
// Create a Stream of Integers
var streamOfIntegers = IntStream.range(1, 6);
// Create a Stream of Strings
var streamOfStrings = Stream.of("apple", "banana", "cherry");
// Create an Infinite Stream
var infiniteStream = Stream.generate(() => Math.random());
// Create an Empty Stream
var emptyStream = Stream.empty();
// Create a Stream from a File
var fileStream = Files.lines(Paths.get("file.txt"));
// Create a Stream from a Stream Builder
var streamBuilder = Stream.builder();
var streamFromBuilder = streamBuilder.add("hello").add("world").build();
console.log(streamFromCollection);// [1, 2, 3, 4, 5]
console.log(streamFromArray); // [1, 2, 3, 4, 5]
console.log(streamOfIntegers); // [1, 2, 3, 4, 5]
console.log(streamOfStrings); // ["apple", "banana", "cherry"]
console.log(infiniteStream); // [0.12345..., 0.98765..., 0.54321..., ...]
console.log(emptyStream); //[]
console.log(fileStream); // [line1, line2, line3, ...]
console.log(streamFromBuilder); // ["hello", "world"]
Intermediate Operations
Intermediate Operation | Description | Example |
---|---|---|
filter | Filters the elements of a stream based on a specified condition. | stream.filter(x -> x > 5) |
map | Transforms each element of a stream by applying a given function. | stream.map(x -> x * 2) |
flatMap | Flattens nested streams into a single stream. | stream.flatMap(List::stream) |
distinct | Removes duplicate elements from a stream. | stream.distinct() |
sorted | Sorts the elements of a stream in natural order or using a custom comparator. | stream.sorted() |
limit | Truncates a stream to a specified size. | stream.limit(10) |
skip | Skips a specified number of elements from the beginning of a stream. | stream.skip(5) |
peek | Performs an action on each element of a stream without modifying the stream. | stream.peek(System.out::println) |
var filteredStream = stream.filter(n => n > 2); // Filter elements based on a condition
var mappedStream = stream.map(n => n * 2); // Map each element to a new value
var distinctStream = stream.distinct(); // Remove duplicate elements
var sortedStream = stream.sorted(); // Sort the elements
Terminal Operations
Terminal Operation | Description | Example |
---|---|---|
forEach | Performs an action for each element in the stream. | stream.forEach(System.out::println) |
count | Returns the count of elements in the stream. | stream.count() |
collect | Performs a mutable reduction operation on the elements of the stream. | stream.collect(Collectors.toList()) |
min | Returns the minimum element of the stream based on a specified comparator. | stream.min(Comparator.naturalOrder()) |
max | Returns the maximum element of the stream based on a specified comparator. | stream.max(Comparator.naturalOrder()) |
anyMatch | Checks if any element in the stream matches a given condition. | stream.anyMatch(x -> x > 5) |
allMatch | Checks if all elements in the stream match a given condition. | stream.allMatch(x -> x > 0) |
noneMatch | Checks if no elements in the stream match a given condition. | stream.noneMatch(x -> x < 0) |
findFirst | Returns the first element of the stream. | stream.findFirst() |
findAny | Returns any element of the stream. | stream.findAny() |
var collectedList = stream.collect(Collectors.toList()); // Collect the elements into a List
var forEachStream = stream.forEach(n => console.log(n)); // Perform an action for each element
var count = stream.count(); // Count the number of elements
var sum = stream.sum(); // Calculate the sum of the elements
var max = stream.max(); // Find the maximum element
var anyMatch = stream.anyMatch(n => n > 3); // Check if any element matches a condition
var allMatch = stream.allMatch(n => n > 0); // Check if all elements match a condition
var noneMatch = stream.noneMatch(n => n < 0); // Check if no element matches a condition
var reducedValue = stream.reduce(0, (a, b) => a + b); // Reduce the elements to a single value
These examples demonstrate how terminal operations can be used to perform actions on the elements of a stream and retrieve results or perform checks based on the stream's content.