public class CollectionHelper extends Object
| Constructor and Description |
|---|
CollectionHelper() |
| Modifier and Type | Method and Description |
|---|---|
static <T,V extends T> |
addToList(List<T> list,
V... elems)
Denullifies the given
list and adds elems to it. |
static <T> T |
findInCollection(Collection<T> coll,
java.util.function.Predicate<T> func)
Finds the first element in the collection that satisfies the predicate
func. |
static <T> void |
forEach(Iterable<T> coll,
java.util.function.Consumer<T> func)
Executes function
func on the given collection coll. |
static boolean |
isEmpty(Collection<?> coll)
Checks if
coll is either null or empty. |
static boolean |
isEmpty(Map<?,?> map)
Checks if
map is either null or empty. |
static <T> boolean |
isInCollection(T ele,
Collection<T> coll)
Checks whether
ele is in the given coll. |
static <E extends Enum<E>> |
isInEnumSet(E e,
E... enums)
Checks if the enum
e is in the given array of enums. |
static boolean |
isNotEmpty(Collection<?> coll)
Negation of
isEmpty(Collection). |
static boolean |
isNotEmpty(Map<?,?> map)
Negation of
isEmpty(Map). |
static void |
sanitize(Collection<?> coll)
Removes null elements from the collection.
|
static void |
sanitize(Map<?,?> map)
Removes elements with either null key or null value from the map.
|
static int |
sizeOf(Collection<?> coll)
Returns the size of the given collection.
|
static <T> List<T> |
toList(T... elems)
Converts the given array of elements of type T to a new instance of
ArrayList of the same type. |
static <T> Set<T> |
toSet(T... elems)
Converts the given array of elements of type T to a new instance of
HashSet of the same type. |
public static <T,V extends T> List<T> addToList(List<T> list, V... elems)
list and adds elems to it. T - type of the listV - type of the elements. Must be a subclass of <T>list - to add elements toelems - elements to be added to the list. Null elements will not be added into the listCommonHelper.denullify(List)public static <T> T findInCollection(Collection<T> coll, java.util.function.Predicate<T> func)
func.T - type of collection's contentcoll - collection to be iteratedfunc - a boolean function to evaluate the elements in collpublic static <T> void forEach(Iterable<T> coll, java.util.function.Consumer<T> func)
func on the given collection coll. T - both the collection and function must be of the same typecoll - subclass of Iterable on which the function func will be performedfunc - function to be performed on each element of the collection collpublic static boolean isEmpty(Collection<?> coll)
coll is either null or empty.coll - collection to be checked, may be any subclass or implementation of Collectioncoll is empty or null, false otherwisepublic static boolean isEmpty(Map<?,?> map)
map is either null or empty.map - map to be checked, may be any subclass or implementation of Mapmap is empty or null, false otherwisepublic static <T> boolean isInCollection(T ele,
Collection<T> coll)
ele is in the given coll. Comparison is done by invoking
Collection.contains(Object).
equals() implementation (e.g. not calling super.equals())
as this may lead to incorrect result.T - type of elementele - to be evaluated againstcoll - to be iterated overpublic static <E extends Enum<E>> boolean isInEnumSet(E e, E... enums)
e is in the given array of enums. Comparison is done by
invoking e.equals().E - type of enume - to be checkedenums - to be iterated overe is found in enums. e is nullenums is nulle is not found in enumspublic static boolean isNotEmpty(Collection<?> coll)
isEmpty(Collection).coll - to be checkedcoll is neither null nor empty, false otherwisepublic static boolean isNotEmpty(Map<?,?> map)
isEmpty(Map).map - to be checkedmap is neither null nor empty, false otherwisepublic static void sanitize(Collection<?> coll)
coll - to be sanitizedUnsupportedOperationException - if the collection is immutable or fixed-sizeCollection.removeIf(java.util.function.Predicate)public static void sanitize(Map<?,?> map)
map - to be sanitizedUnsupportedOperationException - if the map is immutable or fixed-sizeCollection.removeIf(java.util.function.Predicate)public static int sizeOf(Collection<?> coll)
coll - any subclass or implementation of CollectionisEmpty(Collection)public static <T> Set<T> toSet(T... elems)
HashSet of the same type. Only adds non-null elements into the set.Copyright © 2018. All rights reserved.