I want to retrieve and remove the next item without java 8 stream
Streaming
is going away.
Stream & lt; Integer & gt; IntegerStream = stream.iterate (0, x - & gt; new integer (x + 1)); Integer zero = integerstream.tag (); // 0 integer a = integerstream.tag (); // 1 ...
Is this possible?
Yes, there is a way to do that, but there are some limitations.
Stream & lt; Integer & gt; InfiniteStream = stream.iterate (0, x - & gt; New integer (x + 1)); Iterator & LT; Integer & gt; Iter = infiniteStream.iterator (); Integer zero = iter.next (); Integer a = iter.next ();
Alternatively,
stream & lt; Integer & gt; InfiniteStream = stream.iterate (0, x - & gt; New integer (x + 1)); Splitterator & LT; Integer & gt; Splitterator = infinitestream.spliterator (); Splittertor.TeAvance (i -> System.out.println (i)); // zero spliterator.tryAdvance (I -> System.out.println (i)); Looking at the Iterator
or splitterator
from a
to stream
, it is possible , Or to ask if it is a parallel stream, etc. These are defined on the interface, which is a superintendent, making them easy to remember.
In this case we know that the stream is infinite, so there is no need to call the . If you want to peel the first elements first and then start the stream processing again, you can return a splitterter to the stream like this: hasNext ()
method of Iterator or tryAdvance () of SplitterEditor
Iterator ()
and spliterator ()
methods stream
terminal operation which means that they return after saying The exclusive use of values indicated by the stream near the itater or splitter operator is not permitted for further operation of the stream (such as filter
or map
and beyond) and IllegalStateException will be completed with
. < Code> stream & lt; Integer & gt; Stream 2 = Streamsport. Stream (splitter, falls);
This will probably work well for some things, but I'm not sure I would recommend this technique in general. I think it adds some extra objects and thereby calls additional method in the way of building the next element.
Editorial comment (not related to your question):
- Do not use
new integer (value)
. Instead useInteger.valueOf (val)
which can reuse the boxing integer if it is available, which is usually correct for range values-128 to 127. - You can switch between
interstream
instead ofstream & lt; Integer & gt;
which avoids the boxing overhead completely, does not have full complement of stream operations, but it hasiterative ()
, which takes a function that is primitiveint
Runs on values.
Comments
Post a Comment