Class ObjectIterator<O extends StoredObject>
- Type Parameters:
O
- Type of objects of the iterator.
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Iterable<O>
,Iterator<O>
StoredObject
returns an instance of this class.
Note: Please make sure that you call the close()
method to release DB resources held by this instance
once the usage over. However, if you iterate through the whole list or if you use a terminal operation, it will be
closed automatically.
- Author:
- Syam
-
Method Summary
Modifier and TypeMethodDescriptionadd
(ObjectIterator<O> iterator) Concatenate another instance.final ObjectIterator
<O> Add one object to the tail of this instance.addToHead
(ObjectIterator<O> iterator) Concatenate another instance by adding to the head of it.final ObjectIterator
<O> Add one object to the head of this instance so that it will be next entry returned.boolean
Check whether all objects are matching the given filter or not.boolean
Check whether any object is matching the given filter or not.double
average
(ToDoubleFunction<? super O> function) Get the average.void
close()
Close this instance and release all associated resources.Deprecated.collectAll
(Collection<O> collection) Collect all entries to a given collection.<T> Collection
<T> collectAll
(Collection<T> collection, Function<O, T> convertor) Collect all entries to a given collection after converting it to another type.<T> Collection
<T> collectAll
(Function<O, T> convertor) Collect all entries to a collection after converting it to another type.consumeWhile
(Predicate<O> predicate, Consumer<O> consumer) Consume objects while a given predicate is true.<TO extends StoredObject>
ObjectIterator<TO> convert
(ObjectConverter<O, TO> converter) Convert this iterator to another type.long
count()
Get the count.long
Get the count after applying a filter.static <T extends StoredObject>
ObjectIterator<T> create()
Create an empty iterator.static <FROM extends StoredObject,
TO extends StoredObject>
ObjectIterator<TO> create
(ObjectIterator<FROM> iterator, ObjectConverter<FROM, TO> converter) Create an iterator from the given iterator with conversion.static <TO extends StoredObject,
FROM>
ObjectIterator<TO> Create an iterator from some source.static <T extends StoredObject>
ObjectIterator<T> create
(Collection<T> objects) Create an iterator from the given objects.static <TO extends StoredObject,
FROM>
ObjectIterator<TO> Create an iterator from some source.static <T extends StoredObject>
ObjectIterator<T> Create an iterator from the given objects.static <T extends StoredObject>
ObjectIterator<T> Create an iterator from the given objects.static <TO extends StoredObject,
FROM>
ObjectIterator<TO> Create an iterator from some source.static <T extends StoredObject>
ObjectIterator<T> create
(T object) Create an iterator from a single object.static <T extends StoredObject>
ObjectIterator<T> create
(T... objects) Create an iterator from the given objects.Convert the iterator to a new one by eliminating duplicate consecutive entries if any from it.<TO extends StoredObject>
ObjectIterator<TO> expand
(Function<O, ObjectIterator<TO>> function) Expand this iterator using the given function.filter
(BiPredicate<? super O, ? super O> predicate) Apply a bi-filter.Apply a filter.Find the object matching the given filter.Find the first object.void
Consume objects.boolean
hasNext()
iterator()
limit
(long limit) Limit the number of entries.<TO extends StoredObject>
ObjectIterator<TO> Convert this iterator to another type.max
(Comparator<? super O> comparator) Find the object that satisfies the "max" condition that specified by the given comparator.min
(Comparator<? super O> comparator) Find the object that satisfies the "min" condition that specified by the given comparator.next()
boolean
Check whether no object is matching the given filter or not.Do some processing on each object but the same object is available after processing.random()
Select an object randomly.final void
remove()
select
(BiFunction<? super O, ? super O, O> function) Select an object based on the given bi-function (to compare with other entries).single()
Get a single entry from this iterator.single
(boolean showError) Get a single entry from this iterator.Get a single entry from this iterator (after applying the filter).Get a single entry from this iterator (after applying the filter).skip
(long count) Skip a specified number of entries.Skip objects while a given predicate is true.double
sum
(ToDoubleFunction<? super O> function) Get the sum.toList()
Collect all entries to a list.<T> List
<T> Collect all entries to a list after converting it to another type.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.lang.Iterable
spliterator
Methods inherited from interface java.util.Iterator
forEachRemaining
-
Method Details
-
add
Concatenate another instance.There is no need to maintain any references to this instance after this operation for the purpose of closing etc. and you need to take care of the resulting instance only.
- Parameters:
iterator
- Another instance.- Returns:
- A new instance that contains objects from this instance followed by objects from the added instance.
-
addToHead
Concatenate another instance by adding to the head of it.There is no need to maintain any references to this instance after this operation for the purpose of closing etc. and you need to take care of the resulting instance only.
- Parameters:
iterator
- Another instance.- Returns:
- A new instance that contains objects from the added instance followed by objects from this instance.
-
add
Add one object to the tail of this instance.There is no need to maintain any references to this instance after this operation for the purpose of closing etc. and you need to take care of the resulting instance only.
- Parameters:
object
- Object to add.- Returns:
- Resulting instance.
-
addToHead
Add one object to the head of this instance so that it will be next entry returned.There is no need to maintain any references to this instance after this operation for the purpose of closing etc. and you need to take care of the resulting instance only.
- Parameters:
object
- Object to add.- Returns:
- Resulting instance.
-
close
public void close()Close this instance and release all associated resources.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
iterator
-
next
- Specified by:
next
in interfaceIterator<O extends StoredObject>
-
hasNext
public boolean hasNext()- Specified by:
hasNext
in interfaceIterator<O extends StoredObject>
-
remove
public final void remove()- Specified by:
remove
in interfaceIterator<O extends StoredObject>
-
map
Convert this iterator to another type.There is no need to maintain any references to this instance after this operation for the purpose of closing etc. and you need to take care of the resulting instance only.
- Type Parameters:
TO
- To type.- Parameters:
function
- Function to map.- Returns:
- Converted iterator.
-
convert
Convert this iterator to another type.There is no need to maintain any references to this instance after this operation for the purpose of closing etc. and you need to take care of the resulting instance only.
- Type Parameters:
TO
- To type.- Parameters:
converter
- Converter.- Returns:
- Converted iterator.
-
expand
Expand this iterator using the given function. For each item from this iterator, an iterator is derived via the given function and the resulting iterator is a concatenated version of all those iterators streamed in sequence.There is no need to maintain any references to this instance after this operation for the purpose of closing etc. and you need to take care of the resulting instance only.
- Type Parameters:
TO
- To type.- Parameters:
function
- Function to map.- Returns:
- Converted iterator.
-
filter
Apply a filter.There is no need to maintain any references to this instance after this operation for the purpose of closing etc. and you need to take care of the resulting instance only.
- Parameters:
predicate
- Filter to apply.- Returns:
- Filtered iterator.
-
filter
Apply a bi-filter.There is no need to maintain any references to this instance after this operation for the purpose of closing etc. and you need to take care of the resulting instance only.
- Parameters:
predicate
- Filter to apply.- Returns:
- Filtered iterator.
-
process
Do some processing on each object but the same object is available after processing.There is no need to maintain any references to this instance after this operation for the purpose of closing etc. and you need to take care of the resulting instance only.
- Parameters:
consumer
- Processor that consume the object instance but releases it for further consumption.- Returns:
- Filtered iterator that invokes the processor internally. All objects are still available. However, the process may alter the attributes of the object.
-
deduplicate
Convert the iterator to a new one by eliminating duplicate consecutive entries if any from it.There is no need to maintain any references to this instance after this operation for the purpose of closing etc. and you need to take care of the resulting instance only.
- Returns:
- Converted iterator.
-
find
-
findFirst
Find the first object.Note: This is a terminal operation and the iterator is closed after this operation.
- Returns:
- The object found or null if nothing found.
-
skip
Skip a specified number of entries.- Parameters:
count
- Number of entries to drop.- Returns:
- Self.
-
limit
Limit the number of entries. (Rest will be dropped). If you pass a non-positive value, no entries will be returned.There is no need to maintain any references to this instance after this operation for the purpose of closing etc. and you need to take care of the resulting instance only.
- Parameters:
limit
- Limit.- Returns:
- Modified iterator.
-
count
public long count()Get the count.Note: This is a terminal operation and the iterator is closed after this operation.
- Returns:
- Count.
-
count
-
sum
Get the sum.Note: This is a terminal operation and the iterator is closed after this operation.
- Parameters:
function
- Function to extract the value from the object.- Returns:
- Sum.
-
average
Get the average.Note: This is a terminal operation and the iterator is closed after this operation.
- Parameters:
function
- Function to extract the value from the object.- Returns:
- Average.
-
allMatch
-
anyMatch
-
noneMatch
-
forEach
-
consumeWhile
Consume objects while a given predicate is true.There is no need to maintain any references to this instance after this operation for the purpose of closing etc. and you need to take care of the resulting instance only.
- Parameters:
predicate
- Predicate to check.consumer
- Consumer.- Returns:
- An iterator containing the remaining entries.
-
skipWhile
Skip objects while a given predicate is true.There is no need to maintain any references to this instance after this operation for the purpose of closing etc. and you need to take care of the resulting instance only.
- Returns:
- An iterator containing the remaining entries.
-
max
Find the object that satisfies the "max" condition that specified by the given comparator.Note: This is a terminal operation and the iterator is closed after this operation.
- Parameters:
comparator
- Comparator.- Returns:
- Object instance if any found (or null).
-
min
Find the object that satisfies the "min" condition that specified by the given comparator.Note: This is a terminal operation and the iterator is closed after this operation.
- Parameters:
comparator
- Comparator.- Returns:
- Object instance if any found (or null).
-
select
Select an object based on the given bi-function (to compare with other entries).Note: This is a terminal operation and the iterator is closed after this operation.
- Parameters:
function
- Function to apply.- Returns:
- Selected object. Could be null if the iterator is already empty.
-
random
Select an object randomly. An object is guaranteed to be selected without any bias.Note: This is a terminal operation and the iterator is closed after this operation.
- Returns:
- Selected object. Could be null if the iterator is already empty.
-
collectAll
Deprecated.Collect all entries to a collection.Note: This is a terminal operation and the iterator is closed after this operation.
This method is deprecated, use
toList()
instead.- Returns:
- Collection.
-
collectAll
Collect all entries to a given collection.Note: This is a terminal operation and the iterator is closed after this operation.
- Parameters:
collection
- Collection to which entries to be added. If null is passed, a newCollection
instance is created.- Returns:
- Collection.
-
collectAll
Collect all entries to a collection after converting it to another type.Note: This is a terminal operation and the iterator is closed after this operation.
- Parameters:
convertor
- Converter.- Returns:
- Collection.
-
collectAll
Collect all entries to a given collection after converting it to another type.Note: This is a terminal operation and the iterator is closed after this operation.
- Parameters:
collection
- Collection to which entries to be added.convertor
- Converter.- Returns:
- Collection.
-
toList
-
toList
-
single
Get a single entry from this iterator (after applying the filter). Only a single entry is expected and if no entry is found or multiple entries are found, a run-time exception is thrown.Note: This is a terminal operation and the iterator is closed after this operation.
- Parameters:
filter
- Filter to apply.- Returns:
- The object.
-
single
Get a single entry from this iterator (after applying the filter). Only a single entry is expected and if no entry is found or multiple entries are found,null
will be returned (based on the "show error" parameter).Note: This is a terminal operation and the iterator is closed after this operation.
- Parameters:
filter
- Filter to apply.showError
- Whether to throw a run-time exception or not if no entry is found or multiple entries are found.- Returns:
- The object.
-
single
Get a single entry from this iterator. Only a single entry is expected and if no entry is found or multiple entries are found, a run-time exception is thrown.Note: This is a terminal operation and the iterator is closed after this operation.
- Returns:
- The object.
-
single
Get a single entry from this iterator. Only a single entry is expected and if no entry is found or multiple entries are found,null
will be returned (based on the "show error" parameter).Note: This is a terminal operation and the iterator is closed after this operation.
- Parameters:
showError
- Whether to throw a run-time exception or not if no entry is found or multiple entries are found.- Returns:
- The object.
-
create
Create an empty iterator.- Type Parameters:
T
- Type of object of the iterator to create.- Returns:
- Iterator created.
-
create
Create an iterator from a single object.- Type Parameters:
T
- Type of object of the iterator to create.- Parameters:
object
- Object to be included.- Returns:
- Iterator created.
-
create
Create an iterator from the given objects.- Type Parameters:
T
- Type of object of the iterator to create.- Parameters:
objects
- Objects to be included.- Returns:
- Iterator created.
-
create
Create an iterator from the given objects.- Type Parameters:
T
- Type of object of the iterator to create.- Parameters:
objects
- Objects to be included.- Returns:
- Iterator created.
-
create
Create an iterator from the given objects.- Type Parameters:
T
- Type of object of the iterator to create.- Parameters:
objects
- Objects to be included.- Returns:
- Iterator created.
-
create
Create an iterator from the given objects.- Type Parameters:
T
- Type of object of the iterator to create.- Parameters:
objects
- Objects to be included.- Returns:
- Iterator created.
-
create
public static <FROM extends StoredObject,TO extends StoredObject> ObjectIterator<TO> create(ObjectIterator<FROM> iterator, ObjectConverter<FROM, TO> converter) Create an iterator from the given iterator with conversion.- Type Parameters:
FROM
- Type if object of the source.TO
- Type of object of the iterator to create.- Parameters:
iterator
- Objects from the iterator to be included.converter
- Converter.- Returns:
- Iterator created.
-
create
public static <TO extends StoredObject,FROM> ObjectIterator<TO> create(Iterable<FROM> from, Function<FROM, TO> converter) Create an iterator from some source.- Type Parameters:
TO
- Type of object of the iterator to create.FROM
- Type of the source.- Parameters:
from
- Source.converter
- Converter to convert the source to an instance of TO.- Returns:
- Iterator created.
-
create
public static <TO extends StoredObject,FROM> ObjectIterator<TO> create(Stream<FROM> from, Function<FROM, TO> converter) Create an iterator from some source.- Type Parameters:
TO
- Type of object of the iterator to create.FROM
- Type of the source.- Parameters:
from
- Source.converter
- Converter to convert the source to an instance of TO.- Returns:
- Iterator created.
-
create
public static <TO extends StoredObject,FROM> ObjectIterator<TO> create(Iterator<FROM> from, Function<FROM, TO> converter) Create an iterator from some source.- Type Parameters:
TO
- Type of object of the iterator to create.FROM
- Type of the source.- Parameters:
from
- Source.converter
- Converter to convert the source to an instance of TO.- Returns:
- Iterator created.
-