T
- item typepublic class AppendableTraverser<T> extends Object implements Traverser<T>
ArrayDeque
. You can efficiently append(T)
items to it that will be later traversed.
It's useful to be returned from a flat-mapping function when an item is flat-mapped to a small number of items:
Traverser<Integer> at = new AppendableTraverser(2);
Traverser<Integer> t = Traverser.over(10, 20)
.flatMap(item -> {
at.append(item);
at.append(item + 1);
return at;
});
The t
traverser above will output {10, 11, 20, 21}. This approach
reduces the GC pressure by avoiding the allocation of a new traverser for
each item that will traverse over just a few or even zero items.
See ResettableSingletonTraverser
if you have at most one item to
traverse.
Constructor and Description |
---|
AppendableTraverser(int initialCapacity)
Creates an appendable traverser.
|
Modifier and Type | Method and Description |
---|---|
AppendableTraverser<T> |
append(T item)
Returns a traverser that will return all the items of this traverser,
plus an additional item once this one returns
null . |
boolean |
isEmpty()
|
T |
next()
Returns the next item, removing it from this traverser.
|
public AppendableTraverser(int initialCapacity)
@Nonnull public AppendableTraverser<T> append(@Nonnull T item)
null
. After that
it continues forwarding the return values of this traverser. It is
meant to be used on finite traversers.
Default implementations always returns a new traverser instance. If you
need to append multiple objects or use this method frequently,
AppendableTraverser
might be a better choice.
public T next()
Traverser
null
. If this traverser is null-terminated,
getting a null
means it's exhausted and will keep returning
null
forever. Otherwise, trying again later may produce one.public boolean isEmpty()
Copyright © 2022 Hazelcast, Inc.. All rights reserved.