Introduction
Here we will explore many java stream operations. Keep in mind that there are many more. In the second part, we will even program a custom implementation of a stream operation. In general, you can distinguish between Intermediate operations, which transform a stream into another stream, and Terminal operations which produce a result or side effect. Here is a link to the repository to play around yourself.
This part focuses on intermediate operations
1. Filter
Definition: Returns a new stream consisting of the elements that match a given predicate.
In this example, filter() will filter out all elements of the incomming list names, where each name contains the letter „n“. You can define your own predicate, like String length, equals, etc.
2. Map
Definition: Returns a new stream by applying a given function to each element.
In this example, map() will execute the toLowerCase operation on each String element in the list names. Therefore Dennis will be mapped to dennis, Anna to anna and Zoe to zoe.
3. Distinct
Definition: Returns a new stream with distinct elements based on their natural order or a specified comparator
In this example, distinct() will execute filter out the element „Dennis“, since it is available two times in the list. The comparison is based on the equals() method. Therefore right now, if Dennis and dennis are on the list, they are not equal. If you want to filter out case-sensitive data, you need to map the data, e.g. with map(String::toLowerCase)
4. FlatMap
Definition: Returns a new stream by applying a function that returns a stream for each element and then concatenates the resulting streams.
Let’s say we have a list of Person
objects and each Person
has a list of phone numbers. We want to create a single list of all the phone numbers across all Person
objects. We can use flatMap
to accomplish this
In this example, flatMap() will help us to get information from Lists of Lists. We had a Persons List, and each Person has a List of Telephone numbers. With flatMap we can get all telephone numbers with just one operation.
5. Sorted
Definition: Returns a new stream sorted according to its natural order or a specified comparator
In this example, sorted() will order the given list. Since we didn’t specify a comparator, their natural order is Anna, Dennis, Zoe.
6. Peek
Definition: Returns a new stream with the same elements as the original, but with an action performed on each element as they are consumed.
In this example, peek() will execute one method, e.g. System.out.println, but we still can continue with our stream. We could after peeking filter or map our method. In our case, we terminate the stream with toList.
More intermediate operations.
Like said before, there are many operations. Here is a short list if you are interested.
- limit(): Returns a stream that is truncated to the specified number of elements.
- skip(): Returns a stream with the first N elements removed.
- takeWhile(): Returns a stream that consists of the longest prefix of elements that match a given predicate.
- dropWhile(): Returns a stream that consists of the remaining elements of the stream after dropping the longest prefix of elements that match a given predicate.
- filterNot(): Returns a stream consisting of the elements that do not match the given predicate.
- mapToInt(), mapToLong(), mapToDouble(): Returns a stream of primitive type (int, long or double) by applying a function to each element of the stream.
- boxed(): Returns a stream consisting of the elements of the current stream, boxed to their corresponding reference type (e.g. Integer, Long, etc.).
- mapMulti(): Returns a stream that flattens the result of applying a function to each element of the current stream, producing multiple output elements for each input element.
- unordered(): Returns an unordered stream.
- concat(Stream a, Stream b): Returns a new stream that is the concatenation of two streams.
- filterNotNull(): Returns a stream that contains only non-null elements.
- mapToObj(): Returns a stream of objects by applying a function to each element of the stream.
- mapToByte(), mapToChar(), mapToFloat(), mapToShort(): Returns a stream of primitive type (byte, char, float, or short) by applying a function to each element of the stream.
- flatMapToInt(), flatMapToLong(), flatMapToDouble(): Returns a stream of primitive type (int, long, or double) by applying a function that returns an IntStream, LongStream, or DoubleStream to each element of the stream.
- flatMapToObj(): Returns a stream of objects by applying a function that returns a Stream to each element of the stream.
Conclusion
You saw many examples of intermediate operations. The most often used are filter and map. Please take a look at the second part of the tutorial for terminal operations. And check out the example project with a lot of streaming operations
Pingback: Java Streams and Lambdas API Tutorial – Overview – Code Nest