public class CollectionUtil extends Object
| Modifier and Type | Method and Description |
|---|---|
static <CT extends Collection<T>,CS extends Collection<S>,T,S> |
castCollection(CT collection,
Class<S> clazz)
Casts a collection containing elements of class T to a collection containing elements of a
class S where S must be assignment-compatible with T.
|
static <T,S> List<S> |
castList(List<T> list,
Class<S> clazz)
Casts a list containing elements of class T to a list containing elements of a class S where
S must be assignment-compatible with T.
|
static boolean |
containsInstance(List<?> list,
Object object)
Returns true if the list contains the exact instance of the specified object.
|
static <T> List<T> |
ensureList(List<T> list)
Ensures that the list is not null, creating one if it is.
|
static <T,I> T |
findMatch(Collection<T> source,
BiPredicate<T,I> criteria,
I... items)
Searches a list of items for the first one that matches an element from the source collection
based on the criteria provided, returning the matching element.
|
static <T> T |
findMatch(Collection<T> source,
Predicate<T> criteria)
Searches a collection for the first element that matches the specified criteria.
|
static <T> List<T> |
findMatches(Collection<T> source,
Predicate<T> criteria)
Searches a collection for the all elements that match the specified criteria.
|
static <T> T |
getFirst(Collection<T> source)
Returns the first element of a collection, or null if the collection is null or empty.
|
static int |
indexOfInstance(List<?> list,
Object object)
Performs a lookup for the exact instance of an object in the list and returns its index, or
-1 if not found.
|
static boolean |
intersects(Iterable<?> iter1,
Iterable<?> iter2)
Returns true if the two iterables intersect (i.e., have at least one element in common).
|
static boolean |
intersects(Object[] ary1,
Object[] ary2)
Returns true if the two arrays intersect (i.e., have at least one element in common).
|
static boolean |
isEmpty(Collection<?> source)
Returns true if the collection is null or empty.
|
static <T,S extends T> |
iterableForType(Collection<T> collection,
Class<S> type)
Returns an iterable that produces only collection members of the specified type.
|
static <T,S extends T> |
iteratorForType(Collection<T> collection,
Class<S> type)
Returns a list iterator that produces only collection elements of the specified type.
|
static <T,S extends T> |
iteratorForType(Iterator<T> iterator,
Class<S> type)
Returns an iterator that returns only elements of the specified type.
|
static boolean |
notEmpty(Collection<?> source)
Returns true if the collection is not empty.
|
static <T> List<T> |
replaceElements(List<T> dest,
Collection<T> source)
Replaces the contents of the destination list with that of the source list.
|
static <T> List<T> |
replaceElements(List<T> dest,
T... elements)
Replaces the contents of the destination list with the specified elements.
|
static <T> List<T> |
toList(T... elements)
Converts an array to a list, handling nulls.
|
public static <T> T getFirst(Collection<T> source)
T - The collection element type.source - The collection.public static boolean isEmpty(Collection<?> source)
source - The collection.public static boolean notEmpty(Collection<?> source)
source - The collection.public static boolean containsInstance(List<?> list, Object object)
list - List to search.object - Object instance to locate.public static int indexOfInstance(List<?> list, Object object)
list - List to search.object - Object instance to locate.public static <T,S> List<S> castList(List<T> list, Class<S> clazz)
T - Class of list elements.S - Target class for cast.list - List to be recast.clazz - Class to which to cast list elements.public static <CT extends Collection<T>,CS extends Collection<S>,T,S> CS castCollection(CT collection, Class<S> clazz)
T - Class of collection elements.CT - Class of Collection of T elements.S - Target class for cast.CS - Class of Collection of S elements.collection - Collection to be recast.clazz - Class to which to cast collection elements.public static <T,S extends T> Iterator<S> iteratorForType(Collection<T> collection, Class<S> type)
T - Class of collection elements.S - Subclass of T to be used by iterator.collection - Collection to iterate.type - Type of element to return.public static <T,S extends T> Iterator<S> iteratorForType(Iterator<T> iterator, Class<S> type)
T - Class of collection elements.S - Subclass of T to be used by iterator.iterator - Iterator to wrap.type - Type of element to return.public static <T,S extends T> Iterable<S> iterableForType(Collection<T> collection, Class<S> type)
T - Class of collection elements.S - Subclass of T to be used by iterable.collection - Collection to iterate.type - Type of element to return.public static boolean intersects(Object[] ary1, Object[] ary2)
ary1 - The first array.ary2 - The second array.public static boolean intersects(Iterable<?> iter1, Iterable<?> iter2)
iter1 - The first iterable.iter2 - The second iterable.public static <T> List<T> toList(T... elements)
T - The element type.elements - The array of elements.public static <T> List<T> ensureList(List<T> list)
T - The list's element type.list - The list.public static <T> List<T> replaceElements(List<T> dest, Collection<T> source)
T - The lists' element type.dest - The destination list. If null, a new list will be created.source - The source list. Null is equivalent to an empty list.public static <T> List<T> replaceElements(List<T> dest, T... elements)
T - The lists' element type.dest - The destination list. If null, a new list will be created.elements - A list of elements to be copied to the destination list.public static <T> T findMatch(Collection<T> source, Predicate<T> criteria)
T - The type of collection element.source - The collection to search.criteria - The criteria for a matching element.public static <T,I> T findMatch(Collection<T> source, BiPredicate<T,I> criteria, I... items)
T - The type of collection element.I - The type of item.source - The collection to search.criteria - The criteria for a matching element.items - The list of items to search.public static <T> List<T> findMatches(Collection<T> source, Predicate<T> criteria)
T - The type of collection element.source - The collection to search.criteria - The criteria for a matching element.Copyright © 2023 Fujion Framework. All rights reserved.