Java Streams and Lambda expressions provide a powerful way to process collections of data. They enable functional programming techniques, making your code more concise and readable.
A Stream is a sequence of elements that can be processed in parallel or sequentially. It is part of the Java 8 Stream API.
You can create a Stream from a Collection using the stream()
method.
List<String> list = Arrays.asList("a", "b", "c");
Stream<String> stream = list.stream();
A Lambda expression is a concise way to represent an anonymous function that can be passed around as a parameter.
You can use a Lambda expression to define the behavior of operations like map()
, filter()
, and forEach()
.
list.stream().filter(s -> s.startsWith("a")).forEach(System.out::println);
map()
and flatMap()
?
map()
transforms each element, while flatMap()
flattens nested structures into a single Stream.
You can sort a Stream using the sorted()
method.
list.stream().sorted().forEach(System.out::println);
collect()
?
The collect()
method is used to accumulate the elements of a Stream into a collection.
List<String> result = list.stream().collect(Collectors.toList());
You can use the max()
method with a Comparator.
Optional<String> max = list.stream().max(Comparator.naturalOrder());
findFirst()
and findAny()
?
findFirst()
returns the first element in the Stream, while findAny()
can return any element (useful in parallel streams).
You can use the anyMatch()
method.
boolean hasA = list.stream().anyMatch(s -> s.equals("a"));
A terminal operation is an operation that produces a result or a side-effect and consumes the Stream, such as forEach()
, collect()
, or reduce()
.
You can use the collect()
method with Collectors.toList()
.
List<String> result = list.stream().collect(Collectors.toList());
reduce()
?
The reduce()
method is used to combine elements of a Stream into a single result.
Optional<String> concatenated = list.stream().reduce((s1, s2) -> s1 + s2);
You can use the skip()
method to skip a specified number of elements.
list.stream().skip(1).forEach(System.out::println);
filter()
and map()
?
filter()
is used to exclude elements based on a condition, while map()
is used to transform elements.
You can use the Arrays.stream()
method.
String[] array = {"a", "b", "c"};
Stream<String> stream = Arrays.stream(array);
A parallel Stream allows for parallel processing of elements, utilizing multiple threads for performance improvement.
You can create a parallel Stream using the parallelStream()
method on a Collection.
List<String> list = Arrays.asList("a", "b", "c");
Stream<String> parallelStream = list.parallelStream();
Optional
class?
The Optional
class is a container object which may or may not contain a non-null value, used to avoid null references.
You can use methods like orElse()
or ifPresent()
to handle empty Optionals.
String value = optionalValue.orElse("default");
A method reference is a shorthand notation of a lambda expression to call a method. It can be used with Streams.
You can use method references in place of Lambda expressions.
list.stream().forEach(System.out::println);
Collectors
class?
The Collectors
class provides various static methods to collect the elements of a Stream into collections.
You can use the Collectors.groupingBy()
method to group elements by a classifier function.
Map<Integer, List<String>> grouped = list.stream().collect(Collectors.groupingBy(String::length));
flatMap()
method used for?
The flatMap()
method is used to flatten nested structures into a single Stream.
You can use the Collectors.toMap()
method.
Map<String, Integer> map = list.stream().collect(Collectors.toMap(Function.identity(), String::length));
distinct()
method used for?
The distinct()
method is used to filter out duplicate elements from a Stream.
You can use the limit()
method to restrict the number of elements.
list.stream().limit(2).forEach(System.out::println);
peek()
method used for?
The peek()
method is used to perform an action on each element of the Stream without modifying it.
You can use the allMatch()
method.
boolean allMatch = list.stream().allMatch(s -> s.length() > 0);
Stream.of()
method used for?
The Stream.of()
method is used to create a Stream from a specified set of values.
Stream<String> stream = Stream.of("a", "b", "c");
You can create an infinite Stream using the Stream.iterate()
or Stream.generate()
methods.
Stream<Integer> infiniteStream = Stream.iterate(0, n -> n + 1);
takeWhile()
method?
The takeWhile()
method is used to take elements from a Stream while a condition is true.
dropWhile()
?
The dropWhile()
method is used to drop elements from a Stream while a condition is true.
Stream.Builder
?
The Stream.Builder
is a mutable builder for creating Streams.
You can handle exceptions using try-catch blocks within a Lambda expression.
Stream.concat()
method?
The Stream.concat()
method is used to concatenate two Streams into one.
Stream<String> combined = Stream.concat(stream1, stream2);
You can use the Files.lines()
method to create a Stream from a file.
Stream<String> lines = Files.lines(Paths.get("file.txt"));
Collectors.joining()
method?
The Collectors.joining()
method is used to concatenate the elements of a Stream into a single String.
String result = list.stream().collect(Collectors.joining(", "));
You can filter out null values using the filter()
method.
list.stream().filter(Objects::nonNull).forEach(System.out::println);
Collectors.partitioningBy()
method?
The Collectors.partitioningBy()
method is used to partition elements of a Stream into two groups based on a predicate.
Collectors.mapping()
?
The Collectors.mapping()
method is used to apply a mapping function before accumulating the results.
Collectors.counting()
method?
The Collectors.counting()
method is used to count the number of elements in a Stream.
Collectors.summingInt()
?
The Collectors.summingInt()
method is used to sum the integer values of a Stream.
int sum = list.stream().collect(Collectors.summingInt(String::length));
Collectors.averagingDouble()
method?
The Collectors.averagingDouble()
method is used to calculate the average of double values in a Stream.
Collectors.reducing()
?
The Collectors.reducing()
method is used to perform a reduction on the elements of a Stream.
Stream.empty()
method?
The Stream.empty()
method returns an empty sequential Stream.
You can create a Stream from a String using the chars()
method.
IntStream stream = "abc".chars();
Stream.iterate()
method?
The Stream.iterate()
method generates an infinite Stream by applying a function repeatedly.
Stream.generate()
?
The Stream.generate()
method creates an infinite Stream by generating elements using a Supplier.
Stream.ofNullable()
method?
The Stream.ofNullable()
method creates a Stream containing a single element or an empty Stream if the element is null.