public final class Collections extends Object
Modifier and Type | Method and Description |
---|---|
static List |
arrayToList(Object source)
Convert the supplied array into a List.
|
static <T> Set<T> |
asSet(Collection<T> c)
Returns the specified collection as a
Set instance. |
static <T> Set<T> |
concat(Set<T> c,
T... elements)
Concatenate the specified set with the specified array elements, resulting in a new
LinkedHashSet with
the array elements appended to the end of the existing Set. |
static boolean |
contains(Enumeration enumeration,
Object element)
Check whether the given Enumeration contains the given element.
|
static boolean |
contains(Iterator iterator,
Object element)
Check whether the given Iterator contains the given element.
|
static boolean |
containsAny(Collection source,
Collection candidates)
Return
true if any element in 'candidates ' is
contained in 'source '; otherwise returns false . |
static boolean |
containsInstance(Collection collection,
Object element)
Check whether the given Collection contains the given element instance.
|
static <T> List<T> |
emptyList()
Returns a type-safe immutable empty
List . |
static <K,V> Map<K,V> |
emptyMap()
Returns a type-safe immutable empty
Map . |
static <T> Set<T> |
emptySet()
Returns a type-safe immutable empty
Set . |
static Class<?> |
findCommonElementType(Collection collection)
Find the common element type of the given Collection, if any.
|
static Object |
findFirstMatch(Collection source,
Collection candidates)
Return the first element in '
candidates ' that is contained in
'source '. |
static Object |
findValueOfType(Collection<?> collection,
Class<?>[] types)
Find a single value of one of the given types in the given Collection:
searching the Collection for a value of the first type, then
searching for a value of the second type, etc.
|
static <T> T |
findValueOfType(Collection<?> collection,
Class<T> type)
Find a single value of the given type in the given Collection.
|
static boolean |
hasUniqueObject(Collection collection)
Determine whether the given Collection only contains a single unique object.
|
static <T,C extends Collection<T>> |
immutable(C c)
Null-safe factory method that returns an immutable/unmodifiable view of the specified collection instance.
|
static <T> List<T> |
immutable(List<T> list)
Shorter null-safe convenience alias for
Collections.unmodifiableList(List) so both classes
don't need to be imported. |
static <K,V> Map<K,V> |
immutable(Map<K,V> m)
Shorter null-safe convenience alias for
Collections.unmodifiableList(List) so both classes
don't need to be imported. |
static <T> Set<T> |
immutable(Set<T> set)
Shorter null-safe convenience alias for
Collections.unmodifiableSet(Set) so both classes don't
need to be imported. |
static boolean |
isEmpty(Collection<?> collection)
Return
true if the supplied Collection is null
or empty. |
static boolean |
isEmpty(Map<?,?> map)
Return
true if the supplied Map is null
or empty. |
static void |
mergeArrayIntoCollection(Object array,
Collection collection)
Merge the given array into the given Collection.
|
static void |
mergePropertiesIntoMap(Properties props,
Map map)
Merge the given Properties instance into the given Map,
copying all properties (key-value pairs) over.
|
static <T> Collection<T> |
nullSafe(Collection<T> c)
Returns a non-null collection, either
c if it is not null, or emptyList() otherwise. |
static <T> Set<T> |
nullSafe(Set<T> s)
Returns a non-null set, either
s if it is not null, or emptySet() otherwise. |
static <T> List<T> |
of(T... elements)
Returns a type-safe immutable
List containing the specified array elements. |
static <T> Set<T> |
setOf(T... elements)
Returns a type-safe immutable
Set containing the specified array elements. |
static int |
size(Collection<?> collection)
Returns the collection's size or
0 if the collection is null . |
static int |
size(Map<?,?> map)
Returns the map's size or
0 if the map is null . |
static <A,E extends A> |
toArray(Enumeration<E> enumeration,
A[] array)
Marshal the elements from the given enumeration into an array of the given type.
|
static <E> Iterator<E> |
toIterator(Enumeration<E> enumeration)
Adapt an enumeration to an iterator.
|
public static <T> List<T> emptyList()
List
.T
- list element typeList
.public static <T> Set<T> emptySet()
Set
.T
- set element typeSet
.public static <K,V> Map<K,V> emptyMap()
Map
.K
- map key typeV
- map value typeMap
.@SafeVarargs public static <T> List<T> of(T... elements)
List
containing the specified array elements.T
- list element typeelements
- array elements to include in the listList
containing the specified array elements.public static <T> Set<T> asSet(Collection<T> c)
Set
instance.T
- collection element typec
- the collection to represent as a setSet
containing the specified collection elements.@SafeVarargs public static <T> Set<T> setOf(T... elements)
Set
containing the specified array elements.T
- set element typeelements
- array elements to include in the setSet
containing the specified array elements.public static <K,V> Map<K,V> immutable(Map<K,V> m)
Collections.unmodifiableList(List)
so both classes
don't need to be imported.K
- map key typeV
- map value typem
- map to wrap in an immutable/unmodifiable collectionm
.public static <T> Set<T> immutable(Set<T> set)
Collections.unmodifiableSet(Set)
so both classes don't
need to be imported.T
- set element typeset
- set to wrap in an immutable Setset
public static <T> List<T> immutable(List<T> list)
Collections.unmodifiableList(List)
so both classes
don't need to be imported.T
- list element typelist
- list to wrap in an immutable Listlist
public static <T,C extends Collection<T>> C immutable(C c)
List
, Set
and Collection
arguments.C
- type of collectionT
- type of elements in the collectionc
- collection to wrap in an immutable/unmodifiable collectionl
.public static <T> Set<T> nullSafe(Set<T> s)
s
if it is not null, or emptySet()
otherwise.T
- type of elements in the sets
- the set to check for nulls
if it is not null, or emptySet()
otherwise.public static <T> Collection<T> nullSafe(Collection<T> c)
c
if it is not null, or emptyList()
otherwise.T
- type of elements in the collectionc
- the collection to check for nullc
if it is not null, or emptyList()
otherwise.public static boolean isEmpty(Collection<?> collection)
true
if the supplied Collection is null
or empty. Otherwise, return false
.collection
- the Collection to checkpublic static int size(Collection<?> collection)
0
if the collection is null
.collection
- the collection to check.0
if the collection is null
.public static int size(Map<?,?> map)
0
if the map is null
.map
- the map to check0
if the map is null
.public static boolean isEmpty(Map<?,?> map)
true
if the supplied Map is null
or empty. Otherwise, return false
.map
- the Map to checkpublic static List arrayToList(Object source)
A null
source value will be converted to an
empty List.
source
- the (potentially primitive) arrayObjects.toObjectArray(Object)
@SafeVarargs public static <T> Set<T> concat(Set<T> c, T... elements)
LinkedHashSet
with
the array elements appended to the end of the existing Set.T
- set element typec
- the set to append toelements
- the array elements to append to the end of the setLinkedHashSet
with the array elements appended to the end of the original set.public static void mergeArrayIntoCollection(Object array, Collection collection)
array
- the array to merge (may be null
)collection
- the target Collection to merge the array intopublic static void mergePropertiesIntoMap(Properties props, Map map)
Uses Properties.propertyNames()
to even catch
default properties linked into the original Properties instance.
props
- the Properties instance to merge (may be null
)map
- the target Map to merge the properties intopublic static boolean contains(Iterator iterator, Object element)
iterator
- the Iterator to checkelement
- the element to look fortrue
if found, false
elsepublic static boolean contains(Enumeration enumeration, Object element)
enumeration
- the Enumeration to checkelement
- the element to look fortrue
if found, false
elsepublic static boolean containsInstance(Collection collection, Object element)
Enforces the given instance to be present, rather than returning
true
for an equal element as well.
collection
- the Collection to checkelement
- the element to look fortrue
if found, false
elsepublic static boolean containsAny(Collection source, Collection candidates)
true
if any element in 'candidates
' is
contained in 'source
'; otherwise returns false
.source
- the source Collectioncandidates
- the candidates to search forpublic static Object findFirstMatch(Collection source, Collection candidates)
candidates
' that is contained in
'source
'. If no element in 'candidates
' is present in
'source
' returns null
. Iteration order is
Collection
implementation specific.source
- the source Collectioncandidates
- the candidates to search fornull
if not foundpublic static <T> T findValueOfType(Collection<?> collection, Class<T> type)
T
- the generic type parameter for type
collection
- the Collection to searchtype
- the type to look fornull
if none or more than one such value foundpublic static Object findValueOfType(Collection<?> collection, Class<?>[] types)
collection
- the collection to searchtypes
- the types to look for, in prioritized ordernull
if none or more than one such value foundpublic static boolean hasUniqueObject(Collection collection)
collection
- the Collection to checktrue
if the collection contains a single reference or
multiple references to the same instance, false
elsepublic static Class<?> findCommonElementType(Collection collection)
collection
- the Collection to checknull
if no clear
common type has been found (or the collection was empty)public static <A,E extends A> A[] toArray(Enumeration<E> enumeration, A[] array)
A
- the element type of the array that will be createdE
- the element type contained within the enumeration.enumeration
- the collection to convert to an arrayarray
- an array instance that matches the type of array to returnA
that contains the elements in the specified enumeration
.public static <E> Iterator<E> toIterator(Enumeration<E> enumeration)
E
- the type of elements in the enumerationenumeration
- the enumerationCopyright © 2014–2023 jsonwebtoken.io. All rights reserved.