Overview of Java Streams
Java introduced streams in version 8 to simplify and optimize code writing. A stream is a sequence of objects supporting various methods to achieve a desired result. Using streams can reduce a long set of codes to just a few lines.
Streams enable performing operations such as filtering, mapping, reducing, and sorting. These operations drastically reduce code complexity.
In this blog, we will go through Java Streams Overview, Features, and Operations
Lambda Operator
The lambda operator “->”, introduced in Java 8, is a feature that facilitates the implementation of functional interfaces concisely and expressively.
In this we partition the value into two parts, Left part specifies the parameter required by the expression whereas, the right part specifies the actions/condition.
Syntax: “(parameters) -> expression”
Features of Java Streams
The features of Java stream are mentioned below:
- Take input from Collections, Arrays, or I/O channels, but are not data structures themselves.
- Preserve the original data structure and deliver results through pipelined methods.
- Lazily execute intermediate operations, allowing pipelining, with terminal operations concluding the stream and yielding the result.
Different Operations on Streams
There are two types of Operations in Streams:
- Intermediate Operations: In Java streams, these operations transform or filter elements lazily, generating a new stream in the process.
- Terminate Operations: Concluding actions on streams in Java, these operations yield a result or trigger a side-effect, signifying the end of stream processing.
The intermediate and terminal operations are interrelated, and considering them in isolation doesn’t yield meaningful results. Hence, for any terminated operation to successfully operate, the intermediate operation should return true.
The working of any Stream can be explained in the following stages:
- Creation of Stream.
- Performing an intermediate operation on the initial stream to transform it into another stream.
- Performing terminal operations on the final stream to obtain the result.
Understanding the Syntax of Streams:
Input:
{ArrayList<String> name = new ArrayList<String>(); name.add("abhijeet"); name.add("Abhishek"); name.add("Avinaash"); name.add("shivaansh"); name.add("Ram"); //To Convert into Stream compatible format we give .stream() Long c = name.stream().filter(s->s.startsWith("A")).count(); System.out.println(c);
Output:
2
Explanation:
The array is made stream-compatible by using “.streams()”
The intermediate operation could be defined by the expression “.filter(s->s.startsWith(“A”))”
The terminate operation could be defined by “.count();”
From this, we have seen that a stream is an array that is a filtered and specified version of the earlier array.
A few of the Intermediate operations can be seen as follows:
- Filter(): This method filters out the stream with a more precise value requirement.
- Map(): This method helps us to return a stream consisting of the results of applying the given function to the elements of the stream.
- Sorted(): This method is used to sort the stream.
A few of the Terminate Operations can be seen as follows:
- forEach(): This method is used to iterate through every stream element.
-
count(): This method gives us the intermediate operation performed.
-
Collect(): This method returns the result of intermediate operations performed.
Conclusion
In a nutshell, Java Streams, introduced in Java version 8, is a game-changer for code simplicity and optimization. Their ability to transform complex code into a few concise lines enhances efficiency and reduces manual effort. With features like non-invasive data structure handling and lazy execution of operations, streams are a versatile tool for developers.
The example illustrates how a simple ArrayList can be transformed effortlessly. Intermediate operations (Filter(), Map(), Sorted()) and terminal operations (forEach(), count(), collect()) provide a powerful toolkit for streamlined coding. Mastering Java Streams is not just a skill; it’s a paradigm shift in coding practices, promising a brighter future for code optimization. Happy coding! 🚀
You can read more about it here: https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html
Good content writing 👍
Very informative blog Smriti..!!!
Informative blog,
Got to know about the concept of Java streams, will try to implement it.
Very informative Smriti….!!
Thanks Smriti for the blog. its precise and a good read.