0
# Flowable API
1
2
Flowable is RxJava's implementation of the Reactive Streams specification, providing backpressured reactive streams for handling sequences of 0 to N items with built-in flow control. It implements the Publisher interface and supports standard backpressure strategies.
3
4
## Capabilities
5
6
### Flowable Creation
7
8
Factory methods for creating Flowable instances with backpressure support.
9
10
```java { .api }
11
/**
12
* Creates a Flowable that emits a single item
13
* @param item the item to emit
14
* @return Flowable that emits the single item
15
*/
16
public static <T> Flowable<T> just(T item);
17
18
/**
19
* Creates a Flowable from an Iterable source
20
* @param source the Iterable to convert
21
* @return Flowable that emits items from the Iterable
22
*/
23
public static <T> Flowable<T> fromIterable(Iterable<? extends T> source);
24
25
/**
26
* Creates a Flowable from a Publisher (Reactive Streams)
27
* @param publisher the Publisher to convert
28
* @return Flowable wrapping the Publisher
29
*/
30
public static <T> Flowable<T> fromPublisher(Publisher<? extends T> publisher);
31
32
/**
33
* Creates a Flowable using a custom emitter function
34
* @param source the FlowableOnSubscribe function
35
* @param mode the BackpressureStrategy to use
36
* @return Flowable created from the custom emitter
37
*/
38
public static <T> Flowable<T> create(FlowableOnSubscribe<T> source, BackpressureStrategy mode);
39
40
/**
41
* Creates a Flowable that emits sequential integers
42
* @param start the starting value
43
* @param count the number of items to emit
44
* @return Flowable emitting integers from start to start+count-1
45
*/
46
public static Flowable<Integer> range(int start, int count);
47
48
/**
49
* Creates a Flowable that emits at specified intervals
50
* @param period the emission interval
51
* @param unit the time unit
52
* @return Flowable emitting sequential longs at intervals
53
*/
54
public static Flowable<Long> interval(long period, TimeUnit unit);
55
56
/**
57
* Creates a Flowable from a callable, executed lazily
58
* @param callable the Callable to execute
59
* @return Flowable that emits the callable result
60
*/
61
public static <T> Flowable<T> fromCallable(Callable<? extends T> callable);
62
```
63
64
### Transformation Operators
65
66
Transform emitted items with full backpressure support.
67
68
```java { .api }
69
/**
70
* Transform items using a mapping function
71
* @param mapper function to transform each item
72
* @return Flowable with transformed items
73
*/
74
public final <R> Flowable<R> map(Function<? super T, ? extends R> mapper);
75
76
/**
77
* Transform items to Publishers and flatten the results
78
* @param mapper function returning Publisher for each item
79
* @return Flowable with flattened results
80
*/
81
public final <R> Flowable<R> flatMap(Function<? super T, ? extends Publisher<? extends R>> mapper);
82
83
/**
84
* Transform items to Publishers and concatenate them in order
85
* @param mapper function returning Publisher for each item
86
* @return Flowable with concatenated results in order
87
*/
88
public final <R> Flowable<R> concatMap(Function<? super T, ? extends Publisher<? extends R>> mapper);
89
90
/**
91
* Emit only items that pass a predicate test
92
* @param predicate function to test each item
93
* @return Flowable with filtered items
94
*/
95
public final Flowable<T> filter(Predicate<? super T> predicate);
96
97
/**
98
* Scan items with an accumulator function
99
* @param accumulator function to accumulate values
100
* @return Flowable emitting accumulated values
101
*/
102
public final Flowable<T> scan(BiFunction<T, T, T> accumulator);
103
104
/**
105
* Buffer items into lists of specified size
106
* @param count the size of each buffer
107
* @return Flowable emitting lists of buffered items
108
*/
109
public final Flowable<List<T>> buffer(int count);
110
111
/**
112
* Group items by a key selector function
113
* @param keySelector function to select grouping key
114
* @return Flowable emitting GroupedFlowable instances
115
*/
116
public final <K> Flowable<GroupedFlowable<K, T>> groupBy(Function<? super T, ? extends K> keySelector);
117
```
118
119
### Backpressure Handling
120
121
Control flow when downstream can't keep up with upstream.
122
123
```java { .api }
124
/**
125
* Buffer all upstream items when backpressure occurs
126
* @return Flowable that buffers all items
127
*/
128
public final Flowable<T> onBackpressureBuffer();
129
130
/**
131
* Buffer upstream items with specified capacity
132
* @param capacity maximum buffer size
133
* @return Flowable that buffers up to capacity items
134
*/
135
public final Flowable<T> onBackpressureBuffer(int capacity);
136
137
/**
138
* Drop items when backpressure occurs
139
* @return Flowable that drops items under backpressure
140
*/
141
public final Flowable<T> onBackpressureDrop();
142
143
/**
144
* Drop items with callback when backpressure occurs
145
* @param onDrop action called for each dropped item
146
* @return Flowable that drops items under backpressure
147
*/
148
public final Flowable<T> onBackpressureDrop(Consumer<? super T> onDrop);
149
150
/**
151
* Keep only the latest item when backpressure occurs
152
* @return Flowable that keeps only latest items
153
*/
154
public final Flowable<T> onBackpressureLatest();
155
156
/**
157
* Reduce request amounts to manage backpressure
158
* @param reducer function to reduce requested amounts
159
* @return Flowable with reduced request amounts
160
*/
161
public final Flowable<T> onBackpressureReduce(BiFunction<T, T, T> reducer);
162
```
163
164
### Subscription and Scheduling
165
166
Control subscription behavior and execution context.
167
168
```java { .api }
169
/**
170
* Subscribe with a simple onNext callback
171
* @param onNext function called for each emitted item
172
* @return Disposable for managing the subscription
173
*/
174
public final Disposable subscribe(Consumer<? super T> onNext);
175
176
/**
177
* Subscribe with Subscriber interface
178
* @param subscriber the Subscriber to receive emissions
179
*/
180
public final void subscribe(Subscriber<? super T> subscriber);
181
182
/**
183
* Subscribe and block until completion, returning all items
184
* @return Iterable of all emitted items
185
*/
186
public final Iterable<T> blockingIterable();
187
188
/**
189
* Subscribe and return the first item, blocking
190
* @return the first emitted item
191
*/
192
public final T blockingFirst();
193
194
/**
195
* Specify the Scheduler for subscription operations
196
* @param scheduler the Scheduler to use for subscriptions
197
* @return Flowable operating on the specified scheduler
198
*/
199
public final Flowable<T> subscribeOn(Scheduler scheduler);
200
201
/**
202
* Specify the Scheduler for observation operations
203
* @param scheduler the Scheduler to use for observations
204
* @return Flowable observing on the specified scheduler
205
*/
206
public final Flowable<T> observeOn(Scheduler scheduler);
207
```
208
209
### Parallel Processing
210
211
Convert to parallel processing with multiple streams.
212
213
```java { .api }
214
/**
215
* Convert to parallel processing using available processors
216
* @return ParallelFlowable for parallel processing
217
*/
218
public final ParallelFlowable<T> parallel();
219
220
/**
221
* Convert to parallel processing with specified parallelism
222
* @param parallelism number of parallel streams
223
* @return ParallelFlowable for parallel processing
224
*/
225
public final ParallelFlowable<T> parallel(int parallelism);
226
227
/**
228
* Convert to parallel processing with custom prefetch
229
* @param parallelism number of parallel streams
230
* @param prefetch prefetch amount for each stream
231
* @return ParallelFlowable for parallel processing
232
*/
233
public final ParallelFlowable<T> parallel(int parallelism, int prefetch);
234
```
235
236
### Type Conversions
237
238
Convert between reactive types.
239
240
```java { .api }
241
/**
242
* Convert to Observable (loses backpressure)
243
* @return Observable equivalent of this Flowable
244
*/
245
public final Observable<T> toObservable();
246
247
/**
248
* Convert to Single (takes first item or errors)
249
* @return Single with the first emitted item
250
*/
251
public final Single<T> firstOrError();
252
253
/**
254
* Convert to Single (takes last item or errors)
255
* @return Single with the last emitted item
256
*/
257
public final Single<T> lastOrError();
258
259
/**
260
* Convert to Maybe (takes first item or completes empty)
261
* @return Maybe with the first emitted item or empty
262
*/
263
public final Maybe<T> firstElement();
264
265
/**
266
* Convert to Completable (ignores items, keeps completion/error)
267
* @return Completable that signals completion or error
268
*/
269
public final Completable ignoreElements();
270
```
271
272
## Types
273
274
```java { .api }
275
/**
276
* Interface for creating custom Flowable sources
277
*/
278
public interface FlowableOnSubscribe<T> {
279
void subscribe(FlowableEmitter<T> emitter) throws Throwable;
280
}
281
282
/**
283
* Emitter interface for custom Flowable creation
284
*/
285
public interface FlowableEmitter<T> extends Emitter<T> {
286
void onNext(T value);
287
void onError(Throwable error);
288
void onComplete();
289
long requested();
290
}
291
292
/**
293
* Subscriber interface following Reactive Streams specification
294
*/
295
public interface FlowableSubscriber<T> extends Subscriber<T> {
296
void onSubscribe(Subscription s);
297
void onNext(T t);
298
void onError(Throwable t);
299
void onComplete();
300
}
301
302
/**
303
* Backpressure strategy enumeration
304
*/
305
public enum BackpressureStrategy {
306
MISSING, // No backpressure handling
307
ERROR, // Error when backpressure occurs
308
BUFFER, // Buffer all items
309
DROP, // Drop items when backpressured
310
LATEST // Keep only latest items
311
}
312
313
/**
314
* Grouped Flowable for groupBy operations
315
*/
316
public abstract class GroupedFlowable<K, T> extends Flowable<T> {
317
public abstract K getKey();
318
}
319
```
320
321
**Usage Examples:**
322
323
```java
324
import io.reactivex.rxjava3.core.Flowable;
325
import io.reactivex.rxjava3.core.BackpressureStrategy;
326
import io.reactivex.rxjava3.schedulers.Schedulers;
327
import java.util.concurrent.TimeUnit;
328
329
// Basic Flowable with backpressure handling
330
Flowable.range(1, 1000000)
331
.onBackpressureBuffer(1000)
332
.observeOn(Schedulers.computation())
333
.subscribe(System.out::println);
334
335
// Custom Flowable creation with backpressure
336
Flowable.<String>create(emitter -> {
337
for (int i = 0; i < 10; i++) {
338
if (emitter.isCancelled()) break;
339
emitter.onNext("Item " + i);
340
}
341
if (!emitter.isCancelled()) {
342
emitter.onComplete();
343
}
344
}, BackpressureStrategy.BUFFER)
345
.subscribe(System.out::println);
346
347
// Parallel processing
348
Flowable.range(1, 100)
349
.parallel()
350
.runOn(Schedulers.computation())
351
.map(x -> x * x)
352
.sequential()
353
.subscribe(System.out::println);
354
355
// Reactive Streams interop
356
Publisher<String> publisher = Flowable.just("Hello", "World");
357
Flowable.fromPublisher(publisher)
358
.subscribe(System.out::println);
359
360
// Backpressure strategies
361
Flowable.interval(1, TimeUnit.MILLISECONDS)
362
.onBackpressureDrop(dropped -> System.out.println("Dropped: " + dropped))
363
.observeOn(Schedulers.single())
364
.subscribe(System.out::println);
365
```