Package io.micronaut.core.util
Class CollectionUtils
java.lang.Object
io.micronaut.core.util.CollectionUtils
Utility methods for working with Collection
types
- Since:
- 1.0
- Author:
- Graeme Rocher
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <E> List<E>
The method will merge the list and element into a new list.static <E> List<E>
concat
(List<E> list1, Collection<E> collection) The method will merge two list into a new list.static <E> Set<E>
The method will merge the set and element into a new set.static <E> Set<E>
concat
(Set<E> set1, Collection<E> collection) The method will merge two sets into a new set.convertCollection
(Class<? extends Iterable<T>> iterableType, Collection<T> collection) Attempts to convert a collection to the given iterabable typeenumerationToIterable
(@Nullable Enumeration<T> enumeration) Convert anEnumeration
to aIterable
.static <T> Set<T>
enumerationToSet
(Enumeration<T> enumeration) Convert anEnumeration
to aSet
.static boolean
isEmpty
(@Nullable Collection collection) Null safe empty check.static boolean
Null safe empty check.static boolean
isIterableOrMap
(Class<?> type) Is the given type an iterable or map type.static boolean
isNotEmpty
(@Nullable Collection collection) Null safe not empty check.static boolean
isNotEmpty
(@Nullable Map map) Null safe not empty check.static <T> List<T>
iterableToList
(Iterable<T> iterable) static <T> Set<T>
iterableToSet
(Iterable<T> iterable) static <T> Set<T>
iteratorToSet
(Iterator<T> iterator) static <T> T
last
(@NonNull Collection<T> collection) Returns the last element of a collection.static Map
Create aLinkedHashMap
from an array of values.static <K,
V> HashMap<K, V> newHashMap
(int size) Create newHashMap
sized to fit all the elements of the size provided.static <E> HashSet<E>
newHashSet
(int size) Create newHashSet
sized to fit all the elements of the size provided.static <K,
V> LinkedHashMap<K, V> newLinkedHashMap
(int size) Create newLinkedHashMap
sized to fit all the elements of the size provided.static <E> LinkedHashSet<E>
newLinkedHashSet
(int size) Create newLinkedHashSet
sized to fit all the elements of the size provided.static <T> Set<T>
setOf
(T... objects) Creates a set of the given objects.static String
Produce a string representation of the given iterable.static String
Produce a string representation of the given iterable.unmodifiableList
(@Nullable List<T> list) Null safe version ofCollections.unmodifiableList(List)
.
-
Constructor Details
-
CollectionUtils
public CollectionUtils()
-
-
Method Details
-
concat
The method will merge the set and element into a new set.- Type Parameters:
E
- The element type- Parameters:
set
- The setelement
- The element- Returns:
- The new set
- Since:
- 4.0.0
-
concat
The method will merge two sets into a new set.- Type Parameters:
E
- The element type- Parameters:
set1
- The first setcollection
- The second collection- Returns:
- The new set
- Since:
- 4.0.0
-
concat
The method will merge the list and element into a new list.- Type Parameters:
E
- The element type- Parameters:
list
- The listelement
- The element- Returns:
- The new list
- Since:
- 4.0.0
-
concat
The method will merge two list into a new list.- Type Parameters:
E
- The element type- Parameters:
list1
- The first listcollection
- The second collection- Returns:
- The new list
- Since:
- 4.0.0
-
newHashSet
Create newHashSet
sized to fit all the elements of the size provided.- Type Parameters:
E
- The element type- Parameters:
size
- The size to fit all the elements- Returns:
- a new
HashSet
with reallocated size - Since:
- 4.0.0
-
newLinkedHashSet
Create newLinkedHashSet
sized to fit all the elements of the size provided.- Type Parameters:
E
- The element type- Parameters:
size
- The size to fit all the elements- Returns:
- a new
LinkedHashSet
with reallocated size - Since:
- 4.0.0
-
newHashMap
Create newHashMap
sized to fit all the elements of the size provided.- Type Parameters:
K
- The key typeV
- The value type- Parameters:
size
- The size to fit all the elements- Returns:
- a new
HashMap
with reallocated size - Since:
- 4.0.0
-
newLinkedHashMap
Create newLinkedHashMap
sized to fit all the elements of the size provided.- Type Parameters:
K
- The key typeV
- The value type- Parameters:
size
- The size to fit all the elements- Returns:
- a new
LinkedHashMap
with reallocated size - Since:
- 4.0.0
-
isIterableOrMap
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
Null safe empty check.- Parameters:
map
- The map- Returns:
- True if it is empty or null
-
isNotEmpty
Null safe not empty check.- Parameters:
map
- The map- Returns:
- True if it is not null and not empty
-
isEmpty
Null safe empty check.- Parameters:
collection
- The collection- Returns:
- True if it is empty or null
-
isNotEmpty
Null safe not empty check.- Parameters:
collection
- The collection- Returns:
- True if it is not null and not empty
-
convertCollection
public static <T> Optional<Iterable<T>> convertCollection(Class<? extends Iterable<T>> iterableType, 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
Create aLinkedHashMap
from an array of values.- Parameters:
values
- The values- Returns:
- The created map
-
iteratorToSet
- Type Parameters:
T
- The type- Parameters:
iterator
- The iterator- Returns:
- The set
-
enumerationToSet
Convert anEnumeration
to aSet
.- Type Parameters:
T
- The type- Parameters:
enumeration
- The iterator- Returns:
- The set
-
enumerationToIterable
@NonNull public static <T> @NonNull Iterable<T> enumerationToIterable(@Nullable @Nullable Enumeration<T> enumeration) Convert anEnumeration
to aIterable
.- Type Parameters:
T
- The type- Parameters:
enumeration
- The iterator- Returns:
- The set
-
setOf
Creates a set of the given objects.- Type Parameters:
T
- The type- Parameters:
objects
- The objects- Returns:
- The set
-
toString
Produce a string representation of the given iterable.- Parameters:
iterable
- The iterable- Returns:
- The string representation
-
toString
Produce a string representation of the given iterable.- Parameters:
delimiter
- The delimiteriterable
- The iterable- Returns:
- The string representation
-
iterableToList
- Type Parameters:
T
- The generic type- Parameters:
iterable
- The iterable- Returns:
- The list
-
iterableToSet
- Type Parameters:
T
- The generic type- Parameters:
iterable
- The iterable- Returns:
- The set
-
unmodifiableList
Null safe version ofCollections.unmodifiableList(List)
.- Type Parameters:
T
- The generic type- Parameters:
list
- The list- Returns:
- A non-null unmodifiable list
-
last
Returns the last element of a collection.- Type Parameters:
T
- The generic type- Parameters:
collection
- The collection- Returns:
- The last element of a collection or null
-