Package io.micronaut.core.util
Class CollectionUtils
- java.lang.Object
-
- io.micronaut.core.util.CollectionUtils
-
public class CollectionUtils extends java.lang.Object
Utility methods for working with
.Collection
types- Since:
- 1.0
-
-
Constructor Summary
Constructors Constructor Description CollectionUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> java.util.Optional<java.lang.Iterable<T>>
convertCollection(java.lang.Class<? extends java.lang.Iterable<T>> iterableType, java.util.Collection<T> collection)
Attempts to convert a collection to the given iterabable typestatic <T> java.lang.Iterable<T>
enumerationToIterable(java.util.Enumeration<T> enumeration)
Convert anEnumeration
to aIterable
.static <T> java.util.Set<T>
enumerationToSet(java.util.Enumeration<T> enumeration)
Convert anEnumeration
to aSet
.static boolean
isEmpty(java.util.Collection collection)
Null safe empty check.static boolean
isEmpty(java.util.Map map)
Null safe empty check.static boolean
isIterableOrMap(java.lang.Class<?> type)
Is the given type an iterable or map type.static boolean
isNotEmpty(java.util.Collection collection)
Null safe not empty check.static boolean
isNotEmpty(java.util.Map map)
Null safe not empty check.static <T> java.util.List<T>
iterableToList(java.lang.Iterable<T> iterable)
Converts anIterable
to aList
.static <T> java.util.Set<T>
iterableToSet(java.lang.Iterable<T> iterable)
Converts anIterable
to aSet
.static <T> java.util.Set<T>
iteratorToSet(java.util.Iterator<T> iterator)
Convert anIterator
to aSet
.static <T> T
last(java.util.Collection<T> collection)
Returns the last element of a collection.static java.util.Map
mapOf(java.lang.Object... values)
Create aLinkedHashMap
from an array of values.static <T> java.util.Set<T>
setOf(T... objects)
Creates a set of the given objects.static java.lang.String
toString(java.lang.Iterable<?> iterable)
Produce a string representation of the given iterable.static java.lang.String
toString(java.lang.String delimiter, java.lang.Iterable<?> iterable)
Produce a string representation of the given iterable.static <T> java.util.List<T>
unmodifiableList(java.util.List<T> list)
Null safe version ofCollections.unmodifiableList(List)
.
-
-
-
Method Detail
-
isIterableOrMap
public static boolean isIterableOrMap(java.lang.Class<?> type)
Is the given type an iterable or map type.- Parameters:
type
- The type- Returns:
- True if it is iterable or map
- Since:
- 2.0.0
-
isEmpty
public static boolean isEmpty(@Nullable java.util.Map map)
Null safe empty check.- Parameters:
map
- The map- Returns:
- True if it is empty or null
-
isNotEmpty
public static boolean isNotEmpty(@Nullable java.util.Map map)
Null safe not empty check.- Parameters:
map
- The map- Returns:
- True if it is not null and not empty
-
isEmpty
public static boolean isEmpty(@Nullable java.util.Collection collection)
Null safe empty check.- Parameters:
collection
- The collection- Returns:
- True if it is empty or null
-
isNotEmpty
public static boolean isNotEmpty(@Nullable java.util.Collection collection)
Null safe not empty check.- Parameters:
collection
- The collection- Returns:
- True if it is not null and not empty
-
convertCollection
public static <T> java.util.Optional<java.lang.Iterable<T>> convertCollection(java.lang.Class<? extends java.lang.Iterable<T>> iterableType, java.util.Collection<T> collection)
Attempts to convert a collection to the given iterabable type
.- Type Parameters:
T
- The collection generic type- Parameters:
iterableType
- The iterable typecollection
- The collection- Returns:
- An
Optional
of the converted type
-
mapOf
public static java.util.Map mapOf(java.lang.Object... values)
Create aLinkedHashMap
from an array of values.- Parameters:
values
- The values- Returns:
- The created map
-
iteratorToSet
public static <T> java.util.Set<T> iteratorToSet(java.util.Iterator<T> iterator)
Convert anIterator
to aSet
.- Type Parameters:
T
- The type- Parameters:
iterator
- The iterator- Returns:
- The set
-
enumerationToSet
public static <T> java.util.Set<T> enumerationToSet(java.util.Enumeration<T> enumeration)
Convert anEnumeration
to aSet
.- Type Parameters:
T
- The type- Parameters:
enumeration
- The iterator- Returns:
- The set
-
enumerationToIterable
@NonNull public static <T> java.lang.Iterable<T> enumerationToIterable(@Nullable java.util.Enumeration<T> enumeration)
Convert anEnumeration
to aIterable
.- Type Parameters:
T
- The type- Parameters:
enumeration
- The iterator- Returns:
- The set
-
setOf
public static <T> java.util.Set<T> setOf(T... objects)
Creates a set of the given objects.- Type Parameters:
T
- The type- Parameters:
objects
- The objects- Returns:
- The set
-
toString
public static java.lang.String toString(java.lang.Iterable<?> iterable)
Produce a string representation of the given iterable.- Parameters:
iterable
- The iterable- Returns:
- The string representation
-
toString
public static java.lang.String toString(java.lang.String delimiter, java.lang.Iterable<?> iterable)
Produce a string representation of the given iterable.- Parameters:
delimiter
- The delimiteriterable
- The iterable- Returns:
- The string representation
-
iterableToList
public static <T> java.util.List<T> iterableToList(java.lang.Iterable<T> iterable)
Converts anIterable
to aList
.- Type Parameters:
T
- The generic type- Parameters:
iterable
- The iterable- Returns:
- The list
-
iterableToSet
public static <T> java.util.Set<T> iterableToSet(java.lang.Iterable<T> iterable)
Converts anIterable
to aSet
.- Type Parameters:
T
- The generic type- Parameters:
iterable
- The iterable- Returns:
- The set
-
unmodifiableList
@NonNull public static <T> java.util.List<T> unmodifiableList(@Nullable java.util.List<T> list)
Null safe version ofCollections.unmodifiableList(List)
.- Type Parameters:
T
- The generic type- Parameters:
list
- The list- Returns:
- A non-null unmodifiable list
-
-