Introduction
Java Streams provide many out-of-the-box powerful features. In this article, we will look into some of the lesser-used or known features among the developers.
🚀 Unlock Your Path to Success with the Ultimate Everything Bundle! 🚀
Introducing the Everything Bundle — your one-stop solution to mastering Java, Spring Boot, SQL, acing interviews, and securing certifications!
Stream.ofNullable
This method was introduced in Java 9 and helps filter all the null values from the collection, potentially saving us from null pointer exceptions.
In the example below we have a names list that contains null, which we filter using the Stream.ofNullable method.
[Alice, Bob, Charlie]
2. Stream.iterate()
iterate() method is used for creating infinite streams of sequences. It takes seed and unary function that applies the function to the previous element.
static <T> Stream<T> iterate(T seed, UnaryOperator<T> f)
In the below example, our seed is 0, and the unary operator is n -> n+2.
0
2
4
6
8
10
12
14
16
18
Since iterate() generates infinite streams we should have some termination such as limit, findFirst, or findAny, etc. to avoid an infinite loop.
3. Collectors.collectingAndThen
collectingAndThen() method was introduced in Java 8. It's a special kind of collector that allows you to perform a special kind of transformation on the result of another collector.
In the below example, our collector is getting transformed by first mapping with index to uppercase operation and then making that map an unmodifiableMap.
{0=APPLE, 1=BANANA, 2=ORANGE}
4. Stream.takeWhile and Stream.dropWhile
takeWhile() and dropWhile() method was introduced in java9 for processing streams condinally.
dropWhile() drops the element from the stream of elements until the condition is true.
takeWhile() returns the stream of elements until the condition is true
In the below example, we drop the elements til they are less than 3 and then we take until the elements are less than 6. In nutshell taking elements from 3 to 5
3
4
5
5. IntStream for Ranges:
Introduced in Java 8, IntStream provides a stream of primitive integers. IntStream.range() method generates a stream of integers that starts inclusive and ends exclusive.
IntStream.rangeClosed() method generates a stream of integers that starts and ends inclusive.
6. Collectors.teeing()
teeing() method introduced in Java 12 was created for purposes where we can apply two separate collectors together on the stream of elements and then join the result using BiFunction.
In the below example, we compute the max and min for the stream of elements and then return the result as a single map.
{max=1, min=4}
7. Stream.concat()
stream.concat() method concatenates two streams and produces a new stream.
1
2
3
4
5
6
8. Collectors.partitioningBy
partitioningBy() is used to partition a stream of integers into two groups based on the predicate.
In the below example, we are partitioning the fruits into two different groups based on length.
{false=[apple, grape], true=[banana, orange]}
Conclusion
In this article, we went through 8 lesser-known features of Java Streams API.
If you know some other methods that we have not covered feel free to comment and I will update the blog.
🚀 Become Java Spring Boot Full Stack Cloud Developer. Learn AWS, React, Docker, Spring Data JPA & Spring Security. 🚀
[NEW] Master Spring Boot 3 & Spring Framework 6 with Java
Bestseller | 4.5 ⭐ (6k ratings) | 37,892 students