public class CollectionHelper
extends java.lang.Object
| Constructor and Description |
|---|
CollectionHelper() |
| Modifier and Type | Method and Description |
|---|---|
static <T> void |
addOrRemove(java.util.Collection<T> collection,
T value,
boolean add)
Adds or removes a value from the given collection, according to the given flag (true = add, false = remove)
|
static <T> Page<T> |
asPage(java.lang.Iterable<T> collection)
Returns a new
Page containing a copy of the given Collection as the total elements |
static <T> Page<T> |
asPage(java.util.List<T> all,
int pageSize,
int currentPage)
Applies the pagination to an in-memory list
|
static <T> Page<T> |
asPage(T... elements)
Returns a new
Page containing a copy of the given Collection as the total elements |
static <T> java.util.Set<T> |
asSet(java.util.List<T> list)
Returns the given list as a Set
|
static <T> java.util.Set<T> |
asSet(T... elements)
Returns the given elements as a Set
|
static <T> java.util.stream.Stream<T> |
asStream(java.lang.Iterable<T> iterable)
Returns a
Stream equivalent to the given Iterable |
static <T> java.util.stream.Stream<T> |
asStream(java.util.Iterator<T> iterator)
Returns a
Stream equivalent to the given iterator |
static <T> java.util.stream.Stream<T> |
asStream(T[] array)
Returns a
Stream equivalent to the given array |
static <T> boolean |
containsAll(java.util.Collection<T> first,
java.util.Collection<T> second) |
static boolean |
containsAny(java.util.Collection<?> collection,
java.util.Collection<?> toTest)
Checks whether the first collection contains at least one element of the second collection
|
static boolean |
containsAny(java.util.Collection<?> collection,
java.lang.Object... toTest)
Checks whether the first collection contains at least one element of the second collection
|
static boolean |
equals(java.util.Map<?,?> map1,
java.util.Map<?,?> map2)
Returns whether all keys and values are equals on both maps.
|
static <I,O extends I> |
filterByType(java.util.stream.Stream<I> stream,
java.lang.Class<O> subType)
Returns a filtered
Stream containing only the elements which are instances of the given subclass |
static <T> T |
first(java.lang.Iterable<T> collection)
Returns the first element in a collection, or null when the collection is empty or null
|
static <T> T |
first(T[] value)
Returns the first element in an array, or null if the array is null or empty
|
static java.lang.Object[] |
flatten(java.lang.Object... input)
Returns an array comprised of all elements of all objects in the given input.
|
static <K,V> V |
getOrSupply(java.util.Map<K,V> map,
K key,
java.util.function.Supplier<V> supplier)
Returns the given key from the Map.
|
static java.lang.Integer |
getTotalCount(java.lang.Iterable<?> collection)
Returns the total count of the given collection, handling pages with the
Page.getTotalCount() method. |
static int |
hashCode(java.util.Map<?,?> map)
Returns the hashCode of the given Map, by hashing each key and value
|
static boolean |
hasOnlyOneElement(java.util.Collection<?> collection)
Returns true if the collection has only one element, handling nulls.
|
static boolean |
isEmpty(java.lang.Iterable<?> collection)
Checks whether the given collection is null or empty
|
static boolean |
isEmpty(java.util.Map<?,?> map)
Checks whether the given map is null or empty
|
static boolean |
isEmpty(java.lang.Object[] array)
Checks whether the given array is null or empty
|
static boolean |
isNotEmpty(java.lang.Iterable<?> collection)
Checks whether the given collection is not null and not empty
|
static boolean |
isNotEmpty(java.util.Map<?,?> map)
Checks whether the given map is not null and not empty
|
static boolean |
isNotEmpty(java.lang.Object[] array)
Checks whether the given array is not null and not empty
|
static <T> java.lang.Iterable<T> |
iterable(java.util.Enumeration<T> enumeration)
Returns an
Iterable for the given enumeration |
static <T> java.lang.Iterable<T> |
iterable(java.util.Iterator<T> iterator)
Returns an
Iterable which returns the given iterator. |
static java.lang.String |
join(java.lang.Iterable<?> collection,
java.lang.String separator)
Joins a collection of values into a single string, using the given separator.
|
static java.lang.String |
join(java.lang.Object[] array,
java.lang.String separator)
Joins an array of values into a single string, using the given separator.
|
static int |
length(java.lang.Object[] array)
It returns the array's length supporting null values (returning 0).
|
static <T> java.util.Collection<T> |
orEmpty(java.util.Collection<T> collection)
Returns an empty list if the given list is null
|
static <T> java.util.List<T> |
orEmpty(java.util.List<T> list)
Returns an empty list if the given list is null
|
static <K,V> java.util.Map<K,V> |
orEmpty(java.util.Map<K,V> map)
Returns an empty map if the given map is null
|
static <T> java.util.Set<T> |
orEmpty(java.util.Set<T> set)
Returns an empty set if the given set is null
|
static <T> T |
random(java.util.List<T> list)
Returns a random item from a collection, handling nulls
|
static void |
removeNulls(java.util.Collection<?> collection)
Removes any null elements on the given collection
|
static int |
size(java.util.Collection<?> collection)
Returns the collection size, handling nulls (returns 0 if the collection is null)
|
static int |
size(java.lang.Object[] array)
Returns the size of the given array, or 0 if null
|
static <T extends java.lang.Comparable<? super T>> |
sort(java.util.Collection<T> collection)
It sorts the specified collection returning an ordered List.
If the collection already is a list it return the same instance. |
static <T> java.util.List<T> |
sort(java.util.Collection<T> collection,
java.util.Comparator<? super T> comparator)
It sorts the specified collection returning an ordered List.
If the collection already is a list it return the same instance. |
static <T> java.util.List<java.util.List<T>> |
split(java.util.List<T> list,
int maxSize)
Splits the given list into several sub lists, with the given maximum size
|
static <T> java.util.Iterator<T> |
toIterator(java.util.Enumeration<T> enumeration)
Returns an iterator with the same elements as the given enumeration
|
public static <T> void addOrRemove(java.util.Collection<T> collection,
T value,
boolean add)
public static <T> Page<T> asPage(java.lang.Iterable<T> collection)
Page containing a copy of the given Collection as the total elementspublic static <T> Page<T> asPage(java.util.List<T> all, int pageSize, int currentPage)
@SafeVarargs public static <T> Page<T> asPage(T... elements)
Page containing a copy of the given Collection as the total elementspublic static <T> java.util.Set<T> asSet(java.util.List<T> list)
@SafeVarargs public static <T> java.util.Set<T> asSet(T... elements)
public static <T> java.util.stream.Stream<T> asStream(java.lang.Iterable<T> iterable)
Stream equivalent to the given Iterablepublic static <T> java.util.stream.Stream<T> asStream(java.util.Iterator<T> iterator)
Stream equivalent to the given iteratorpublic static <T> java.util.stream.Stream<T> asStream(T[] array)
Stream equivalent to the given arraypublic static <T> boolean containsAll(java.util.Collection<T> first,
java.util.Collection<T> second)
first - the container collection or nullsecond - the contained collection or nullpublic static boolean containsAny(java.util.Collection<?> collection,
java.util.Collection<?> toTest)
public static boolean containsAny(java.util.Collection<?> collection,
java.lang.Object... toTest)
public static boolean equals(java.util.Map<?,?> map1,
java.util.Map<?,?> map2)
public static <I,O extends I> java.util.stream.Stream<O> filterByType(java.util.stream.Stream<I> stream,
java.lang.Class<O> subType)
Stream containing only the elements which are instances of the given subclasspublic static <T> T first(java.lang.Iterable<T> collection)
public static <T> T first(T[] value)
public static java.lang.Object[] flatten(java.lang.Object... input)
Iterable: Add each element returned by Iterable.iterator()public static <K,V> V getOrSupply(java.util.Map<K,V> map,
K key,
java.util.function.Supplier<V> supplier)
public static java.lang.Integer getTotalCount(java.lang.Iterable<?> collection)
Page.getTotalCount() method.
If the collection is a Page and the configuration is set to skip total count, this method returns null.public static int hashCode(java.util.Map<?,?> map)
public static boolean hasOnlyOneElement(java.util.Collection<?> collection)
#size(Iterable)public static boolean isEmpty(java.lang.Iterable<?> collection)
public static boolean isEmpty(java.util.Map<?,?> map)
public static boolean isEmpty(java.lang.Object[] array)
public static boolean isNotEmpty(java.lang.Iterable<?> collection)
public static boolean isNotEmpty(java.util.Map<?,?> map)
public static boolean isNotEmpty(java.lang.Object[] array)
public static <T> java.lang.Iterable<T> iterable(java.util.Enumeration<T> enumeration)
Iterable for the given enumerationpublic static <T> java.lang.Iterable<T> iterable(java.util.Iterator<T> iterator)
Iterable which returns the given iterator. Useful to use iterators in the enhanced for looppublic static java.lang.String join(java.lang.Iterable<?> collection,
java.lang.String separator)
public static java.lang.String join(java.lang.Object[] array,
java.lang.String separator)
public static int length(java.lang.Object[] array)
public static <T> java.util.Collection<T> orEmpty(java.util.Collection<T> collection)
public static <T> java.util.List<T> orEmpty(java.util.List<T> list)
public static <K,V> java.util.Map<K,V> orEmpty(java.util.Map<K,V> map)
public static <T> java.util.Set<T> orEmpty(java.util.Set<T> set)
public static <T> T random(java.util.List<T> list)
public static void removeNulls(java.util.Collection<?> collection)
public static int size(java.util.Collection<?> collection)
public static int size(java.lang.Object[] array)
public static <T extends java.lang.Comparable<? super T>> java.util.List<T> sort(java.util.Collection<T> collection)
public static <T> java.util.List<T> sort(java.util.Collection<T> collection,
java.util.Comparator<? super T> comparator)
public static <T> java.util.List<java.util.List<T>> split(java.util.List<T> list,
int maxSize)
public static <T> java.util.Iterator<T> toIterator(java.util.Enumeration<T> enumeration)