Package org.apache.juneau.internal
Class CollectionUtils
java.lang.Object
org.apache.juneau.internal.CollectionUtils
Utility methods for collections.
See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <E> List<E>
Adds all the specified values to the specified collection.static <E> Set<E>
Adds all the specified values to the specified collection.static <E> SortedSet<E>
Adds all the specified values to the specified collection.static <E> List<E>
alist
(E... values) Convenience method for creating an array-backed list by callingArrays.asList(Object...)
.static <E> E[]
array
(Collection<E> value, Class<E> componentType) Converts the specified collection to an array.static <E> Collection<E>
copyOf
(Collection<E> val) Creates a new collection from the specified collection.static <E> ArrayList<E>
Convenience method for copying a list.static <K,
V> Map<K, V> Creates a new map from the specified map.static <E> Set<E>
Creates a new set from the specified collection.static <T> Optional<T>
empty()
Returns an emptyOptional
.static <E> List<E>
Simple passthrough toCollections.emptyList()
static <E> void
forEachReverse
(E[] value, Consumer<E> action) Iterates the specified array in reverse order.static <E> void
forEachReverse
(List<E> value, Consumer<E> action) Iterates the specified list in reverse order.static <E> boolean
isNotEmpty
(Collection<E> value) Returnstrue if the specified collection is notnull and not empty.static <K,
V> boolean isNotEmpty
(Map<K, V> value) Returnstrue if the specified map is notnull and not empty.static <E> E
last
(E[] l) Returns the last entry in an array.static <E> E
Returns the last entry in a list.static <E> LinkedList<E>
linkedList
(E... values) Convenience method for creating aLinkedList
.static <E> ArrayList<E>
list
(int size) Convenience method for creating anArrayList
of the specified size.static <E> ArrayList<E>
list
(E... values) Convenience method for creating anArrayList
.static <E> ListBuilder<E>
listBuilder
(Class<E> elementType, Type... elementTypeArgs) Instantiates a new builder of the specified list type.static <E> ListBuilder<E>
listBuilder
(List<E> addTo) Instantiates a new builder on top of the specified list.static <E> ArrayList<E>
listFrom
(Collection<E> value) Creates anArrayList
copy from a collection.static <E> ArrayList<E>
listFrom
(Collection<E> value, boolean nullIfEmpty) Creates anArrayList
copy from a collection.static <K,
V> LinkedHashMap<K, V> map()
Convenience method for creating aLinkedHashMap
.static <K,
V> LinkedHashMap<K, V> map
(K k1, V v1) Convenience method for creating aLinkedHashMap
.static <K,
V> LinkedHashMap<K, V> map
(K k1, V v1, K k2, V v2) Convenience method for creating aLinkedHashMap
.static <K,
V> LinkedHashMap<K, V> map
(K k1, V v1, K k2, V v2, K k3, V v3) Convenience method for creating aLinkedHashMap
.static <K,
V> MapBuilder<K, V> mapBuilder
(Class<K> keyType, Class<V> valueType, Type... valueTypeArgs) Instantiates a new builder of the specified map type.static <K,
V> MapBuilder<K, V> mapBuilder
(Map<K, V> addTo) Instantiates a new builder on top of the specified map.static <K,
V> LinkedHashMap<K, V> Creates anArrayList
copy from a collection.static <T> Optional<T>
optional
(T value) Returns an optional of the specified value.static <E> List<E>
prependAll
(List<E> value, E... entries) Adds all the specified values to the specified collection.static <E> LinkedHashSet<E>
set
(E... values) Convenience method for creating aLinkedHashSet
.static <E> SetBuilder<E>
setBuilder
(Class<E> elementType, Type... elementTypeArgs) Instantiates a new builder of the specified set.static <E> SetBuilder<E>
setBuilder
(Set<E> addTo) Instantiates a new builder on top of the specified set.static <E> Set<E>
setFrom
(Collection<E> val) Creates a new set from the specified collection.static <E> ArrayList<E>
sortedList
(E... values) Convenience method for creating anArrayList
and sorting it.static <E> ArrayList<E>
sortedList
(Comparator<E> comparator, E[] values) Convenience method for creating anArrayList
and sorting it.static <E> ArrayList<E>
sortedList
(Comparator<E> comparator, Collection<E> value) Convenience method for creating anArrayList
and sorting it.static <K,
V> TreeMap<K, V> Convenience method for creating aTreeMap
.static <E> TreeSet<E>
sortedSet
(E... values) Convenience method for creating aTreeSet
.static <E> TreeSet<E>
sortedSetFrom
(Collection<E> value) Creates a newTreeSet
from the specified collection.static <E> TreeSet<E>
sortedSetFrom
(Collection<E> value, boolean nullIfEmpty) Creates a newTreeSet
from the specified collection.static <E> List<E>
Wraps the specified list inCollections.unmodifiableList(List)
.static <K,
V> Map<K, V> Wraps the specified map inCollections.unmodifiableMap(Map)
.static <E> Set<E>
Wraps the specified set inCollections.unmodifiableSet(Set)
.static <E> List<E>
ulist
(E... values) Convenience method for creating an unmodifiable list.static <E> List<E>
unmodifiable
(List<E> value) Wraps the specified list inCollections.unmodifiableList(List)
.static <K,
V> Map<K, V> unmodifiable
(Map<K, V> value) Wraps the specified map inCollections.unmodifiableMap(Map)
.static <E> Set<E>
unmodifiable
(Set<E> value) Wraps the specified set inCollections.unmodifiableSet(Set)
.static <E> Set<E>
uset
(E... values) Convenience method for creating an unmodifiableLinkedHashSet
.
-
Constructor Details
-
CollectionUtils
public CollectionUtils()
-
-
Method Details
-
setFrom
Creates a new set from the specified collection.- Type Parameters:
E
- The element type.- Parameters:
val
- The value to copy from.- Returns:
- A new
LinkedHashSet
, ornull if the input was null.
-
copyOf
Creates a new set from the specified collection.- Type Parameters:
E
- The element type.- Parameters:
val
- The value to copy from.- Returns:
- A new
LinkedHashSet
, ornull if the input was null.
-
copyOf
Creates a new collection from the specified collection.- Type Parameters:
E
- The element type.- Parameters:
val
- The value to copy from.- Returns:
- A new
LinkedHashSet
, ornull if the input was null.
-
copyOf
Creates a new map from the specified map.- Type Parameters:
K
- The key type.V
- The value type.- Parameters:
val
- The value to copy from.- Returns:
- A new
LinkedHashMap
, ornull if the input was null.
-
mapBuilder
Instantiates a new builder on top of the specified map.- Type Parameters:
K
- The key type.V
- The value type.- Parameters:
addTo
- The map to add to.- Returns:
- A new builder on top of the specified map.
-
mapBuilder
public static <K,V> MapBuilder<K,V> mapBuilder(Class<K> keyType, Class<V> valueType, Type... valueTypeArgs) Instantiates a new builder of the specified map type.- Type Parameters:
K
- The key type.V
- The value type.- Parameters:
keyType
- The key type.valueType
- The value type.valueTypeArgs
- The value type args.- Returns:
- A new builder on top of the specified map.
-
listBuilder
Instantiates a new builder on top of the specified list.- Type Parameters:
E
- The element type.- Parameters:
addTo
- The list to add to.- Returns:
- A new builder on top of the specified list.
-
listBuilder
Instantiates a new builder of the specified list type.- Type Parameters:
E
- The element type.- Parameters:
elementType
- The element type.elementTypeArgs
- The element type args.- Returns:
- A new builder on top of the specified list.
-
setBuilder
Instantiates a new builder on top of the specified set.- Type Parameters:
E
- The element type.- Parameters:
addTo
- The set to add to.- Returns:
- A new builder on top of the specified set.
-
setBuilder
Instantiates a new builder of the specified set.- Type Parameters:
E
- The element type.- Parameters:
elementType
- The element type.elementTypeArgs
- The element type args.- Returns:
- A new builder on top of the specified set.
-
emptyList
Simple passthrough toCollections.emptyList()
- Type Parameters:
E
- The element type.- Returns:
- A new unmodifiable empty list.
-
list
Convenience method for creating anArrayList
.- Type Parameters:
E
- The element type.- Parameters:
values
- The values to initialize the list with.- Returns:
- A new modifiable list.
-
list
Convenience method for creating anArrayList
of the specified size.- Type Parameters:
E
- The element type.- Parameters:
size
- The initial size of the list.- Returns:
- A new modifiable list.
-
linkedList
Convenience method for creating aLinkedList
.- Type Parameters:
E
- The element type.- Parameters:
values
- The values to initialize the list with.- Returns:
- A new modifiable list.
-
alist
Convenience method for creating an array-backed list by callingArrays.asList(Object...)
.- Type Parameters:
E
- The element type.- Parameters:
values
- The values to initialize the list with.- Returns:
- A new modifiable list, or
null if the array wasnull .
-
listFrom
Creates anArrayList
copy from a collection.- Type Parameters:
E
- The element type.- Parameters:
value
- The collection to copy from.- Returns:
- A new modifiable list.
-
mapFrom
Creates anArrayList
copy from a collection.- Type Parameters:
K
- The key type.V
- The value type.- Parameters:
value
- The collection to copy from.- Returns:
- A new modifiable list.
-
listFrom
Creates anArrayList
copy from a collection.- Type Parameters:
E
- The element type.- Parameters:
value
- The collection to copy from.nullIfEmpty
- Iftrue will returnnull if the collection is empty.- Returns:
- A new modifiable list.
-
set
Convenience method for creating aLinkedHashSet
.- Type Parameters:
E
- The element type.- Parameters:
values
- The values to initialize the set with.- Returns:
- A new modifiable set.
-
uset
Convenience method for creating an unmodifiableLinkedHashSet
.- Type Parameters:
E
- The element type.- Parameters:
values
- The values to initialize the set with.- Returns:
- A new unmodifiable set.
-
ulist
Convenience method for creating an unmodifiable list.- Type Parameters:
E
- The element type.- Parameters:
values
- The values to initialize the list with.- Returns:
- A new unmodifiable list, or
null if the array wasnull .
-
sortedSet
Convenience method for creating aTreeSet
.- Type Parameters:
E
- The element type.- Parameters:
values
- The values to initialize the set with.- Returns:
- A new modifiable set.
-
sortedSetFrom
Creates a newTreeSet
from the specified collection.- Type Parameters:
E
- The element type.- Parameters:
value
- The value to copy from.- Returns:
- A new
TreeSet
, ornull if the input was null.
-
sortedSetFrom
Creates a newTreeSet
from the specified collection.- Type Parameters:
E
- The element type.- Parameters:
value
- The value to copy from.nullIfEmpty
- Iftrue returnsnull if the collection is empty.- Returns:
- A new
TreeSet
, ornull if the input was null.
-
map
Convenience method for creating aLinkedHashMap
.- Type Parameters:
K
- The key type.V
- The value type.- Returns:
- A new modifiable map.
-
map
Convenience method for creating aLinkedHashMap
.- Type Parameters:
K
- The key type.V
- The value type.- Parameters:
k1
- Key 1.v1
- Value 1.- Returns:
- A new modifiable map.
-
map
Convenience method for creating aLinkedHashMap
.- Type Parameters:
K
- The key type.V
- The value type.- Parameters:
k1
- Key 1.v1
- Value 1.k2
- Key 2.v2
- Value 2.- Returns:
- A new modifiable map.
-
map
Convenience method for creating aLinkedHashMap
.- Type Parameters:
K
- The key type.V
- The value type.- Parameters:
k1
- Key 1.v1
- Value 1.k2
- Key 2.v2
- Value 2.k3
- Key 3.v3
- Value 3.- Returns:
- A new modifiable map.
-
sortedMap
Convenience method for creating aTreeMap
.- Type Parameters:
K
- The key type.V
- The value type.- Returns:
- A new modifiable set.
-
copyOf
Convenience method for copying a list.- Type Parameters:
E
- The element type.- Parameters:
value
- The list to copy.- Returns:
- A new modifiable list.
-
sortedList
Convenience method for creating anArrayList
and sorting it.- Type Parameters:
E
- The element type.- Parameters:
values
- The values to initialize the list with.- Returns:
- A new modifiable list.
-
sortedList
Convenience method for creating anArrayList
and sorting it.- Type Parameters:
E
- The element type.- Parameters:
comparator
- The comparator to use to sort the list.values
- The values to initialize the list with.- Returns:
- A new modifiable list.
-
sortedList
Convenience method for creating anArrayList
and sorting it.- Type Parameters:
E
- The element type.- Parameters:
comparator
- The comparator to use to sort the list.value
- The values to initialize the list with.- Returns:
- A new modifiable list.
-
unmodifiable
Wraps the specified list inCollections.unmodifiableList(List)
.- Type Parameters:
E
- The element type.- Parameters:
value
- The list to wrap.- Returns:
- The wrapped list.
-
unmodifiable
Wraps the specified set inCollections.unmodifiableSet(Set)
.- Type Parameters:
E
- The element type.- Parameters:
value
- The set to wrap.- Returns:
- The wrapped set.
-
unmodifiable
Wraps the specified map inCollections.unmodifiableMap(Map)
.- Type Parameters:
K
- The key type.V
- The value type.- Parameters:
value
- The map to wrap.- Returns:
- The wrapped map.
-
synced
Wraps the specified list inCollections.unmodifiableList(List)
.- Type Parameters:
E
- The element type.- Parameters:
value
- The list to wrap.- Returns:
- The wrapped list.
-
synced
Wraps the specified set inCollections.unmodifiableSet(Set)
.- Type Parameters:
E
- The element type.- Parameters:
value
- The set to wrap.- Returns:
- The wrapped set.
-
synced
Wraps the specified map inCollections.unmodifiableMap(Map)
.- Type Parameters:
K
- The key type.V
- The value type.- Parameters:
value
- The map to wrap.- Returns:
- The wrapped map.
-
array
Converts the specified collection to an array.- Type Parameters:
E
- The element type.- Parameters:
value
- The collection to convert.componentType
- The component type of the array.- Returns:
- A new array.
-
forEachReverse
Iterates the specified list in reverse order.- Type Parameters:
E
- The element type.- Parameters:
value
- The list to iterate.action
- The action to perform.
-
forEachReverse
Iterates the specified array in reverse order.- Type Parameters:
E
- The element type.- Parameters:
value
- The array to iterate.action
- The action to perform.
-
addAll
Adds all the specified values to the specified collection. Creates a new set if the value isnull .- Type Parameters:
E
- The element type.- Parameters:
value
- The collection to add to.entries
- The entries to add.- Returns:
- The set.
-
addAll
Adds all the specified values to the specified collection. Creates a new set if the value isnull .- Type Parameters:
E
- The element type.- Parameters:
value
- The collection to add to.entries
- The entries to add.- Returns:
- The set.
-
addAll
Adds all the specified values to the specified collection. Creates a new set if the value isnull .- Type Parameters:
E
- The element type.- Parameters:
value
- The collection to add to.entries
- The entries to add.- Returns:
- The set.
-
prependAll
Adds all the specified values to the specified collection. Creates a new set if the value isnull .- Type Parameters:
E
- The element type.- Parameters:
value
- The collection to add to.entries
- The entries to add.- Returns:
- The set.
-
last
Returns the last entry in a list.- Type Parameters:
E
- The element type.- Parameters:
l
- The list.- Returns:
- The last element, or
null if the list isnull or empty.
-
last
Returns the last entry in an array.- Type Parameters:
E
- The element type.- Parameters:
l
- The array.- Returns:
- The last element, or
null if the array isnull or empty.
-
optional
Returns an optional of the specified value.- Type Parameters:
T
- The component type.- Parameters:
value
- The value.- Returns:
- A new Optional.
-
empty
Returns an emptyOptional
.- Type Parameters:
T
- The component type.- Returns:
- An empty
Optional
.
-
isNotEmpty
Returnstrue if the specified collection is notnull and not empty.- Type Parameters:
E
- The element type.- Parameters:
value
- The value being checked.- Returns:
true if the specified collection is notnull and not empty.
-
isNotEmpty
Returnstrue if the specified map is notnull and not empty.- Type Parameters:
K
- The key type.V
- The value type.- Parameters:
value
- The value being checked.- Returns:
true if the specified map is notnull and not empty.
-