# skipWhile

Published articles for skipWhile.

This is one page of public article previews, not the complete archive. Follow Next page to continue. Summaries are not the original full articles.

## Java 9 Flow API: taking and skipping

DevFeed: [Java 9 Flow API: taking and skipping](<https://devfeed.tech/articles/java-9-flow-api-taking-and-skipping-24812.md>)

Original publisher: [Read original article](<https://akarnokd.blogspot.com/2017/09/java-9-flow-api-taking-and-skipping.html>)

Author: David Karnok (noreply@blogger.com)

Published: 2017-09-30T20:44:00Z

Content type: tutorial

Language: en

Sources: [Akarnokd - Advanced RxJava](<https://devfeed.tech/sources/akarnokd-advanced-rxjava.md>)

Topics: [Java 9](<https://devfeed.tech/topics/java-9.md>), [API](<https://devfeed.tech/topics/api.md>), [reactive](<https://devfeed.tech/topics/reactive.md>)

Tags: [api](<https://devfeed.tech/tags/api.md>), [flow](<https://devfeed.tech/tags/flow.md>), [flow-api](<https://devfeed.tech/tags/flow-api.md>), [implementation](<https://devfeed.tech/tags/implementation.md>), [java-9](<https://devfeed.tech/tags/java-9.md>), [jdk](<https://devfeed.tech/tags/jdk.md>), [publisher](<https://devfeed.tech/tags/publisher.md>), [reactive](<https://devfeed.tech/tags/reactive.md>), [reactive-streams](<https://devfeed.tech/tags/reactive-streams.md>), [skip](<https://devfeed.tech/tags/skip.md>), [skipwhile](<https://devfeed.tech/tags/skipwhile.md>), [streams](<https://devfeed.tech/tags/streams.md>), [subscriber](<https://devfeed.tech/tags/subscriber.md>), [subscription](<https://devfeed.tech/tags/subscription.md>), [take](<https://devfeed.tech/tags/take.md>), [takeuntil](<https://devfeed.tech/tags/takeuntil.md>), [takewhile](<https://devfeed.tech/tags/takewhile.md>)

### AI overview

A tutorial on implementing take and skip-style operators with Java 9's Flow API. It explains how to limit a flow, cancel the upstream subscription when the limit is reached, complete the downstream subscriber, handle terminal events, and account for backpressure behavior.

### Source excerpt

Introduction Limiting or skipping over parts of a flow is a very common task: either we are only interested in the first N items or we don't care about the first N items. Sometimes, N is unknown but we can decide, based on the current item, when to stop relaying items or, in contrast, when to start relaying items. Take(N) In concept, limiting a flow to a certain size should be straightforward: count the number of items received via onNext and when the limit is reached, issue a cancel() towards the upstream and onComplete() towards the downstream. public static <T> Flow.Publisher<T> take(Flow.Publisher<T> source, long n) { return new TakePublisher<>(source, n); } The operator's implementation requires little state: static final class TakeSubscriber<T> implements Flow.Subscriber<T> { final Flow.Subscriber<? super T> downstream; Flow.Subscription upstream; long remaining; TakeSubscriber( Flow.Subscriber<? super> downstream, long n) { this.downstream = downstream; this.remaining = n; } @Override public void onSubscribe(Flow.Subscription s) { // TODO implement } @Override public void onNext(T item) { // TODO implement } @Override public void onError(Throwable throwable) { // TODO implement } @Override public void onComplete() { // TODO implement } } In its simplest form, there is no need for intercepting the request() and cancel() calls from the downstream: these can be passthrought, however, since the operator has to stop the sequence upon reaching the limit (remaining == 0), the upstream's Flow.Subscriber has to be stored. @Override public void onSubscribe(Flow.Subscription s) { this.upstream = s; downstream.onSubscribe(s); } In onSubscribe, we only have to store the Flow.Subscription and forward it to the downstream. @Override public void onNext(T item) { long r = remaining; if (r > 0L) { remaining = --r; downstream.onNext(item); if (r == 0) { upstream.cancel(); downstream.onComplete(); } } } While remaining is positive, we decrement it and save it into its field foll