Interface ICollectionTools
public interface ICollectionTools
ICollectionTools offers default implementations of Collection functionality.
- See Also:
-
Method Summary
Static MethodsModifier and TypeMethodDescriptionstatic <E> boolean
containsIf
(Collection<E> coll, Predicate<? super E> predicate) Determines whether the list contains a matching element.static <E> int
countIf
(Collection<E> coll, Predicate<? super E> predicate) Counts how many elements in the list match the predicate.static <E,
C extends Collection<E>>
Cfilter
(Collection<E> coll, Predicate<? super E> predicate, Supplier<C> factory) Create a new list by applying the specified filter to all elements.static <E,
R, C extends Collection<R>>
IList<R> filterMap
(Collection<E> coll, Predicate<E> filter, Function<E, R> func) Create a new list by applying the specified filter first and then the mapping function to all elements selected.static <E,
R, C extends Collection<R>>
CfilterMap
(Collection<E> coll, Predicate<E> filter, Function<E, R> func, Supplier<C> factory) Create a new collection by applying the specified filter first and then the mapping function to all elements selected.static <E,
R, RC extends Collection<R>, C extends Collection<R>>
CflatMap
(Collection<E> coll, Function<E, RC> func, Supplier<C> factory) Create a new list by applying the specified mapping function to all elements.static <E> E
getFirst
(Collection<E> coll) Returns the first element stored in the collection.static <E> E
getFirstOrNull
(Collection<E> coll) Returns the first element stored in the collection.static <E> E
getIf
(Collection<E> coll, Predicate<? super E> predicate) Returns the first element stored in the collection which matches the predicate.static <E> E
getSingle
(Collection<E> coll) Returns the only element stored in the collection.static <E> E
getSingleOrNull
(Collection<E> coll) Returns the only element stored in the collection or null if the collection is empty.static <E,
R, C extends Collection<R>>
Cmap
(Collection<E> coll, Function<E, R> func, Supplier<C> factory) Create a new list by applying the specified mapping function to all elements.static <E,
R> IList <R> mapFilter
(Collection<E> coll, Function<E, R> func, Predicate<R> filter) Create a new list by applying the specified mapping function to all elements and then filtering it.static <E,
R, C extends Collection<R>>
CmapFilter
(Collection<E> coll, Function<E, R> func, Predicate<R> filter, Supplier<C> factory) Create a new collection by applying the specified mapping function to all elements and then filtering it.
-
Method Details
-
getFirst
Returns the first element stored in the collection. If the collection is empty, aNoSuchElementException
is thrown.- Returns:
- first element stored in the collection
-
getFirstOrNull
Returns the first element stored in the collection. If the collection is empty, null is returned.- Returns:
- first element stored in the collection or null if empty
-
getSingle
Returns the only element stored in the collection. If the collection size is not 1, aNoSuchElementException
is thrown.- Returns:
- only element stored in the collection
-
getSingleOrNull
Returns the only element stored in the collection or null if the collection is empty. If the collection's size is greater than 1, aNoSuchElementException
is thrown.- Returns:
- only element stored in the collection or null if empty
-
getIf
Returns the first element stored in the collection which matches the predicate. If the collection is empty of no element matches, null is returned.- Returns:
- first element matching the predicate or null if not founds
-
containsIf
Determines whether the list contains a matching element.- Parameters:
predicate
- predicate used to search element- Returns:
- true if the list contains a matching element, false otherwise
-
countIf
Counts how many elements in the list match the predicate.- Parameters:
predicate
- a predicate which returnstrue
for elements to be counted- Returns:
- count how many elements in the list match the predicate
-
filter
static <E,C extends Collection<E>> C filter(Collection<E> coll, Predicate<? super E> predicate, Supplier<C> factory) Create a new list by applying the specified filter to all elements.- Parameters:
predicate
- filter predicate- Returns:
- created list
-
map
static <E,R, C mapC extends Collection<R>> (Collection<E> coll, Function<E, R> func, Supplier<C> factory) Create a new list by applying the specified mapping function to all elements.- Parameters:
func
- mapping function- Returns:
- created list
-
flatMap
static <E,R, C flatMapRC extends Collection<R>, C extends Collection<R>> (Collection<E> coll, Function<E, RC> func, Supplier<C> factory) Create a new list by applying the specified mapping function to all elements.- Parameters:
func
- mapping function- Returns:
- created list
-
mapFilter
Create a new list by applying the specified mapping function to all elements and then filtering it.- Parameters:
func
- mapping functionfilter
- filter predicate- Returns:
- created list
-
mapFilter
static <E,R, C mapFilterC extends Collection<R>> (Collection<E> coll, Function<E, R> func, Predicate<R> filter, Supplier<C> factory) Create a new collection by applying the specified mapping function to all elements and then filtering it.- Parameters:
func
- mapping functionfilter
- filter predicatefactory
- factory to create collection- Returns:
- created list
-
filterMap
static <E,R, IList<R> filterMapC extends Collection<R>> (Collection<E> coll, Predicate<E> filter, Function<E, R> func) Create a new list by applying the specified filter first and then the mapping function to all elements selected.- Parameters:
filter
- filter predicatefunc
- mapping function- Returns:
- created list
-
filterMap
static <E,R, C filterMapC extends Collection<R>> (Collection<E> coll, Predicate<E> filter, Function<E, R> func, Supplier<C> factory) Create a new collection by applying the specified filter first and then the mapping function to all elements selected.- Parameters:
filter
- filter predicatefunc
- mapping functionfactory
- factory to create collection- Returns:
- created list
-