Class CollectionHelper
java.lang.Object
io.github.torand.javacommons.collection.CollectionHelper
Helper functions for collections
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> List
<T> Creates a list of specified iterable items.static <T> List
<T> asList
(T... items) Creates a list of specified item array.static <T> List
<T> asNonEmptyList
(T first, T... others) Creates a list of at least one, possibly multiple items.static <T> List
<T> Concatenates the specified iterables into a single list.static <T> List
<T> Concatenates the elements of the specified iterable and array into a single list.static <T> Stream
<T> concatStream
(Iterable<T> first, Iterable<T> second) Creates a concatenated stream from the elements in the specified iterables.static <T> boolean
containsAny
(Iterable<T> iterable, Predicate<T> predicate) Tests whether the specified iterable contains an element satisfying a predicate.static <T> boolean
containsOneOf
(Iterable<T> iterable, T... items) Tests whether the specified iterable contains one of the specified items.static <T> T
Returns the first element of the specified iterable.static boolean
isEmpty
(Collection<?> collection) Returns whether the specified collection is null or contains no elements.static boolean
Returns whether the specified map is null or contains no elements.static boolean
nonEmpty
(Collection<?> collection) Returns whether the specified collection contains at least one element.static boolean
Returns whether the specified map contains at least one element.static <T> Stream
<T> streamSafely
(Iterable<T> iterable) Creates a stream from the elements in the specified iterable.static <T> Stream
<T> streamSafely
(Iterator<T> iterator) Creates a stream from the elements in the specified iterator.static <T> Stream
<T> streamSafely
(T... items) Creates a stream from the elements in the specified array.static <T> T
Returns the last element of the specified iterable.
-
Method Details
-
asList
Creates a list of specified iterable items.- Type Parameters:
T
- the item type.- Parameters:
items
- the item iterable.- Returns:
- the list.
-
asList
Creates a list of specified item array.- Type Parameters:
T
- the item type.- Parameters:
items
- the item array.- Returns:
- the list.
-
asNonEmptyList
Creates a list of at least one, possibly multiple items.- Type Parameters:
T
- the item type.- Parameters:
first
- the first item, which cannot be null.others
- the additional items, which can be null or empty.- Returns:
- the list.
-
isEmpty
Returns whether the specified collection is null or contains no elements.- Parameters:
collection
- the collection.- Returns:
- true if the collection is null or contains no elements; else false.
-
isEmpty
Returns whether the specified map is null or contains no elements.- Parameters:
map
- the map.- Returns:
- true if the map is null or contains no elements; else false.
-
nonEmpty
Returns whether the specified collection contains at least one element.- Parameters:
collection
- the collection.- Returns:
- true if the collection contains at least one element; else false.
-
nonEmpty
Returns whether the specified map contains at least one element.- Parameters:
map
- the map.- Returns:
- true if the map contains at least one element; else false.
-
streamSafely
Creates a stream from the elements in the specified iterable. If the iterable is null or contains no elements, an empty stream is returned.- Type Parameters:
T
- the element type.- Parameters:
iterable
- the iterable.- Returns:
- the stream.
-
streamSafely
Creates a stream from the elements in the specified iterator. If the iterator is null or contains no elements, an empty stream is returned.- Type Parameters:
T
- the element type.- Parameters:
iterator
- the iterator.- Returns:
- the stream.
-
streamSafely
Creates a stream from the elements in the specified array. If the array is null or contains no elements, an empty stream is returned.- Type Parameters:
T
- the item type.- Parameters:
items
- the items.- Returns:
- the stream.
-
concatStream
Creates a concatenated stream from the elements in the specified iterables.- Type Parameters:
T
- the element type.- Parameters:
first
- the first iterable.second
- the second iterable.- Returns:
- the stream.
-
concat
Concatenates the specified iterables into a single list.- Type Parameters:
T
- the element type.- Parameters:
first
- the first iterable.second
- the second iterable.- Returns:
- the list.
-
concat
Concatenates the elements of the specified iterable and array into a single list.- Type Parameters:
T
- the element type.- Parameters:
first
- the iterable.second
- the array.- Returns:
- the list.
-
headOf
Returns the first element of the specified iterable.- Type Parameters:
T
- the element type.- Parameters:
iterable
- the iterable.- Returns:
- the first element of the iterable.
-
tailOf
Returns the last element of the specified iterable.- Type Parameters:
T
- the element type.- Parameters:
iterable
- the iterable.- Returns:
- the lastelement of the iterable.
-
containsAny
Tests whether the specified iterable contains an element satisfying a predicate.- Type Parameters:
T
- the element type.- Parameters:
iterable
- the iterable.predicate
- the predicate.- Returns:
- true if the iterable contains an element satisfying the predicate; else false.
-
containsOneOf
Tests whether the specified iterable contains one of the specified items.- Type Parameters:
T
- the element type.- Parameters:
iterable
- the iterable.items
- the items to look for.- Returns:
- true if the iterable contains one of the specified items; else false.
-