Mike Simmons wrote:In general, you can't add to a stream once it's been created. ...
You can start off with a “populated” Stream and methods like filter() and limit() can remove all elements. For example, in the following example, everything is coloured white, black, green or blue. So line 3 produces an empty Stream.The takeWhile() and dropWhile() methods can also produce an empty Stream. In the following example, you should be able to work out that I have made a mistake, and I challenge you to correct it.Anil Philip wrote:. . . of what use is an empty stream... . . .
Campbell Ritchie wrote:In the following example, you should be able to work out that I have made a mistake
.dropWhile(i -> i != 0)
.takeWhile(i -> i != 0) // Work out how to avoid repeated code
Campbell Ritchie wrote:I think you are confusing takeWhile() or dropWhile() and filter(); please suggest how you would correct my mistake.
1
2
3
4
5
[]
Anil Philip wrote:I don't know what you are trying to do. You will always get an empty list at the end.
Mike Simmons wrote:
I found this confusing too, but the key explanation was in the comment:
This describes what the intent was, which is not an empty stream - unless you enter two 0's in a row. So for this input:
the goal is to ignore everything before (and including) the first 0, and also everything after (and including) the second 0. What remains is:
However, the code he wrote does produce an empty stream. He's asking, how could it be changed to produce the input described in the comment?
I'm not sure how this is an example of why we might need an empty stream. Rather, it's an example of how a programming error might produce one unintentionally.
Yes, I did say there was an error in my code and I think MS has found it. I don't, however, think AP has fully identified my error. Let's expand the code a bit to use System.in on JShell.Mike Simmons wrote:. . . I'm not sure how this is an example of why we might need an empty stream. Rather, it's an example of how a programming error might produce one unintentionally.
Intended result = [123, 234, 345, 456] and result obtained = []. Did your peek() call give you any useful information?=Campbell's JShelljshell> {
...> Scanner scan = new Scanner(System.in);
...> List<Integer> numbers =
...> scan.tokens() // all tokens
...> .filter(s -> new Scanner(s).hasNextInt()) // keep only ints
...> .mapToInt(Integer::parseInt) // change to primitive
...> .dropWhile(i -> i != 0) // miss out non-zeroes
...> .takeWhile(i -> i != 0) // go on until zero is found
...> .mapToObj(Integer::valueOf) // Turn into object
...> .collect(ArrayList::new, List::add, List::addAll);
...> System.out.println(numbers);
...> }
Campbell 123 is 876 brilliant 999 and 0 CodeRanch 123 is 234 a 345 good 456 place 0 to -987654321 learn.
It is possible to concatenate two Streams of compatible types. Method link. Not sure, but I don't think that feature is used at all frequently.Anil Philip wrote:Can you add to a stream? . . .
Campbell Ritchie wrote:There is a much simpler way to remedy the error. It is only necessary to “lose” the one zero, which you can do with .skip(1L).
Anil Philip wrote:what use is an empty stream
Campbell Ritchie wrote:My earlier code showed how an empty Stream can arise from the correct operation of correctl
Liutauras Vilda wrote:
Anil Philip wrote:The original question is not answered. Not about returning an empty stream as an end condition in a check, but why would one create it as in the call empty() in the OP.
Mike Simmons wrote:As for "what use is an empty Stream", though... well, you can use it anytime you have an API that expects a Stream, but you don't have anything to pass it. Much like you might need to use an empty list or zero-length array. For example, the Optional class has a method to convert the optional to a Stream of zero or one elements ...
Ron McLeod wrote:
I feel that this post provided a good explanation:
Mike Simmons wrote:As for "what use is an empty Stream", though... well, you can use it anytime you have an API that expects a Stream, but you don't have anything to pass it. Much like you might need to use an empty list or zero-length array. For example, the Optional class has a method to convert the optional to a Stream of zero or one elements ...
Yes, Mike and [edit]Ron[/edit] have both answered it. Occasionally you will use it to signal that there are zero values. For example, I have a name in two parts, so the most accurate ways to represent my middle names would be a 0‑length array, or a 0‑size List, or a 0‑element Stream. Call the Stream 0‑element rather than empty and you are being assertive about my having zero middle names. And don't try using null instead.Anil Philip wrote:. . . The original question is not answered. . . .
He was expelled for perverse baking experiments. This tiny ad is a model student:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|