Package org.kiwiproject.collect
Class KiwiStreams
java.lang.Object
org.kiwiproject.collect.KiwiStreams
Utilities related to Streams that are not already in the JDKs
Stream
or Guava's Streams.-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Optional<T>Find the first object having the giventypeToFindin a stream of objects.static <T> Optional<T>Find the first object having the giventypeToFindand matching the supplied predicate in a stream of objects.static <T> Stream<T>stream(Collection<T> collection, KiwiStreams.StreamMode mode)
-
Method Details
-
findFirst
Find the first object having the giventypeToFindin a stream of objects.- Type Parameters:
T- the type token of the type we want to find- Parameters:
stream- the stream of objects of some (unknown) typetypeToFind- the class of the object to find- Returns:
- an Optional containing the first object of the given type, or empty
-
findFirst
public static <T> Optional<T> findFirst(Stream<?> stream, Class<T> typeToFind, Predicate<T> predicate) Find the first object having the giventypeToFindand matching the supplied predicate in a stream of objects.- Type Parameters:
T- the type token of the type we want to find- Parameters:
stream- the stream of objects of some (unknown) typetypeToFind- the class of the object to findpredicate- the condition that must be satisfied for a match to occur- Returns:
- an Optional containing the first object of the given type, or empty
-
stream
Return a sequential or parallelStreamovercollectionbased on the givenKiwiStreams.StreamMode.Normally you must hard code the type of stream directly in the code using either
Collection.stream()orCollection.parallelStream(). This method provides a simple way to determine how to process a stream at runtime, for example based on number of elements or type of algorithm.- Type Parameters:
T- the type of collection elements- Parameters:
collection- the collection to streammode- the mode to use when streaming the collection- Returns:
- a sequential or parallel
Streamovercollection
-