Package org.jdbi.v3.core.result
Interface ResultIterable<T>
-
- Type Parameters:
T- iterable element type
- All Superinterfaces:
java.lang.Iterable<T>
- All Known Subinterfaces:
BatchResultIterable<T>
- All Known Implementing Classes:
ResultSetResultIterable
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
@FunctionalInterface public interface ResultIterable<T> extends java.lang.Iterable<T>AnIterableof values, usually mapped from aResultSet. Generally, ResultIterables may only be traversed once.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static classResultIterable.ResultIteratorDelegate<T,R>An implementation ofResultIteratorthat delegates calls to the iterator provided in the constructor.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Deprecated Methods Modifier and Type Method Description default <R> Rcollect(java.util.stream.Collector<? super T,?,R> collector)Collect the results into a container specified by a collector.default <R extends java.util.Collection<? super T>>
RcollectInto(java.lang.reflect.Type containerType)Collect the results into a container type.default <R extends java.util.Collection<? super T>>
RcollectInto(GenericType<R> containerType)Collect the results into a collection type.default java.util.List<T>collectIntoList()Returns results in aList.default java.util.Set<T>collectIntoSet()Returns results in aSet.default <K,V>
java.util.Map<K,V>collectToMap(java.util.function.Function<? super T,? extends K> keyFunction, java.util.function.Function<? super T,? extends V> valueFunction)Collect the results into a Map, using the given functions to compute keys and values.default ResultIterable<T>filter(java.util.function.Predicate<? super T> predicate)Convenience method to filter theResultIterableby applying the specifiedPredicate.
This method has the look and feel ofStream.filter(Predicate)without making use of streams.default java.util.Optional<T>findFirst()Returns the first row in the result set, if present.default java.util.Optional<T>findOne()Returns the only row in the result set, if any.default TfindOnly()default Tfirst()Returns the first row in the result set.default voidforEach(java.util.function.Consumer<? super T> action)default intforEachWithCount(java.util.function.Consumer<? super T> action)Performs the specified action on each remaining element and returns the iteration i.e.ResultIterator<T>iterator()Stream all the rows of the result set out with anIterator.default java.util.List<T>list()Returns results in aListusing the JDK specific default implementation ofListas provided byCollectors.toList().default <R> ResultIterable<R>map(java.util.function.Function<? super T,? extends R> mapper)Returns aResultIterable<U>derived from thisResultIterable<T>, by transforming elements using the given mapper function.static <T> ResultIterable<T>of(java.util.function.Supplier<java.sql.ResultSet> resultSetSupplier, RowMapper<T> mapper, StatementContext ctx)Returns a ResultIterable backed by the given result set supplier, mapper, and context.static <T> ResultIterable<T>of(ResultIterator<T> iterator)Returns a ResultIterable backed by the given iterator.default Tone()Returns the only row in the result set.default <U> Ureduce(U identity, java.util.function.BiFunction<U,T,U> accumulator)Reduce the results.default java.util.Set<T>set()Returns results in aSetusing the JDK specific default implementation ofSetas provided byCollectors.toSet().default java.util.stream.Stream<T>stream()Returns the stream of results.default <C extends java.util.Collection<T>>
CtoCollection(java.util.function.Supplier<C> supplier)Collect the results into a collection object similar toCollectors.toCollection(Supplier).default <X extends java.lang.Exception>
voiduseIterator(IteratorConsumer<T,X> consumer)Passes the iterator of results to the consumer.default <X extends java.lang.Exception>
voiduseStream(StreamConsumer<T,X> consumer)Passes the stream of results to the consumer.default <R,X extends java.lang.Exception>
RwithIterator(IteratorCallback<T,R,X> callback)Passes the iterator of results to the callback.default <R,X extends java.lang.Exception>
RwithStream(StreamCallback<T,R,X> callback)Passes the stream of results to the callback.
-
-
-
Method Detail
-
of
static <T> ResultIterable<T> of(java.util.function.Supplier<java.sql.ResultSet> resultSetSupplier, RowMapper<T> mapper, StatementContext ctx)
Returns a ResultIterable backed by the given result set supplier, mapper, and context.- Type Parameters:
T- the mapped type- Parameters:
resultSetSupplier- result set suppliermapper- row mapperctx- statement context- Returns:
- the result iterable
-
of
static <T> ResultIterable<T> of(ResultIterator<T> iterator)
Returns a ResultIterable backed by the given iterator.- Type Parameters:
T- iterator element type- Parameters:
iterator- the result iterator- Returns:
- a ResultIterable
-
iterator
ResultIterator<T> iterator()
Stream all the rows of the result set out with anIterator. TheIteratormust be closed to release database resources.- Specified by:
iteratorin interfacejava.lang.Iterable<T>- Returns:
- the results as a streaming Iterator
-
useIterator
default <X extends java.lang.Exception> void useIterator(IteratorConsumer<T,X> consumer) throws X extends java.lang.Exception
Passes the iterator of results to the consumer. Database resources owned by the query are released before this method returns.- Parameters:
consumer- a consumer which receives the iterator of results.- Throws:
X- any exception thrown by the callbackX extends java.lang.Exception
-
withIterator
default <R,X extends java.lang.Exception> R withIterator(IteratorCallback<T,R,X> callback) throws X extends java.lang.Exception
Passes the iterator of results to the callback. Database resources owned by the query are released before this method returns.- Type Parameters:
R- the type returned by the callback- Parameters:
callback- a callback which receives the iterator of results, and returns some result.- Returns:
- the value returned by the callback.
- Throws:
X- any exception thrown by the callbackX extends java.lang.Exception
-
map
default <R> ResultIterable<R> map(java.util.function.Function<? super T,? extends R> mapper)
Returns aResultIterable<U>derived from thisResultIterable<T>, by transforming elements using the given mapper function.- Type Parameters:
R- Element type of the returned ResultIterable- Parameters:
mapper- function to apply to elements of this ResultIterable- Returns:
- the new ResultIterable
-
forEach
default void forEach(java.util.function.Consumer<? super T> action)
- Specified by:
forEachin interfacejava.lang.Iterable<T>
-
forEachWithCount
default int forEachWithCount(java.util.function.Consumer<? super T> action)
Performs the specified action on each remaining element and returns the iteration i.e. record count.
It is often useful (e.g. for logging) to know the record count while processing result sets.int cnt = h.createQuery("select * from something").mapTo(String.class) .forEachWithCount(System.out::println); System.out.println(cnt + " records selected");- Parameters:
action- action to apply (required)- Returns:
- iteration count
- Since:
- 3.31
-
one
default T one()
Returns the only row in the result set. Returnsnullif the row itself isnull.- Returns:
- the only row in the result set.
- Throws:
java.lang.IllegalStateException- if the result set contains zero or multiple rows
-
findOne
default java.util.Optional<T> findOne()
Returns the only row in the result set, if any. ReturnsOptional.empty()if zero rows are returned, or if the row itself isnull.- Returns:
- the only row in the result set, if any.
- Throws:
java.lang.IllegalStateException- if the result set contains multiple rows
-
findOnly
@Deprecated default T findOnly()
Get the only row in the result set.- Returns:
- the object mapped from the singular row in the results
- Throws:
java.lang.IllegalStateException- if zero or multiple rows are returned
-
first
default T first()
Returns the first row in the result set. Returnsnullif the row itself isnull.- Returns:
- the first row in the result set.
- Throws:
java.lang.IllegalStateException- if zero rows are returned
-
findFirst
default java.util.Optional<T> findFirst()
Returns the first row in the result set, if present. ReturnsOptional.empty()if zero rows are returned or the first row isnull.- Returns:
- the first row in the result set, if present.
-
stream
default java.util.stream.Stream<T> stream()
Returns the stream of results.Note: the returned stream owns database resources, and must be closed via a call to
BaseStream.close(), or by using the stream in a try-with-resources block:try (Stream<T> stream = query.stream()) { // do stuff with stream }- Returns:
- the stream of results.
- See Also:
useStream(StreamConsumer),withStream(StreamCallback)
-
useStream
default <X extends java.lang.Exception> void useStream(StreamConsumer<T,X> consumer) throws X extends java.lang.Exception
Passes the stream of results to the consumer. Database resources owned by the query are released before this method returns.- Type Parameters:
X- the exception type thrown by the callback, if any- Parameters:
consumer- a consumer which receives the stream of results.- Throws:
X- any exception thrown by the callbackX extends java.lang.Exception
-
withStream
default <R,X extends java.lang.Exception> R withStream(StreamCallback<T,R,X> callback) throws X extends java.lang.Exception
Passes the stream of results to the callback. Database resources owned by the query are released before this method returns.- Type Parameters:
R- the type returned by the callbackX- the exception type thrown by the callback, if any- Parameters:
callback- a callback which receives the stream of results, and returns some result.- Returns:
- the value returned by the callback.
- Throws:
X- any exception thrown by the callbackX extends java.lang.Exception
-
list
default java.util.List<T> list()
Returns results in aListusing the JDK specific default implementation ofListas provided byCollectors.toList(). The same limitations apply to the list instance returned from this method. If more control over the list type is required, usetoCollection(Supplier)or see thecollectIntoList()method.- Returns:
- results in a
List - See Also:
collectIntoList(),toCollection(Supplier)
-
set
default java.util.Set<T> set()
Returns results in aSetusing the JDK specific default implementation ofSetas provided byCollectors.toSet(). The same limitations apply to the set instance returned from this method. If more control over the set type is required, usetoCollection(Supplier)or see thecollectIntoSet()method.- Returns:
- results in a
Set - Since:
- 3.38.0
- See Also:
collectIntoSet(),toCollection(Supplier)
-
collect
default <R> R collect(java.util.stream.Collector<? super T,?,R> collector)
Collect the results into a container specified by a collector.- Type Parameters:
R- the generic type of the container- Parameters:
collector- the collector- Returns:
- the container with the query result
-
collectToMap
default <K,V> java.util.Map<K,V> collectToMap(java.util.function.Function<? super T,? extends K> keyFunction, java.util.function.Function<? super T,? extends V> valueFunction)
Collect the results into a Map, using the given functions to compute keys and values.- Type Parameters:
K- the key typeV- the value type- Parameters:
keyFunction- a function that transforms the query result to a map keyvalueFunction- a function that transforms the query result to a map value- Returns:
- the collected Map
- Since:
- 3.38.0
-
toCollection
@Alpha default <C extends java.util.Collection<T>> C toCollection(java.util.function.Supplier<C> supplier)
Collect the results into a collection object similar toCollectors.toCollection(Supplier).- Parameters:
supplier- a supplier providing a new empty Collection into which the results will be inserted- Returns:
- A new collection with all results inserted
- Since:
- 3.38.0
-
collectInto
@Alpha default <R extends java.util.Collection<? super T>> R collectInto(java.lang.reflect.Type containerType)
Collect the results into a container type.- Parameters:
containerType- ATypeobject that must describe a container type- Returns:
- A new collection implementing the container type with all results inserted
- Throws:
java.lang.UnsupportedOperationException- if the implementation does not support this operation- Since:
- 3.38.0
-
collectInto
@Alpha default <R extends java.util.Collection<? super T>> R collectInto(GenericType<R> containerType)
Collect the results into a collection type.- Parameters:
containerType- AGenericTypeobject that describes a collection type- Returns:
- A new collection implementing the container type with all results inserted
- Throws:
java.lang.UnsupportedOperationException- if the implementation does not support this operation- Since:
- 3.38.0
-
collectIntoList
@Alpha default java.util.List<T> collectIntoList()
Returns results in aList. The implementation of the list can be changed by registering aCollector:
orjdbi.getConfig(JdbiCollectors.class).registerCollector(List.class, Collectors.toCollection(LinkedList::new));handle.registerCollector(List.class, Collectors.toCollection(LinkedList::new));
If no collector is registered, then this method behaves likelist().- Returns:
- results in a
List - Since:
- 3.38.0
- See Also:
list(),toCollection(Supplier)
-
collectIntoSet
@Alpha default java.util.Set<T> collectIntoSet()
Returns results in aSet. The implementation of the set can be changed by registering aCollector:
orjdbi.getConfig(JdbiCollectors.class).registerCollector(Set.class, Collectors.toCollection(LinkedHashSet::new));handle.registerCollector(Set.class, Collectors.toCollection(LinkedHashSet::new));
If no collector is registered, then this method behaves likeset().- Returns:
- results in a
Set - Since:
- 3.38.0
- See Also:
set(),toCollection(Supplier)
-
reduce
default <U> U reduce(U identity, java.util.function.BiFunction<U,T,U> accumulator)Reduce the results. Using aBiFunction<U, T, U>, repeatedly combine query results until only a single value remains.- Type Parameters:
U- the accumulator type- Parameters:
identity- theUto combine with the first resultaccumulator- the function to apply repeatedly- Returns:
- the final
U
-
filter
default ResultIterable<T> filter(java.util.function.Predicate<? super T> predicate)
Convenience method to filter theResultIterableby applying the specifiedPredicate.
This method has the look and feel ofStream.filter(Predicate)without making use of streams.Please note that filtering takes place in Java i.e. your client code, not in the database.
Filtering inside the database will most likely be of higher performance than filtering outside, as intermediate results are loaded into Java and then discarded. Moreover, indexes that may exist in the database will not be utilized here.- Parameters:
predicate- a non-null predicate to apply to each element to determine whether it should be included in the result- Returns:
- the new result iterable
- Since:
- 3.31
-
-