-
Notifications
You must be signed in to change notification settings - Fork 252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should Stream.iteratorStream be deprecated? #304
Comments
👍 |
Splitterators created with the correct Characteristics should be safe (unsure). Maybe Stream<->java.util.Stream can be made safe, rather than deprecated. |
https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html Down the bottom. "Consumable. The elements of a stream are only visited once during the life of a stream. Like an Iterator, a new stream must be generated to revisit the same elements of the source." Java 8 Streams are designed for 1 pass only. So a 2nd pass is a misuse. |
The deprecation of |
I see. It does not need attention yet. Its just something noticed a year ago that caught me out. Thought I make sure its noticed. Even a warning in the javadoc that By throwing an Exception, at least we provide Deterministic behavior. |
So safety by documentation? Make people aware of limitations of |
The following static method in Stream is unsafe.
public static <A> Stream<A> iteratorStream(final Iterator<A> it)
Due to Stream's use of
WeakRef
, when you visit the elements ofStream
you can get a different result on the 2nd pass. (Internal mutable iterator keeps its state while cache is cleared)I think this method should be avoided at all times because its behavior is unpredictable.
The text was updated successfully, but these errors were encountered: