Class CollectionUtils
java.lang.Object
com.thanlinardos.spring_enterprise_library.objects.utils.CollectionUtils
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> List<T> combineToList(Collection<T>... collections) Returns a combined list of the input collections.static <T> List<T> combineToList(List<T> first, List<T> second) Returns a combined list of the two input lists.static <T> Set<T> combineToSet(Collection<T>... collections) Returns a combined set of the input collections.static <T> Set<T> combineToSet(Set<T> first, Set<T> second) Returns a combined set of the two input sets.static <T,S> boolean consideredEqual(BiPredicate<T, S> predicate, List<T> elementList1, List<S> elementList2) Method that determines if two lists are considered equal by applying a two-argument predicate to corresponding elements in the lists.static <B> booleanconsideredEqualIgnoringOrder(BiPredicate<B, B> predicate, Collection<B> first, Collection<B> second) Returns: true if both collections are null or empty false if one is null or empty and the other isn't false if the two collections have different sizes true if none of the previous conditions apply, and all elements of the first collection a return true for the given predicate on any element of the second collection b (and vice versa) .static <T,S> boolean consideredNotEqual(BiPredicate<T, S> predicate, List<T> elementList1, List<S> elementList2) Method that determines if two lists are NOT considered equal by applying a two-argument predicate to corresponding elements in the lists.static <T> booleancontains(Collection<T> collection, Predicate<T> predicate) Returns whether the given collection contains an element that matches the predicate.static <T> booleanReturns whether the given array contains an element that matches the predicate.static <A,B> boolean containsAnyNull(Collection<A> collection, Function<A, B> getter) Returns true if the result of thegetteris null for any element of the collection.static <T> booleancontainsAnyOf(Collection<T> collection, T... values) Returns whether the given collection contains an element that matches any value.static <T> List<T> emptyListIfNull(Collection<T> collection) Returns an empty list if the given collection object was null, otherwise maps the collection to a list.static <T,R> List <T> filterByMaxValueForField(Collection<T> collection, Function<T, R> extractor, Comparator<R> comparator) This function filters a collection (and transforms it to a List) to only include the elements having the maximum value based on a given extractor and comparator.static <T> List<T> filterByPredicate(Collection<T> entities, Predicate<T> predicate) Filters the entities by the given predicate.static <T> TfindExactlyOne(Collection<T> entities, Predicate<T> predicate) Finds exactly one entity based on the predicate, otherwise throws.static <T> Optional<T> findExactlyOneOrNone(Collection<T> entities, Predicate<T> predicate) Finds exactly one or none in the colleciton, based on the predicate, otherwise throws.static <T extends U,U>
booleanhasDifferenceBetweenAnyWay(Set<T> a, Set<U> b) Determines if there is any difference between the two sets.static <T> booleanhasSizeInRange(Collection<T> collection, Integer from, Integer to) Returns true if the collection has a size in the given range.static <T extends U,U>
booleanintersects(Collection<T> a, Collection<U> b) Determines if there is any intersection between elements in two collections.static <T> booleanisEmpty(Collection<T> collection) Returns whether the collection is null or empty.static <T> booleanisIn(T target, Collection<T> values) Returns if the given value is in the supplied values.static <T> booleanisIn(T target, T... values) Returns if the given value is in the supplied values.static booleanisNotEmpty(Object[] array) Returns whether the array is not null or empty.static <T> booleanisNotEmpty(Collection<T> collection) Returns whether the collection is not null or empty.static <T> booleanisNotIn(T target, Collection<T> values) Returns if the given value is not in the supplied values.static <T> booleanisNotIn(T target, T... values) Returns if the given value is not in the supplied values.static <T> List<T> listOf(T... elements) Creates a list of elements.static <T,R> List <R> merge(BiPredicate<T, T> shouldMerge, BinaryOperator<R> merge, Function<T, R> convert, List<T> elementList) Method that uses an input to operate on the 2 elements at a time, after converting them to typeCollectionUtilswith themergeFunction.static <T> booleannotContains(Collection<T> collection, Predicate<T> predicate) Returns whether the given collection does not contain an element that matches the predicate.static <T> Collection<T> removeAll(Collection<T> base, Collection<T> filter, BiPredicate<T, T> removeIf) Performs an operation equivalent toCollection.removeAll(Collection), but with a custom predicate instead of using the equals method of<T>, and returns the result instead of performing the modification in-place.static <T,U extends Collection<T>>
UrequireNotEmpty(U collection) Validates that the collection is not null or empty.static <T> Collection<T> retainAll(Collection<T> base, Collection<T> filter, BiPredicate<T, T> retainIf) Performs an operation equivalent toCollection.retainAll(Collection), but with a custom predicate instead of using the equals method of<T>, and returns the result instead of performing the modification in-place.static <T> List<T> Returns a shallow copy of the given list with the elements in reverse order.static <T,R> List <R> sortAndMerge(BiPredicate<T, T> shouldMerge, BinaryOperator<R> merge, Function<T, R> convert, Comparator<T> listComparator, List<T> elementList) Sorts the givenelementListand then merges its elements usingmerge(BiPredicate, BinaryOperator, Function, List).static <T,S, R> List <R> zipWith(BiFunction<T, S, R> function, List<T> elementList1, List<S> elementList2) Method that joins two lists by applying a two-argument function to corresponding elements in the lists and returning a list of the results.
-
Constructor Details
-
CollectionUtils
public CollectionUtils()
-
-
Method Details
-
isEmpty
Returns whether the collection is null or empty.- Type Parameters:
T- collection element type- Parameters:
collection- the collection.- Returns:
- true if the collection is empty or null.
-
isNotEmpty
Returns whether the collection is not null or empty.- Type Parameters:
T- collection element type- Parameters:
collection- the collection.- Returns:
- true if the collection is not empty or null.
-
requireNotEmpty
Validates that the collection is not null or empty.- Type Parameters:
T- collection element typeU- collection type- Parameters:
collection- the collection.- Returns:
- the collection if not null or empty
- Throws:
CoreException- if the collection is null or empty
-
contains
Returns whether the given collection contains an element that matches the predicate.Returns
falseif the collection is null.- Type Parameters:
T- collection element type- Parameters:
collection- the collection.predicate- the predicate.- Returns:
- true if an element matching the predicate is found, otherwise false.
-
containsAnyOf
Returns whether the given collection contains an element that matches any value.- Type Parameters:
T- collection element type- Parameters:
collection- the collectionvalues- values to check for- Returns:
- true if any values is found in the collection, false otherwise.
-
contains
Returns whether the given array contains an element that matches the predicate.Returns
falseif the collection is null.- Type Parameters:
T- collection element type- Parameters:
collection- the collection.predicate- the predicate.- Returns:
- true if an element matching the predicate is found, otherwise false.
-
containsAnyNull
Returns true if the result of thegetteris null for any element of the collection.- Type Parameters:
A- collection element typeB- collection element getter return type- Parameters:
collection- the collection.getter- a getter of the collection element type- Returns:
- true if the result of the
getteris null for any element of the collection
-
notContains
Returns whether the given collection does not contain an element that matches the predicate.WARNING Returns
trueif the collection is null.- Type Parameters:
T- collection element type- Parameters:
collection- the collection.predicate- the predicate.- Returns:
- true if no elements matches the predicate, otherwise false.
-
isIn
Returns if the given value is in the supplied values. Is null safe.- Type Parameters:
T- collection element type- Parameters:
target- the input value to check.values- the values to check against.- Returns:
- true if the value is in the list of supplied values, otherwise false.
-
isIn
Returns if the given value is in the supplied values. Is null safe.- Type Parameters:
T- collection element type- Parameters:
target- the input value to check.values- the values to check against.- Returns:
- true if the value is in the list of supplied values, otherwise false.
-
isNotIn
Returns if the given value is not in the supplied values. Is null safe.- Type Parameters:
T- collection element type- Parameters:
target- the input value to check.values- the values to check against.- Returns:
- true if the value is not in the list of supplied values, otherwise false.
-
isNotIn
Returns if the given value is not in the supplied values. Is null safe.- Type Parameters:
T- collection element type- Parameters:
target- the input value to check.values- theCollectionof values to check against.- Returns:
- true if the value is not in the list of supplied values, otherwise false.
-
combineToList
Returns a combined list of the input collections. Does not modify or copy the elements insides the collections. Does not preserve the order.- Type Parameters:
T- collection element type- Parameters:
collections- the input collections.- Returns:
- return a list of all the elements from the collections.
-
combineToList
Returns a combined list of the two input lists. Does not modify or copy the elements inside the lists. Combines by appending the two lists in order to a new list.- Type Parameters:
T- list element type- Parameters:
first- first listsecond- second list- Returns:
- return a list of all the elements from the lists.
-
combineToSet
Returns a combined set of the input collections. Does not modify or copy the elements insides the collections. Does not preserve the order.- Type Parameters:
T- collection element type- Parameters:
collections- the input collections.- Returns:
- return a set of all the elements from the collections.
-
combineToSet
Returns a combined set of the two input sets. Does not modify or copy the elements inside the sets. Does not preserve the order.- Type Parameters:
T- set element type- Parameters:
first- the first setsecond- the second set- Returns:
- return a set of all the elements from the sets.
-
reverse
-
zipWith
public static <T,S, List<R> zipWithR> (BiFunction<T, S, R> function, List<T> elementList1, List<S> elementList2) Method that joins two lists by applying a two-argument function to corresponding elements in the lists and returning a list of the results. That is, the two lists are traversed together, and the given function is called pairwise on each member of both lists. The two lists must have the same length, which will also be the length of the returned list.- Parameters:
function- The function that is applied to the elements of the two lists to form the result list.elementList1- The first list.elementList2- the second list.- Returns:
- a list of the zipped results.
- Throws:
CoreException- if any of the list is empty or nullCoreException- if any of the two lists does not have the same amount of elements.
-
merge
public static <T,R> List<R> merge(BiPredicate<T, T> shouldMerge, BinaryOperator<R> merge, Function<T, R> convert, List<T> elementList) Method that uses an input to operate on the 2 elements at a time, after converting them to typeCollectionUtilswith themergeFunction. If applyingshouldMergeon a pair of elements results in true, then they are merged in a single element usingmerge, otherwise they get added to the result list.It is noted that for operating on the pair of current and previous elements a stack
Dequeis used, in order to pop the previous converted element and merge it with the current one whenshouldMergeis true. Otherwise, the current converted element gets pushed to the top of the stack.- Type Parameters:
T- the type class of the input elements to merge.R- the type class of the merged result list.- Parameters:
shouldMerge- the inputBiPredicatethat checks whether the pair of elements it is applied to should be merged.merge- theBinaryOperatorto use when merging the converted pair of elements.convert- theFunctionthat converts an element of the input list to the result typeCollectionUtilsthat gets operated on bymergeelementList- the input list of elements of typeCollectionUtilsto operate on. May need to be sorted by some criteria, since the shouldMerge function is applied on pairs of elements at a time that are in adjacent indexes of the list.- Returns:
- the result list of type
CollectionUtilswith all the merged and non-merged converted elements.
-
sortAndMerge
public static <T,R> List<R> sortAndMerge(BiPredicate<T, T> shouldMerge, BinaryOperator<R> merge, Function<T, R> convert, Comparator<T> listComparator, List<T> elementList) Sorts the givenelementListand then merges its elements usingmerge(BiPredicate, BinaryOperator, Function, List).- Type Parameters:
T- the type class of the input elements to merge.R- the type class of the merged result list.- Parameters:
shouldMerge- the inputBiPredicatethat checks whether the pair of elements it is applied to should be merged.merge- theBinaryOperatorto use when merging the converted pair of elements.convert- theFunctionthat converts an element of the input list to the result typeCollectionUtilsthat gets operated on bymergelistComparator- the comparator to use to sort the given list before merging.elementList- the input list of elements of typeCollectionUtilsto operate on. May need to be sorted by some criteria, since the shouldMerge function is applied on pairs of elements at a time that are in adjacent indexes of the list.- Returns:
- the result list of type
CollectionUtilswith all the merged and non-merged converted elements.
-
consideredEqual
public static <T,S> boolean consideredEqual(BiPredicate<T, S> predicate, List<T> elementList1, List<S> elementList2) Method that determines if two lists are considered equal by applying a two-argument predicate to corresponding elements in the lists. The two lists are traversed together, and the given predicate is called pairwise on each member of both lists. The two supplied lists should be ordered before use.- Type Parameters:
T- element type of first listS- element type of second list- Parameters:
predicate- The predicate that is applied to the elements of the two lists.elementList1- The first list.elementList2- The second list.- Returns:
- True if both lists have the same length and the given predicate returns true for all pairs constructed from traversing the lists, or both lists are empty, or both lists are null, otherwise false.
-
consideredEqualIgnoringOrder
public static <B> boolean consideredEqualIgnoringOrder(BiPredicate<B, B> predicate, @Nullable Collection<B> first, @Nullable Collection<B> second) Returns:- true if both collections are null or empty
- false if one is null or empty and the other isn't
- false if the two collections have different sizes
- true if none of the previous conditions apply, and all elements of the first collection a return true for the given predicate on any element of the second collection b (and vice versa)
- Type Parameters:
B- collection element class- Parameters:
predicate- the predicate to check equality forfirst- the first collectionsecond- the second collection- Returns:
- true if both collections are considered equal, based on the given predicate
-
consideredNotEqual
public static <T,S> boolean consideredNotEqual(BiPredicate<T, S> predicate, List<T> elementList1, List<S> elementList2) Method that determines if two lists are NOT considered equal by applying a two-argument predicate to corresponding elements in the lists. The two lists are traversed together, and the given predicate is called pairwise on each member of both lists. The two supplied lists should be ordered before use.- Type Parameters:
T- element type of first listS- element type of second list- Parameters:
predicate- The predicate that is applied to the elements of the two lists.elementList1- The first list.elementList2- The second list.- Returns:
- False if both lists have the same length and the given predicate returns true for all pairs constructed from traversing the lists, or both lists are empty, or both lists are null, otherwise True.
-
intersects
Determines if there is any intersection between elements in two collections.- Type Parameters:
T- collectionaelement type.U- collectionbelement type.- Parameters:
a- Collection of elements of typeUor any subtype.b- Collection of elements to check for intersection with.- Returns:
- true if there is an intersection, false otherwise.
-
hasDifferenceBetweenAnyWay
Determines if there is any difference between the two sets. Both by comparing difference in set A to B and set B to A- Type Parameters:
T- set A element typeU- set B element type- Parameters:
a- Set of elements of typeUor any subtypeb- Set of elements to check for differance with- Returns:
- true if there is any difference between the two sets, false otherwise.
-
listOf
Creates a list of elements. Can create lists containing null values.- Type Parameters:
T- collection element type- Parameters:
elements- to create a list from- Returns:
- the created list
-
filterByMaxValueForField
public static <T,R> List<T> filterByMaxValueForField(Collection<T> collection, Function<T, R> extractor, Comparator<R> comparator) This function filters a collection (and transforms it to a List) to only include the elements having the maximum value based on a given extractor and comparator. Notice thatStream.max(Comparator)does not support null, so in case null has to be considered as maximum value, the extractor needs change null to the respective maximum values of the type R.- Type Parameters:
T- The type of the elements of the input Collection (and output List).R- The type of the elements extracted by the extractor and input to the comparator.- Parameters:
collection- The collection to filter.extractor- The extractor to apply on the elements, providing the sub-elements for comparison.comparator- The comparator comparing extracted values.- Returns:
- The filtered List of elements of type T (same type as the elements of the input Collection).
-
removeAll
public static <T> Collection<T> removeAll(Collection<T> base, Collection<T> filter, BiPredicate<T, T> removeIf) Performs an operation equivalent toCollection.removeAll(Collection), but with a custom predicate instead of using the equals method of<T>, and returns the result instead of performing the modification in-place.- Type Parameters:
T- The type of the elements of the input Collection (and output Collection).- Parameters:
base- collection to remove elements fromfilter- collection containing elements to be removed frombaseremoveIf- custom bi-predicate method- Returns:
- the result of removing elements from
basethat satisfy the givenequalitypredicate against any element infilter
-
retainAll
public static <T> Collection<T> retainAll(Collection<T> base, Collection<T> filter, BiPredicate<T, T> retainIf) Performs an operation equivalent toCollection.retainAll(Collection), but with a custom predicate instead of using the equals method of<T>, and returns the result instead of performing the modification in-place.- Type Parameters:
T- The type of the elements of the input Collection (and output Collection).- Parameters:
base- collection to remove elements fromfilter- collection containing elements to retain frombaseretainIf- custom bi-predicate method- Returns:
- the result of retaining elements from
basethat satisfy the givenequalitypredicate against any element infilter, removing all other elements
-
findExactlyOneOrNone
Finds exactly one or none in the colleciton, based on the predicate, otherwise throws.- Type Parameters:
T- entity type- Parameters:
entities- collection of entitiespredicate- predicate to filter for- Returns:
- result of applying
StreamUtils.findExactlyOneOrNone(String, String...)after filtering for thepredicate
-
findExactlyOne
Finds exactly one entity based on the predicate, otherwise throws.- Type Parameters:
T- entity type- Parameters:
entities- collection of entitiespredicate- predicate to filter for- Returns:
- result of applying
StreamUtils.findExactlyOne(String, String...)after filtering for thepredicate
-
filterByPredicate
Filters the entities by the given predicate.- Type Parameters:
T- entity type- Parameters:
entities- collection of entitiespredicate- predicate to filter for- Returns:
- resulting list after filtering for the
predicate
-
emptyListIfNull
Returns an empty list if the given collection object was null, otherwise maps the collection to a list.- Type Parameters:
T- type of the collection- Parameters:
collection- a given collection- Returns:
- an empty list if the given collection object was null, otherwise a list of the collection elements
-
hasSizeInRange
public static <T> boolean hasSizeInRange(@Nullable Collection<T> collection, Integer from, Integer to) Returns true if the collection has a size in the given range.- Type Parameters:
T- type of the collection- Parameters:
collection- a given collectionfrom- fromto- to- Returns:
- true if the collection size (0 if null) is in the given range, otherwise false
-
isNotEmpty
Returns whether the array is not null or empty.- Parameters:
array- the array.- Returns:
- true if the array is not empty or null, false otherwise.
-