public final class CollectionUtil extends Object
Constructor and Description |
---|
CollectionUtil() |
Modifier and Type | Method and Description |
---|---|
static <T> boolean |
contains(Iterable<? extends T> iterable,
Predicate<T> predicate)
Does the supplied
Iterable contain anything that matches the predicate? |
static <T> boolean |
contains(Iterator<? extends T> iterator,
Predicate<T> predicate)
Does the supplied
Iterator contain anything that matches the predicate? |
static <T> List<T> |
copyAsImmutableList(Collection<? extends T> copy)
Return an immutable list copy of the passed collection.
|
static <K,V> Map<K,V> |
copyAsImmutableMap(Map<? extends K,? extends V> copy)
Return an immutable copy of the passed map.
|
static <T> Set<T> |
copyAsImmutableSet(Collection<? extends T> copy)
Return an immutable set copy of the passed collection.
|
static <T> Collection<T> |
filter(Collection<T> collection,
Predicate<? super T> predicate)
Deprecated.
Do not use this method as this returns a live view which in many cases leads to a poor performance. Since v6.4
|
static <T> Iterable<T> |
filter(Iterable<T> iterable,
Predicate<? super T> predicate)
Create a filtered
Iterable . |
static <T> Iterator<T> |
filter(Iterator<T> iterator,
Predicate<? super T> predicate)
Create a filtered
Iterator . |
static <T,R extends T> |
filterByType(Iterable<T> iterable,
Class<R> subclass)
Filter a
Collection for the specified subtype. |
static <T> T |
findFirstMatch(Iterable<? extends T> iterable,
Predicate<T> predicate)
Return the first found element that the predicate matches.
|
static <T> T |
first(Iterable<? extends T> iterable)
Get the first element of a an
Iterable in iteration order, or null if empty. |
static <T> void |
foreach(Iterable<T> iterable,
Consumer<T> consumer)
For each element in the iterator, consume the contents.
|
static <T> void |
foreach(Iterator<? extends T> iterator,
Consumer<T> consumer)
For each element in the iterator, consume the contents.
|
static <T> int |
indexOf(Iterable<? extends T> iterable,
Predicate<? super T> predicate)
Returns the index of the first element that matches the predicate.
|
static <K,R,S> Map<K,S> |
map(Map<K,R> map,
Function<R,S> mapper)
Take a map and eagerly transform all values into a new, immutable Map.
|
static <T> List<T> |
sort(Collection<? extends T> collection,
Comparator<T> comparator)
Copy and sort the passed collection and return an unmodifiable
List of the elements. |
static <T> List<T> |
toList(Enumeration<? extends T> enumeration)
Turn the enumeration into a list.
|
static <T> List<T> |
toList(Iterable<? extends T> iterable)
Turn the iterable into a list.
|
static <T> List<T> |
toList(Iterator<? extends T> iterator)
Turn the iterator into a list.
|
static <T> Set<T> |
toSet(Iterable<? extends T> iterable)
Turn the iterable into a Set.
|
static <T> Set<T> |
toSet(Iterator<? extends T> iterator)
Turn the iterable into a Set.
|
static <T,R> List<R> |
transform(Iterable<? extends T> iterable,
Function<T,R> transformer)
Return a
List that is transformed from elements of the input type
to elements of the output type by a transformer function. |
static <T,R> List<R> |
transform(Iterator<? extends T> iterator,
Function<T,R> transformer)
Return a List that is transformed from elements of the input type to elements of the output type by a transformer function.
|
static <T,R> Iterable<R> |
transformAndFilter(Iterable<T> iterable,
Function<T,R> transformer,
Predicate<R> predicate) |
static <T,R> Iterator<R> |
transformIterator(Iterator<? extends T> iterator,
Function<T,R> transformer)
Return an
Iterator that is transformed from elements of the input
type to elements of the output type by a transformer function. |
static <T,R> Set<R> |
transformSet(Iterable<T> iterable,
Function<T,R> transformer)
Return a
Set that is transformed from elements of the input type
to elements of the output type by a transformer function. |
public static <T> void foreach(@Nonnull Iterator<? extends T> iterator, @Nonnull Consumer<T> consumer)
T
- the type of element to consumeiterator
- to iterate over the elementsconsumer
- to consume the elementspublic static <T> void foreach(Iterable<T> iterable, @Nonnull Consumer<T> consumer)
T
- the element typeiterable
- to iterate over the elements, may be nullconsumer
- to consume the elementspublic static <T> List<T> toList(@Nonnull Iterator<? extends T> iterator)
T
- the element typeiterator
- to iterate over the elementsList
of the elements in the iteratorpublic static <T> List<T> toList(@Nonnull Iterable<? extends T> iterable)
T
- the element typeiterable
- to iterate over the elementsList
of the elements in the iteratorpublic static <T> List<T> toList(@Nonnull Enumeration<? extends T> enumeration)
T
- the element typeenumeration
- to enumerate over the elementsList
of the elements in the iteratorpublic static <T> Set<T> toSet(@Nonnull Iterable<? extends T> iterable)
T
- the element typeiterable
- to iterate over the elementsSet
of the elements in the iteratorpublic static <T> Set<T> toSet(@Nonnull Iterator<? extends T> iterator)
T
- the element typeiterator
- to iterate over the elementsSet
of the elements in the iteratorpublic static <T,R> List<R> transform(@Nonnull Iterator<? extends T> iterator, @Nonnull Function<T,R> transformer)
T
- the input typeR
- the out typeiterator
- to iterate over the contentstransformer
- the function that performs the transformationpublic static <T,R> List<R> transform(@Nonnull Iterable<? extends T> iterable, @Nonnull Function<T,R> transformer)
List
that is transformed from elements of the input type
to elements of the output type by a transformer function.
Note, this performs a copy and applies the transform to all elements. If you want a lazily applied function, see Transform
T
- the input typeR
- the out typeiterable
- the contentstransformer
- the function that performs the transformationpublic static <T,R> Iterator<R> transformIterator(@Nonnull Iterator<? extends T> iterator, @Nonnull Function<T,R> transformer)
Iterator
that is transformed from elements of the input
type to elements of the output type by a transformer function.T
- the input typeR
- the out typeiterator
- the contentstransformer
- the function that performs the transformationIterator
of the transformed typepublic static <T,R> Set<R> transformSet(@Nonnull Iterable<T> iterable, @Nonnull Function<T,R> transformer)
Set
that is transformed from elements of the input type
to elements of the output type by a transformer function.
Note, this performs a copy and applies the transform to all elements.
T
- the input typeR
- the output typeiterable
- the contentstransformer
- the function that performs the transformationpublic static <T> boolean contains(@Nonnull Iterator<? extends T> iterator, @Nonnull Predicate<T> predicate)
Iterator
contain anything that matches the predicate?T
- the element typeiterator
- containing elementspredicate
- the matcherpublic static <T> boolean contains(@Nonnull Iterable<? extends T> iterable, @Nonnull Predicate<T> predicate)
Iterable
contain anything that matches the predicate?T
- the element typeiterable
- containing elementspredicate
- the matcherpublic static <T> Iterator<T> filter(@Nonnull Iterator<T> iterator, @Nonnull Predicate<? super T> predicate)
Iterator
.T
- the element typeiterator
- an iterator that only returns elements approved by the predicatepredicate
- for checking the elementsIterator
public static <T> Iterable<T> filter(@Nonnull Iterable<T> iterable, @Nonnull Predicate<? super T> predicate)
Iterable
.T
- the element typeiterable
- an iterable whose iterators only returns elements approved by the predicatepredicate
- for checking the elementsIterable
public static <T,R> Iterable<R> transformAndFilter(Iterable<T> iterable, Function<T,R> transformer, Predicate<R> predicate)
@Deprecated public static <T> Collection<T> filter(@Nonnull Collection<T> collection, @Nonnull Predicate<? super T> predicate)
Collection
.T
- the element typecollection
- an iterable whose iterators only returns elements approved by the predicatepredicate
- for checking the elementsIterable
public static <T,R extends T> Collection<R> filterByType(@Nonnull Iterable<T> iterable, @Nonnull Class<R> subclass)
Collection
for the specified subtype.T
- the incoming typeR
- the result typeiterable
- an iterable whose values are of the source typesubclass
- the result type, only return elements if they are of this typeCollection
of the subtypepublic static <T> List<T> sort(@Nonnull Collection<? extends T> collection, @Nonnull Comparator<T> comparator)
List
of the elements.T
- the element typecollection
- the collection to copycomparator
- for sortingList
view of the elements.public static <T> List<T> copyAsImmutableList(@Nonnull Collection<? extends T> copy)
T
- the type of elements for the returned collection.copy
- the collection to copy.public static <T> Set<T> copyAsImmutableSet(@Nonnull Collection<? extends T> copy)
T
- the type of elements for the returned collection.copy
- the collection to copy.public static <K,V> Map<K,V> copyAsImmutableMap(@Nonnull Map<? extends K,? extends V> copy)
K
- the type of key in the returned map.V
- the type of value in the returned map.copy
- the map to copy.public static <T> T findFirstMatch(@Nonnull Iterable<? extends T> iterable, Predicate<T> predicate)
T
- the type of element to returniterable
- that may contain the element to returnpredicate
- to match the desired elementpublic static <T> int indexOf(@Nonnull Iterable<? extends T> iterable, @Nonnull Predicate<? super T> predicate)
T
- the type of the elementsiterable
- collection of elementspredicate
- to match the desired elementpublic static <T> T first(@Nonnull Iterable<? extends T> iterable)
Iterable
in iteration order, or null if empty.T
- the typeiterable
- the thing to get something from.public static <K,R,S> Map<K,S> map(Map<K,R> map, Function<R,S> mapper)
K
- the key typeR
- the original value typeS
- the new value typemap
- the original mapmapper
- the mapping functionfor a lazy version.
Copyright © 2002-2022 Atlassian. All Rights Reserved.