a stream can be traversed only once and the stream is said to have been consumed after it has been traversed
In fact a simple sequential stream might only need enough memory for one element. Once that element has been finished with, the memory location is filled by the reference to the next element. There is often no need to keep a record of how many elements have been processed.
ras oscar wrote:And to complete the thought, if for some reason you wanted to use the contents of the stream again, you could assign the contents of each element to an internal array.
And to complete the thought further, you could wrap the stream function into another class that includes a rewind function to close the existing stream and reopen it at index 0.
The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.
That's a pleasureBiniman Idugboe wrote:. . . Thanks.
a stream can be traversed only once and the stream is said to have been consumed after it has been traversed
SCJP 5, OCPJP 7, 8, SCJD 5, SCWCD 4, SCBCD 5, SCJWS 4, IBM OOAD 833 & 834, MongoDB Developer
Prasad Saya wrote:
Streams (java.util.stream.Stream) are meant for data transformation. Collections are, on the other hand, are about data storage and access of that data.
The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.
Your second assertion weakens the first, since if you stream into a collection, you're not necessarily transforming the data, although you are consuming the stream and producing (or adding to) the collection.
SCJP 5, OCPJP 7, 8, SCJD 5, SCWCD 4, SCBCD 5, SCJWS 4, IBM OOAD 833 & 834, MongoDB Developer
Prasad Saya wrote:
Your second assertion weakens the first, since if you stream into a collection, you're not necessarily transforming the data, although you are consuming the stream and producing (or adding to) the collection.
I may create a collection (thru a stream process), but that would be transformed data from a source collection to a target collection. That's what it means. Otherwise it would be just copying a collection.
For example, I may want to collect the names of all the ranchers with more than, say, 20 cows. The rancher collection is transformed: (filtered where cows > 20), mapped (I am getting only the names, not other attributes) and reduced (collected to a collection of choice). That's transformed data.
Also, note that transformation of data is just one of the primary features of the streams. An advantage of using streams is the code shows how the data is transformed (in the process), as opposed to what the code does. This is self documenting - easily readable (hence maintainable) code.
The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.
Don't overlook what I said about the difference between transforming the collection versus transforming the data in the collection.
For that matter, if I read a collection to obtain a collection of database keys so that I can do things with the records indicated by the keys can you really say I've "transformed" those keys? Consumed, yes, but I think most people would balk are considering that as a transformation.
SCJP 5, OCPJP 7, 8, SCJD 5, SCWCD 4, SCBCD 5, SCJWS 4, IBM OOAD 833 & 834, MongoDB Developer
Prasad Saya wrote:
Its still consuming the data for transformation. Yes, one may use the keys to process the data, but the data is also part of that consumption. It must be part of the stream data.
I don't know yet if a stream can be made of keys only and would access an external source to access its related data and then transform that data. That would be a case of causing a side-effect, and according to Stream API javadocs, such procedure is discouraged. Also, one would be introducing statefulness into the stream operations.
The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.
Let's not get into a war of words, but the idea behind the Streams I thought the OP was asking about is that they leave the source of the data unchanged. And that includes all the data in a collection.Prasad Saya wrote:[. . . . Its still consuming the data for transformation. . . . .
That is conceivable. But you would have to use a very awkward intermediate operation to map the key to the data read from the database. You would have a similarly awkward terminal operation to write the new data back, which is a side‑effect. Remember that a few terminal operations, e.g. forEach(), are allowed to have side‑effects, and that reading information from a store of information (I think) doesn't count as a side‑effect.I don't know yet if a stream can be made of keys only and would access an external source . . . .
No. Although it is possible to use a Stream like that, they are suitable for any collection. Please look in the documentation and see whether it says anything about the source collection being modified by other code while it is being read by a Stream.ras oscar wrote:. . . . 1. Streams as presented as library functions in Java are meant to be used where the data is time sensitive, or in flux, or subject to change . . .
Not sure I understand that statement.2. Collections classes as presented in the Java library functions are appropriate where data transformations will be limited to operations within the Java application.
That is a bad example. You might be more likely to take all natural numbers ≤ 500000 and use another method to find whether they are prime, maybe by checking from your array. Example follows, assuming primes is a BitSet which has already been filled with whether a number is or isn't a prime. I would use a Sieve of Eratosthenes to fill that BitSet or array.Line 2 selects values without any transformation; line 4 transforms them to a a different, but related, type, and line 5 collects them into a new List without further transformation. That list will be biased towards small numbers because there are more small primes as a proportion of all numbers; it says in The Strange Case of the Dog in the Night‑Time that the number of primes ≤ n is approximately proportional to logn.Example: reading a file containing an array of values for a Java program that records all prime numbers from zero to 500,000
Tim Holloway wrote:
Prasad Saya wrote:
Its still consuming the data for transformation. Yes, one may use the keys to process the data, but the data is also part of that consumption. It must be part of the stream data.
I don't know yet if a stream can be made of keys only and would access an external source to access its related data and then transform that data. That would be a case of causing a side-effect, and according to Stream API javadocs, such procedure is discouraged. Also, one would be introducing statefulness into the stream operations.
No, there's no transformation being done in this scenario. The data here is an input (possibly one of many inputs) to an unrelated process. In the example case, this process consumes keys and uses them to determine what records from an entirely different (and non-stream) data source will be processes.
Say I have a file with the loan numbers of delinquent loans. I want to mark those loans for follow-up. I read a stream based on the file and the process that marks the loans uses this stream to lookup and mark the loan records. The file is only read, no transformation, conversion or even storage of the data in the file/stream is being done. The data being transformed here is all in the database.
You seem to be thinking of some very intelligent stream processes. That's fine, but the core definition of a stream says nothing about what ultimately should happen to the stream's contents. A basic stream returns data completely un-transformed and leaves it to other processes as to what, if any transformations might be done with that data. The greater does not imply the lesser; just because some streams transform doesn't mean that all streams transform.
Say I have a file with the loan numbers of delinquent loans. I want to mark those loans for follow-up. I read a stream based on the file and the process that marks the loans uses this stream to lookup and mark the loan records. The file is only read, no transformation, conversion or even storage of the data in the file/stream is being done. The data being transformed here is all in the database.
SCJP 5, OCPJP 7, 8, SCJD 5, SCWCD 4, SCBCD 5, SCJWS 4, IBM OOAD 833 & 834, MongoDB Developer
Please only quote the part of the previous post you are addressing, i.e. that part, not the whole post. That simply makes the posts longer without adding new information. Quoting a small part makes it clear what you are addressing.Prasad Saya wrote:. . . Referring this from the above quote: . . .
Have a look at the post about non‑interference I gave you earlier. See whether that helps. I don't know what would happen if the database is altered concurrently. That sounds like a policy decision you would have to make. Are you locking the database? Are new payments being recorded so loans lose their delinquent status? Don't know. I think you are going to have to test that.. . . what happens if the database data gets modified while this process is in progress? . . .
Surely you would update the delinquency file from the database?One can as well read the file with delinquent loans and update the loans database.
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |