Package it.tidalwave.util
Class CollectionUtils
java.lang.Object
it.tidalwave.util.CollectionUtils
This class contains a bunch of utility methods for manipulating lists.
- Since:
- 3.2-ALPHA-13
- Author:
- Fabrizio Giudici
- Status: stable API
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> List
<T> Appends a list to an object.static <T> List
<T> concatAll
(Collection<? extends T>... collections) Returns a concatenation of the givenCollection
s.static <T> T
Returns the first element of a list.static <T> Optional
<T> optionalHead
(List<? extends T> list) Returns the (optional) first element of a list.static <T> List
<T> Reverses a list.static <T> List
<T> safeSubList
(List<? extends T> list, int from, int to) static <T extends Comparable<? super T>>
List<T> Sorts a list.static <T> List
<T> sorted
(List<? extends T> list, Comparator<? super T> comparator) Sorts a list with a givenComparator
.Splits a givenList
at a set of boundaries.static <T> List
<T> Returns the tail element of a list, that is a list without the first element.
-
Constructor Details
-
CollectionUtils
public CollectionUtils()
-
-
Method Details
-
concat
Appends a list to an object. The resulting list is mutable.- Type Parameters:
T
- the type of list items- Parameters:
list
- the listobject
- the list to append- Returns:
- the list with the appended object
-
concatAll
@Nonnull @SafeVarargs public static <T> List<T> concatAll(@Nonnull Collection<? extends T>... collections) Returns a concatenation of the givenCollection
s.- Type Parameters:
T
- the static type- Parameters:
collections
- the input collections- Returns:
- the concatenation
-
reversed
Reverses a list. The resulting list is mutable.- Type Parameters:
T
- the type of list items- Parameters:
list
- the list- Returns:
- the reversed list
-
sorted
@Nonnull public static <T extends Comparable<? super T>> List<T> sorted(@Nonnull List<? extends T> list) Sorts a list. The resulting list is mutable.- Type Parameters:
T
- the type of list items- Parameters:
list
- the list- Returns:
- the sorted list
- Since:
- 3.2-ALPHA-13
-
sorted
@Nonnull public static <T> List<T> sorted(@Nonnull List<? extends T> list, @Nonnull Comparator<? super T> comparator) Sorts a list with a givenComparator
. The resulting list is mutable.- Type Parameters:
T
- the type of list items- Parameters:
list
- the listcomparator
- the comparator- Returns:
- the sorted list
- Since:
- 3.2-ALPHA-13
-
optionalHead
Returns the (optional) first element of a list.- Type Parameters:
T
- the type of list items- Parameters:
list
- the list- Returns:
- the first element
-
head
Returns the first element of a list.- Type Parameters:
T
- the type of list items- Parameters:
list
- the list (cannot be empty)- Returns:
- the first element
- Throws:
IllegalArgumentException
- if the list is empty
-
tail
Returns the tail element of a list, that is a list without the first element. The tail of an empty list is an empty list. The resulting list is mutable.- Type Parameters:
T
- the type of list items- Parameters:
list
- the list- Returns:
- the tail of the list
-
safeSubList
Return a sublist of the originalList
, from the givenfrom
andto
index (not included). If thefrom
index is negative and/or theto
index is lower than thefrom
index or if an attempt is made to read before the start or past the end of the list, truncation silently occurs.- Type Parameters:
T
- the static type- Parameters:
list
- the original listfrom
- the first index (included)to
- the last index (excluded)- Returns:
- the sublist
- Since:
- 3.2-ALPHA-17
-
split
Splits a givenList
at a set of boundaries. Each boundary is the starting point of a sublist to be returned.- Type Parameters:
T
- the static type- Parameters:
list
- the original listboundaries
- the boundaries- Returns:
- a list of sublists
- Since:
- 3.2-ALPHA-17
-