+ 8
Java Streams
Can somebody enlighten me on the introduction to streams in Java. The purpose behind it and how is it efficient. I couldn't find any useful resources related to this. If you could as well provide an answer to my previous question that is shared below, it would be very helpful. Thank you. https://www.sololearn.com/discuss/2121632/?ref=app
8 Réponses
+ 6
Avinesh The name for Java Streams API is certainly not the most intuitive.
For those familiar with C#, they can think of Java Streams API as a clumsy equivalent of LINQ.
Essentially, Java Streams API (and C# LINQ for that matter) will bring a functional programming style to their, otherwise, imperative programming languages.
It would help to understand the basics of the functional programming paradigm to know how these differ.
In an overly simplistic explanation, think of Streams as a flow of data moving from one place to the next. Once the current piece of data has moved on, there is no referring back to it. This is similar to a forward, readonly cursor for those familiar with that concept in databases or XML stream readers.
Streams will output a new instance of the data and feed it as the input for the next stream method. This is essentially method chaining inputs and outputs of one stream method to flow into the next.
(continued...)
+ 6
The logical mindset for this type of programming will feel foreign and possibly awkward for those only familiar with imperative programming.
Stream methods are also similar to pipeline components where a collection of data is transformed from one form to another and then again to another and so on.
Imagine a collection:
["fish", "dog", "bird", "cat", "zebra", "moose"]
And you want to apply the following changes to it:
1. Sort by Value.
2. Filter out values greater than 3 characters.
3. Capitalize first letters.
Imperatively, this would involve a set of loops.
Streams could do the same declaratively like the following pseudo code:
coll.sort()
.filter(x => x. length <= 3)
.select(x =>
x[0].upperCase +
x.subString(1, x.length - 1) )
I've made up the methods above to illustrate the method chaining equivalents of what the Streams API might be doing.
Hopefully, this makes sense.
+ 5
Terminal operations include things like the following:
- getting the collection count, min, or max values
- validating whether a condition is true for some, all, or no items in the collection
- retrieving an item from a collection based on some criteria
- aggregating all values into a single composite value like getting the sum or average of all values.
Most, if not all of these will allow for a listener function to perform some work on each item to complete its intended result.
Hopefully, this helps fill in more blanks.
+ 3
Avinesh Great! 👌
Honestly... this is the first time I've ever attempted to explain Java Streams API and I did it on the fly with no cross referencing from other websites.
My goal was to provide a very basic understanding of the concepts behind Java Streams API, rather than focus on the API itself.
That said, I just came across this rather detailed review of the Streams API and thought it might make sense after reading my high level explanation.
http://tutorials.jenkov.com/java-functional-programming/streams.html#chain-not-graph
It's no secret I favor C# over Java for many different reasons. It's hard not to wonder if Java went out of their way to use very different terminology from equivalent counterparts in C# LINQ.
If that was the case, it could be at the compromise of adding much confusion to beginners.
(continued...)
+ 3
David Carroll Sir your answers are well documented than the java documentation itself😂.
To be real honest I just read about the source, the intermediate operations and the terminal operations on streams before asking you this question.
So now after reading all your answers, it makes a lot more sense.
Thanks a lot for your time and answer, I appreciate it very much👍
You do not have to refer anywhere, because that was a perfect answer for a beginner.
+ 2
David Carroll This is what I was exactly referring to. I have got some idea about streams now. Thanks for helping Sir.
+ 2
Here are a few examples where they could have used less confusing terms:
- "Stream" is essentially a reference to the current collection where each item can be handled by some function, a.k.a. listener.
- "Listener" is simply a function that is called once for each item where some work or evaluation is done on the current item. Think of this more as a function handler, similar to an event handler.
- "Non-terminal Operation" is essentially a stream method that receives a stream as an input and returns a new stream after applying the listener function to all items in the stream collection. These can be chained with other stream methods.
- "Terminal Operation" is essentially a stream method that receives a stream as an input and returns a single value after applying the listener function to at least one or all items in the stream collection. These aren't chained with other stream methods.
(continued...)
+ 1
David Carroll does the terminal operation only return primitives, strings and lists.
If I need to return something like a complete record or something, isn't it possible at the terminal?
[Id= 1, Name=Avinesh] // output
Just assume that I'm using filter() to check whether id==1 and want to print the whole record matching the condition.