GenericStream<T> class
abstract
A sequence of elements supporting sequential and parallel aggregate operations.
The following example illustrates an aggregate operation using GenericStream:
final sum = widgets.stream()
.filter((w) => w.color == RED)
.mapToInt((w) => w.weight)
.sum();
In this example, widgets
is a List of Widget
objects. We create a stream
of Widget
objects via List.stream
, filter it to produce a stream containing
only the red widgets, and then transform it into a stream of int
values
representing the weight of each red widget. Then this stream is summed to
produce a total weight.
In addition to GenericStream, which is a stream of object references, there are primitive specializations for IntStream and DoubleStream, all of which are referred to as "streams" and conform to the characteristics and restrictions described here.
To perform a computation, stream operations are composed into a stream pipeline. A stream pipeline consists of a source (which might be an array, a collection, a generator function, an I/O channel, etc), zero or more intermediate operations (which transform a stream into another stream, such as filter), and a terminal operation (which produces a result or side-effect, such as count or forEach). Streams are lazy; computation on the source data is only performed when the terminal operation is initiated, and source elements are consumed only as needed.
Example Usage
final people = [
Person('Alice', 25, 'Engineering'),
Person('Bob', 30, 'Marketing'),
Person('Charlie', 35, 'Engineering'),
Person('Diana', 28, 'Sales'),
];
// Filter and collect
final engineers = GenericStream.of(people)
.filter((p) => p.department == 'Engineering')
.toList();
// Transform and reduce
final totalAge = GenericStream.of(people)
.mapToInt((p) => p.age)
.sum();
// Group by department
final byDepartment = GenericStream.of(people)
.collect(Collectors.groupingBy((p) => p.department));
// Complex pipeline
final result = GenericStream.of(people)
.filter((p) => p.age > 25)
.map((p) => p.name.toUpperCase())
.sorted()
.limit(2)
.collect(Collectors.joining(', '));
- Inheritance
-
- Object
- BaseStream<
T, GenericStream< T> > - GenericStream
- Annotations
-
- @Generic(GenericStream)
Constructors
- GenericStream()
- A sequence of elements supporting sequential and parallel aggregate operations.
- GenericStream.empty()
-
Creates an empty GenericStream.
factory
- GenericStream.generate(T supplier())
-
Creates a GenericStream by repeatedly calling a supplier function.
factory
- GenericStream.iterate(T seed, T f(T))
-
Creates an infinite GenericStream by repeatedly applying a function.
factory
-
GenericStream.of(Iterable<
T> values) -
Creates a GenericStream from the given values.
factory
- GenericStream.ofSingle(T value)
-
Creates a GenericStream from a single element.
factory
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
allMatch(
bool predicate(T)) → bool - Returns whether all elements of this stream match the provided predicate.
-
anyMatch(
bool predicate(T)) → bool - Returns whether any elements of this stream match the provided predicate.
-
close(
) → void -
Closes this stream, causing all close handlers for this stream pipeline
to be called.
inherited
-
collect(
) → List< T> -
Returns a list containing the elements of this stream.
inherited
-
collectFrom<
A, R> (Collector< T, A, R> collector) → R - Performs a mutable reduction operation on the elements of this stream using a Collector.
-
count(
) → int - Returns the count of elements in this stream.
-
distinct(
) → GenericStream< T> - Returns a stream consisting of the distinct elements (according to Object.==) of this stream.
-
dropWhile(
bool predicate(T)) → GenericStream< T> - Returns a stream consisting of the remaining elements of this stream after dropping the longest prefix of elements that match the given predicate.
-
filter(
bool predicate(T)) → GenericStream< T> - Returns a stream consisting of the elements of this stream that match the given predicate.
-
findAny(
) → Optional< T> - Returns an Optional describing some element of the stream, or an empty Optional if the stream is empty.
-
findFirst(
) → Optional< T> - Returns an Optional describing the first element of this stream, or an empty Optional if the stream is empty.
-
flatMap<
R> (GenericStream< R> mapper(T)) → GenericStream<R> - Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.
-
forEach(
void action(T)) → void - Performs an action for each element of this stream.
-
forEachOrdered(
void action(T)) → void - Performs an action for each element of this stream, in the encounter order of the stream if the stream has a defined encounter order.
-
isParallel(
) → bool -
Returns whether this stream, if a terminal operation were to be executed,
would execute in parallel.
inherited
-
iterable(
) → Iterable< T> -
Returns an iterable for the elements of this stream.
inherited
-
iterator(
) → Iterator< T> -
Returns an iterator for the elements of this stream.
inherited
-
limit(
int maxSize) → GenericStream< T> -
Returns a stream consisting of the elements of this stream, truncated
to be no longer than
maxSize
in length. -
map<
R> (R mapper(T)) → GenericStream< R> - Returns a stream consisting of the results of applying the given function to the elements of this stream.
-
mapToDouble(
double mapper(T)) → DoubleStream - Returns a DoubleStream consisting of the results of applying the given function to the elements of this stream.
-
mapToInt(
int mapper(T)) → IntStream - Returns an IntStream consisting of the results of applying the given function to the elements of this stream.
-
max(
[int comparator(T, T)?]) → Optional< T> - Returns the maximum element of this stream according to the provided Comparator.
-
min(
[int comparator(T, T)?]) → Optional< T> - Returns the minimum element of this stream according to the provided Comparator.
-
noneMatch(
bool predicate(T)) → bool - Returns whether no elements of this stream match the provided predicate.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
onClose(
void closeHandler()) → GenericStream< T> -
Returns an equivalent stream with an additional close handler.
inherited
-
parallel(
) → GenericStream< T> -
Returns an equivalent stream that is parallel.
inherited
-
peek(
void action(T)) → GenericStream< T> - Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.
-
reduce(
T identity, T accumulator(T, T)) → T - Performs a reduction on the elements of this stream, using the provided identity value and an associative accumulation function, and returns the reduced value.
-
reduceOptional(
T accumulator(T, T)) → Optional< T> - Performs a reduction on the elements of this stream, using an associative accumulation function, and returns an Optional describing the reduced value, if any.
-
sequential(
) → GenericStream< T> -
Returns an equivalent stream that is sequential.
inherited
-
skip(
int n) → GenericStream< T> -
Returns a stream consisting of the remaining elements of this stream
after discarding the first
n
elements of the stream. -
sorted(
[int comparator(T, T)?]) → GenericStream< T> - Returns a stream consisting of the elements of this stream, sorted according to natural order.
-
takeWhile(
bool predicate(T)) → GenericStream< T> - Returns a stream consisting of the longest prefix of elements taken from this stream that match the given predicate.
-
toList(
) → List< T> - Returns a List containing the elements of this stream.
-
toSet(
) → Set< T> - Returns a Set containing the elements of this stream.
-
toString(
) → String -
A string representation of this object.
inherited
-
unordered(
) → GenericStream< T> -
Returns an equivalent stream that is unordered.
inherited
-
where(
Predicate< T> predicate) → GenericStream<T> - Returns the first element of this stream that matches the given predicate.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited