Class CollectionUtils
Complex Data Structure Construction:
This class provides convenient shorthand methods for creating complex nested data structures. The primary methods for building structures are:
a(Object...)- Creates arraysao(Object...)- Creates Object arrays (when type inference needs help)list(Object...)/l(Object...)- Creates modifiable listsmap(Object, Object, Object, Object, Object, Object)- Creates modifiable mapsm(Object, Object, Object, Object, Object, Object)- Creates unmodifiable maps
Examples:
Best Practices:
- Use
a(Object...)for arrays when type can be inferred,ao(Object...)for Object arrays - Use
map(Object, Object, Object, Object, Object, Object)when you need modifiable maps - Use
m(Object, Object, Object, Object, Object, Object)when you need immutable/unmodifiable maps - Use
list(Object...)orl(Object...)for modifiable lists - Static import these methods for maximum readability:
import static org.apache.juneau.commons.utils.CollectionUtils.*;
See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T[]a(T... x) Creates an array of objects.static <E> E[][]a2(E[]... value) Creates a 2-dimensional array.static <T> List<T>accumulate(Object o) Traverses all elements in the specified object and accumulates them into a list.static <E> List<E>Adds all the specified values to the specified collection.static <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 <T> T[]addAll(T[] array, T... newElements) Appends one or more elements to an array.static <T> ArrayList<T>al(T... values) Shortcut for creating an ArrayList from the specified values.static Object[]Creates an array of objects with the return type explicitly set toObject[].static <E> E[]Creates an array of the specified component type and length.static <E> E[]array(Collection<E> value, Class<E> componentType) Converts the specified collection to an array.arrayToList(Object array) Converts any array (including primitive arrays) to a List.static boolean[]booleans(boolean... value) Shortcut for creating a boolean array.static byte[]bytes(int... value) Shortcut for creating a byte array.static char[]chars(char... value) Shortcut for creating a char array.static <E> E[]combine(E[]... arrays) Combine an arbitrary number of arrays into a single array.static <T> booleancontains(T element, T[] array) Returnstrue if the specified array contains the specified element using theString.equals(Object)method.static ListcopyArrayToList(Object array, List list) Copies the specified array into the specified list.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 <E> List<E>Makes a deep copy of the specified list.static <E> List<E>Makes a deep copy of the specified list.static <K,V> Map<K, V> Creates a new map from the specified map.static <K,V> Map<K, V> Makes a deep copy of the specified map.static <K,V> Map<K, V> Makes a deep copy of the specified map.static <E> Set<E>Creates a new set from the specified collection.static <E> Set<E>Makes a deep copy of the specified list.static <E> Set<E>Makes a deep copy of the specified list.static <T> T[]copyOf(T[] array) Makes a copy of the specified array.static double[]doubles(double... value) Shortcut for creating a double array.static <E> EReturns the first element in a list.static float[]floats(float... value) Shortcut for creating a float array.static <E> voidforEachReverse(E[] value, Consumer<E> action) Iterates the specified array in reverse order.static <E> voidforEachReverse(List<E> value, Consumer<E> action) Iterates the specified list in reverse order.static <T> HashSet<T>hs(T... values) Shortcut for creating a HashSet from the specified values.static <T> intindexOf(T element, T[] array) Returns the index position of the element in the specified array using theString.equals(Object)method.static int[]ints(int... value) Shortcut for creating an int array.static booleanisEmptyArray(Object[] array) Returnstrue if the specified array is null or has a length of zero.static booleanisNotEmptyArray(Object[] array) Returnstrue if the specified array is not null and has a length greater than zero.static <T> List<T>l(T... values) Shortcut for creating an unmodifiable list out of an array of values.static <E> Elast(E[] l) Returns the last entry in an array.static <E> EReturns the last entry in a list.static intReturns the length of the specified array.static <T> List<T>list(T... values) Shortcut for creating a modifiable list out of an array of values.static <E> Lists<E>Convenience factory for aLists.static <T> List<T>liste()Shortcut for creating an empty list.static <T> List<T>Shortcut for creating an empty list of the specified type.static <T> List<T>Returns a null list.static <E> List<E>Convenience method for creating anArrayList.static <E> ArrayList<E>listOfSize(int size) Convenience method for creating anArrayListof the specified size.static <T> LinkedList<T>ll(T... values) Shortcut for creating a LinkedList from the specified values.static long[]longs(long... value) Shortcut for creating a long array.static <K,V> Map<K, V> m()Convenience method for creating an unmodifiable map.static <K,V> Map<K, V> m(K k1, V v1) Convenience method for creating an unmodifiable map.static <K,V> Map<K, V> m(K k1, V v1, K k2, V v2) Convenience method for creating an unmodifiable map.static <K,V> Map<K, V> m(K k1, V v1, K k2, V v2, K k3, V v3) Convenience method for creating an unmodifiable map.static <K,V> Map<K, V> m(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) Convenience method for creating an unmodifiable map.static <K,V> Map<K, V> m(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) Convenience method for creating an unmodifiable map.static <K,V> Map<K, V> m(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) Convenience method for creating an unmodifiable map.static <K,V> Map<K, V> m(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7) Convenience method for creating an unmodifiable map.static <K,V> Map<K, V> m(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8) Convenience method for creating an unmodifiable map.static <K,V> Map<K, V> m(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9) Convenience method for creating an unmodifiable map.static <K,V> Map<K, V> m(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10) Convenience method for creating an unmodifiable map.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> LinkedHashMap<K, V> map(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) Convenience method for creating aLinkedHashMap.static <K,V> LinkedHashMap<K, V> map(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) Convenience method for creating aLinkedHashMap.static <K,V> LinkedHashMap<K, V> map(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) Convenience method for creating aLinkedHashMap.static <K,V> LinkedHashMap<K, V> map(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7) Convenience method for creating aLinkedHashMap.static <K,V> LinkedHashMap<K, V> map(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8) Convenience method for creating aLinkedHashMap.static <K,V> LinkedHashMap<K, V> map(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9) Convenience method for creating aLinkedHashMap.static <K,V> LinkedHashMap<K, V> map(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10) Convenience method for creating aLinkedHashMap.static <K,V> Maps<K, V> mapb()Convenience factory for aMapsbuilder.static <K,V> Maps<K, V> Convenience factory for aMaps.mapb_so()static <K,V> Map<K, V> mape()Shortcut for creating an empty map with generic types.static <K,V> Map<K, V> Shortcut for creating an empty map of the specified types.static <K,V> Map<K, V> Returns a null map.static <K,V> LinkedHashMap<K, V> Convenience method for creating aLinkedHashMap.static <T> T[]Returnsnull for the specified array type.static <E> List<E>prependAll(List<E> value, E... entries) Adds all the specified values to the specified collection.static <E> E[]reverse(E[] array) Reverses the elements in the specified array in-place.static <E> List<E>Returns a reversed view of the specified list.static <T> Stream<T>Returns a reverse stream of the specified list.static <T> LinkedHashSet<T>set(T... values) Shortcut for creating a modifiable set out of an array of values.static <E> Sets<E>Convenience factory for aSets.static <T> Set<T>sete()Returns an empty immutable set.static <E> LinkedHashSet<E>Convenience method for creating aLinkedHashSet.static short[]shorts(int... value) Shortcut for creating a short array.static <E> List<E>sortedList(E... values) Convenience method for creating anArrayListand sorting it.static <E> List<E>sortedList(Comparator<E> comparator, E[] values) Convenience method for creating anArrayListand sorting it.static <E> ArrayList<E>sortedList(Comparator<E> comparator, Collection<E> value) Convenience method for creating anArrayListand 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 <T> Stream<T>stream(T[] array) Returns a stream of the specified array.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> ObjecttoArray(Collection<?> c, Class<E> elementType) Converts the specified collection to an array.static final List<?>Converts various collection-like objects to aList.static <E> List<E>Converts the specified array to anArrayList static <E> ArrayList<E>toList(Collection<E> value) Creates anArrayListcopy from a collection.static <E> ArrayList<E>toList(Collection<E> value, boolean nullIfEmpty) Creates anArrayListcopy from a collection.toObjectList(Object array) Recursively converts the specified array into a list of objects.static <E> Set<E>toSet(Collection<E> val) Creates a new set from the specified collection.static <T> Set<T>toSet(T[] array) Converts the specified array to aSet .static <E> TreeSet<E>toSortedSet(Collection<E> value) Creates a newTreeSetfrom the specified collection.static <E> TreeSet<E>toSortedSet(Collection<E> value, boolean nullIfEmpty) Creates a newTreeSetfrom the specified collection.static <T> TreeSet<T>toSortedSet(Set<T> copyFrom) Creates a newTreeSetcontaining a copy of the specified set.Converts an array to a stream of objects.static String[]toStringArray(Collection<?> c) Converts the specified collection to an array of strings.static <T> voidTraverses all elements in the specified object and executes a consumer for it.static <T extends Comparable<T>>
TreeSet<T>ts(T... values) Shortcut for creating a TreeSet from the specified values.static <T> List<T>Creates an unmodifiable view of the specified list.static <K,V> Map<K, V> Creates an unmodifiable view of the specified map.static <T> Set<T>Creates an unmodifiable view of the specified set.
-
Method Details
-
a
Creates an array of objects.- Type Parameters:
T- The component type of the array.- Parameters:
x- The objects to place in the array.- Returns:
- A new array containing the specified objects.
-
a2
Creates a 2-dimensional array.This method provides a convenient way to create 2D arrays with cleaner syntax than traditional Java. While you could technically use
a(a(...), a(...)), that approach fails for single-row arrays likea(a(...))because the type system collapses it into a 1D array.Examples:
// 2D array with multiple rows String[][]matrix = a2( a("a" ,"b" ,"c" ), a("d" ,"e" ,"f" ), a("g" ,"h" ,"i" ) );// Single row - this works correctly with a2() String[][]singleRow = a2(a("x" ,"y" ,"z" ));// Returns String[1][3] // Without a2(), this would fail (becomes 1D array): // String[] badAttempt = a(a("x", "y", "z")); // Wrong! Returns String[3] // Empty 2D array String[][]empty = a2();// Returns String[0][] // Compare with traditional Java syntax: String[][]traditional =new String[][] { {"a" ,"b" ,"c" }, {"d" ,"e" ,"f" }, {"g" ,"h" ,"i" } };Use Cases:
- Creating matrices or grids
- Building test data with multiple rows
- Representing tabular data
- Any scenario requiring a 2D array structure
- Type Parameters:
E- The element type of the inner arrays.- Parameters:
value- The 1D arrays that will become rows in the 2D array.- Returns:
- A 2D array containing the specified rows.
-
accumulate
Traverses all elements in the specified object and accumulates them into a list.- Type Parameters:
T- The element type.- Parameters:
o- The object to traverse.- Returns:
- A list containing all accumulated elements.
-
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.
-
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
Appends one or more elements to an array.- Type Parameters:
T- The element type.- Parameters:
array- The array to append to.newElements- The new elements to append to the array.- Returns:
- A new array with the specified elements appended.
-
ao
Creates an array of objects with the return type explicitly set toObject[].This method is useful when you need to force the return type to be
Object[]regardless of the actual types of the elements passed in. This is particularly helpful in scenarios where:- You're mixing different types in the same array
- You need to avoid type inference issues with the generic
a()method - You're working with APIs that specifically require
Object[] - You want to ensure maximum flexibility in what can be stored in the array
Examples:
// Mixed types - works perfectly with ao() Object[]mixed = ao("string" , 42,true , 3.14,null );// Force Object[] return type even for uniform types Object[]strings = ao("a" ,"b" ,"c" );// Returns Object[], not String[] // Compare with a() which infers the most specific type: String[]typed = a("a" ,"b" ,"c" );// Returns String[] // Useful when you need Object[] for APIs: void someMethod(Object[] args) { ... } someMethod(ao("test" , 123,true ));// No type issues // Empty Object array Object[]empty = ao();// With null values Object[]withNulls = ao("value" ,null , 42,null );When to Use:
- Use
a()when you want type inference for homogeneous arrays - Use
ao()when you explicitly needObject[]or have mixed types
- Parameters:
value- The objects to place in the array.- Returns:
- A new
Object[]containing the specified objects.
-
array
Creates an array of the specified component type and length.- Type Parameters:
E- The component type of the array.- Parameters:
componentType- The component type of the array.length- The length of the array.- Returns:
- A new array of the specified type and length. Never
null .
-
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.
-
arrayToList
Converts any array (including primitive arrays) to a List.- Parameters:
array- The array to convert. Can be any array type including primitives.- Returns:
- A List containing the array elements. Primitive values are auto-boxed. Returns null if the input is null.
- Throws:
IllegalArgumentException- if the input is not an array.
-
booleans
Shortcut for creating a boolean array.Example:
boolean []myArray =booleans (true ,false ,true );- Parameters:
value- The values to initialize the array with.- Returns:
- A new boolean array.
-
bytes
Shortcut for creating a byte array.Accepts int values and converts them to bytes, eliminating the need for explicit byte casts.
Example:
byte []myArray =bytes (1, 2, 3);- Parameters:
value- The int values to convert to bytes. Values outside the byte range (-128 to 127) will be truncated.- Returns:
- A new byte array.
-
chars
Shortcut for creating a char array.Example:
char []myArray =chars ('a' ,'b' ,'c' );- Parameters:
value- The values to initialize the array with.- Returns:
- A new char array.
-
combine
Combine an arbitrary number of arrays into a single array.- Type Parameters:
E- The element type.- Parameters:
arrays- Collection of arrays to combine.- Returns:
- A new combined array, or
null if all arrays arenull .
-
contains
Returnstrue if the specified array contains the specified element using theString.equals(Object)method.- Parameters:
element- The element to check for.array- The array to check.- Returns:
true if the specified array contains the specified element,false if the array or element isnull .
-
copyArrayToList
Copies the specified array into the specified list.Works on both object and primitive arrays.
- Parameters:
array- The array to copy into a list.list- The list to copy the values into.- Returns:
- The same list passed in.
-
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
Convenience method for copying a list.- Type Parameters:
E- The element type.- Parameters:
value- The list to copy.- Returns:
- A new modifiable list.
-
copyOf
Makes a deep copy of the specified list.- Type Parameters:
E- The entry type.- Parameters:
l- The list to copy.valueMapper- The function to apply to each value in the list.- Returns:
- A new list with the same values as the specified list, but with values transformed by the specified function. Null if the list being copied was null.
-
copyOf
public static <E> List<E> copyOf(List<E> l, Function<? super E, ? extends E> valueMapper, Supplier<List<E>> listFactory) Makes a deep copy of the specified list.- Type Parameters:
E- The entry type.- Parameters:
l- The list to copy.valueMapper- The function to apply to each value in the list.listFactory- The factory for creating the list.- Returns:
- A new list with the same values as the specified list, but with values transformed by the specified function. Null if the list being copied 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.
-
copyOf
Makes a deep copy of the specified map.- Type Parameters:
K- The key type.V- The value type.- Parameters:
m- The map to copy.valueMapper- The function to apply to each value in the map.- Returns:
- A new map with the same keys as the specified map, but with values transformed by the specified function. Null if the map being copied was null.
-
copyOf
public static <K,V> Map<K,V> copyOf(Map<K, V> m, Function<? super V, ? extends V> valueMapper, Supplier<Map<K, V>> mapFactory) Makes a deep copy of the specified map.- Type Parameters:
K- The key type.V- The value type.- Parameters:
m- The map to copy.valueMapper- The function to apply to each value in the map.mapFactory- The factory for creating the map.- Returns:
- A new map with the same keys as the specified map, but with values transformed by the specified function. Null if the map being copied 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
Makes a deep copy of the specified list.- Type Parameters:
E- The entry type.- Parameters:
l- The list to copy.valueMapper- The function to apply to each value in the list.- Returns:
- A new list with the same values as the specified list, but with values transformed by the specified function. Null if the list being copied was null.
-
copyOf
public static <E> Set<E> copyOf(Set<E> l, Function<? super E, ? extends E> valueMapper, Supplier<Set<E>> setFactory) Makes a deep copy of the specified list.- Type Parameters:
E- The entry type.- Parameters:
l- The list to copy.valueMapper- The function to apply to each value in the list.setFactory- The factory for creating sets.- Returns:
- A new list with the same values as the specified list, but with values transformed by the specified function. Null if the list being copied was null.
-
copyOf
Makes a copy of the specified array.- Type Parameters:
T- The element type.- Parameters:
array- The array to copy.- Returns:
- A new copy of the array, or
null if the array wasnull .s
-
doubles
Shortcut for creating a double array.Example:
double []myArray =doubles (1.0, 2.0, 3.0);- Parameters:
value- The values to initialize the array with.- Returns:
- A new double array.
-
first
Returns the first element in a list.- Type Parameters:
E- The element type.- Parameters:
l- The list. Can benull .- Returns:
- The first element in the list, or
null if the list isnull or empty.
-
floats
Shortcut for creating a float array.Example:
float []myArray =floats (1.0f, 2.0f, 3.0f);- Parameters:
value- The values to initialize the array with.- Returns:
- A new float array.
-
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.
-
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.
-
indexOf
Returns the index position of the element in the specified array using theString.equals(Object)method.- Parameters:
element- The element to check for.array- The array to check.- Returns:
- The index position of the element in the specified array, or
-1 if the array doesn't contain the element, or the array or element isnull .
-
ints
Shortcut for creating an int array.Example:
int []myArray =ints (1, 2, 3);- Parameters:
value- The values to initialize the array with.- Returns:
- A new int array.
-
isEmptyArray
Returnstrue if the specified array is null or has a length of zero.- Parameters:
array- The array to check.- Returns:
true if the specified array is null or has a length of zero.
-
isNotEmptyArray
Returnstrue if the specified array is not null and has a length greater than zero.- Parameters:
array- The array to check.- Returns:
true if the specified array is not null and has a length greater than zero.
-
l
Shortcut for creating an unmodifiable list out of an array of values.- Type Parameters:
T- The element type.- Parameters:
values- The values to add to the list.- Returns:
- An unmodifiable list containing the specified values, or
null if the input isnull .
-
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.
-
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.
-
length
Returns the length of the specified array.This is a null-safe convenience method that wraps
Array.getLength(Object). Works with both object arrays and primitive arrays.Example:
String[]
array1 = {"foo" ,"bar" };int []array2 = {1, 2, 3};int len1 = length(array1 );// Returns 2 int len2 = length(array2 );// Returns 3 int len3 = length(null );// Returns 0 - Parameters:
array- The array object. Can benull .- Returns:
- The length of the array, or
0 if the array isnull or not an array.
-
list
Shortcut for creating a modifiable list out of an array of values.- Type Parameters:
T- The element type.- Parameters:
values- The values to add to the list.- Returns:
- A modifiable list containing the specified values.
-
al
Shortcut for creating an ArrayList from the specified values.This is a convenience method that creates a modifiable ArrayList.
Example:
List<String>
list = al("a" ,"b" ,"c" );- Type Parameters:
T- The element type.- Parameters:
values- The values to add to the list.- Returns:
- A new modifiable ArrayList containing the specified values.
- See Also:
-
ll
Shortcut for creating a LinkedList from the specified values.This is a convenience method that creates a modifiable LinkedList.
Example:
List<String>
list = ll("a" ,"b" ,"c" );- Type Parameters:
T- The element type.- Parameters:
values- The values to add to the list.- Returns:
- A new modifiable LinkedList containing the specified values.
-
hs
Shortcut for creating a HashSet from the specified values.This is a convenience method that creates a modifiable HashSet.
Example:
Set<String>
set = hs("a" ,"b" ,"c" );- Type Parameters:
T- The element type.- Parameters:
values- The values to add to the set.- Returns:
- A new modifiable HashSet containing the specified values.
-
ts
Shortcut for creating a TreeSet from the specified values.This is a convenience method that creates a modifiable TreeSet. Values must be comparable.
Example:
Set<String>
set = ts("c" ,"a" ,"b" );// Sorted: a, b, c - Type Parameters:
T- The element type (must be Comparable).- Parameters:
values- The values to add to the set.- Returns:
- A new modifiable TreeSet containing the specified values.
-
listb
Convenience factory for aLists.- Type Parameters:
E- The element type.- Parameters:
type- The element type.- Returns:
- A new list builder.
-
liste
Shortcut for creating an empty list.This is a convenience method that provides a more concise syntax than
Collections.emptyList(). The "e" suffix indicates "empty".Example:
List<String>
myList =liste ();- Type Parameters:
T- The element type.- Returns:
- An empty unmodifiable list.
-
liste
Shortcut for creating an empty list of the specified type.- Type Parameters:
T- The element type.- Parameters:
type- The element type class.- Returns:
- An empty list.
-
listn
Returns a null list.- Type Parameters:
T- The element type.- Parameters:
type- The element type class.- Returns:
null .
-
listOf
Convenience method for creating anArrayList.- Type Parameters:
E- The element type.- Parameters:
elementType- The element type.values- The values to initialize the list with.- Returns:
- A new modifiable list.
-
listOfSize
Convenience method for creating anArrayListof the specified size.- Type Parameters:
E- The element type.- Parameters:
size- The initial size of the list.- Returns:
- A new modifiable list.
-
longs
Shortcut for creating a long array.Example:
long []myArray =longs (1L, 2L, 3L);- Parameters:
value- The values to initialize the array with.- Returns:
- A new long array.
-
m
Convenience method for creating an unmodifiable map.- Type Parameters:
K- The key type.V- The value type.- Returns:
- A new unmodifiable empty map.
-
m
Convenience method for creating an unmodifiable map. Unlike Map.of(...), supports null keys/values and preserves insertion order.- Type Parameters:
K- The key type.V- The value type.- Parameters:
k1- Key 1.v1- Value 1.- Returns:
- A new unmodifiable map.
-
m
Convenience method for creating an unmodifiable map. Unlike Map.of(...), supports null keys/values and preserves insertion order.- 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 unmodifiable map.
-
m
Convenience method for creating an unmodifiable map. Unlike Map.of(...), supports null keys/values and preserves insertion order.- 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 unmodifiable map.
-
m
Convenience method for creating an unmodifiable map. Unlike Map.of(...), supports null keys/values and preserves insertion order.- 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.k4- Key 4.v4- Value 4.- Returns:
- A new unmodifiable map.
-
m
Convenience method for creating an unmodifiable map. Unlike Map.of(...), supports null keys/values and preserves insertion order.- 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.k4- Key 4.v4- Value 4.k5- Key 5.v5- Value 5.- Returns:
- A new unmodifiable map.
-
m
public static <K,V> Map<K,V> m(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) Convenience method for creating an unmodifiable map. Unlike Map.of(...), supports null keys/values and preserves insertion order.- 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.k4- Key 4.v4- Value 4.k5- Key 5.v5- Value 5.k6- Key 6.v6- Value 6.- Returns:
- A new unmodifiable map.
-
m
public static <K,V> Map<K,V> m(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7) Convenience method for creating an unmodifiable map. Unlike Map.of(...), supports null keys/values and preserves insertion order.- 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.k4- Key 4.v4- Value 4.k5- Key 5.v5- Value 5.k6- Key 6.v6- Value 6.k7- Key 7.v7- Value 7.- Returns:
- A new unmodifiable map.
-
m
public static <K,V> Map<K,V> m(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8) Convenience method for creating an unmodifiable map. Unlike Map.of(...), supports null keys/values and preserves insertion order.- 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.k4- Key 4.v4- Value 4.k5- Key 5.v5- Value 5.k6- Key 6.v6- Value 6.k7- Key 7.v7- Value 7.k8- Key 8.v8- Value 8.- Returns:
- A new unmodifiable map.
-
m
public static <K,V> Map<K,V> m(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9) Convenience method for creating an unmodifiable map. Unlike Map.of(...), supports null keys/values and preserves insertion order.- 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.k4- Key 4.v4- Value 4.k5- Key 5.v5- Value 5.k6- Key 6.v6- Value 6.k7- Key 7.v7- Value 7.k8- Key 8.v8- Value 8.k9- Key 9.v9- Value 9.- Returns:
- A new unmodifiable map.
-
m
public static <K,V> Map<K,V> m(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10) Convenience method for creating an unmodifiable map. Unlike Map.of(...), supports null keys/values and preserves insertion order.- 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.k4- Key 4.v4- Value 4.k5- Key 5.v5- Value 5.k6- Key 6.v6- Value 6.k7- Key 7.v7- Value 7.k8- Key 8.v8- Value 8.k9- Key 9.v9- Value 9.k10- Key 10.v10- Value 10.- Returns:
- A new unmodifiable map.
-
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.
-
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.k4- Key 4.v4- Value 4.- Returns:
- A new modifiable map.
-
map
public static <K,V> LinkedHashMap<K,V> map(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) 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.k4- Key 4.v4- Value 4.k5- Key 5.v5- Value 5.- Returns:
- A new modifiable map.
-
map
public static <K,V> LinkedHashMap<K,V> map(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) 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.k4- Key 4.v4- Value 4.k5- Key 5.v5- Value 5.k6- Key 6.v6- Value 6.- Returns:
- A new modifiable map.
-
map
public static <K,V> LinkedHashMap<K,V> map(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7) 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.k4- Key 4.v4- Value 4.k5- Key 5.v5- Value 5.k6- Key 6.v6- Value 6.k7- Key 7.v7- Value 7.- Returns:
- A new modifiable map.
-
map
public static <K,V> LinkedHashMap<K,V> map(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8) 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.k4- Key 4.v4- Value 4.k5- Key 5.v5- Value 5.k6- Key 6.v6- Value 6.k7- Key 7.v7- Value 7.k8- Key 8.v8- Value 8.- Returns:
- A new modifiable map.
-
map
public static <K,V> LinkedHashMap<K,V> map(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9) 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.k4- Key 4.v4- Value 4.k5- Key 5.v5- Value 5.k6- Key 6.v6- Value 6.k7- Key 7.v7- Value 7.k8- Key 8.v8- Value 8.k9- Key 9.v9- Value 9.- Returns:
- A new modifiable map.
-
map
public static <K,V> LinkedHashMap<K,V> map(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10) 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.k4- Key 4.v4- Value 4.k5- Key 5.v5- Value 5.k6- Key 6.v6- Value 6.k7- Key 7.v7- Value 7.k8- Key 8.v8- Value 8.k9- Key 9.v9- Value 9.k10- Key 10.v10- Value 10.- Returns:
- A new modifiable map.
-
mapb
Convenience factory for aMapsbuilder.This is a shortcut for
Maps.create().ordered() .Example:
Map<String,Object>
map = mapb() .add("foo" , 1) .add("bar" , 2) .build();- Type Parameters:
K- The key type.V- The value type.- Returns:
- A new map builder.
- See Also:
-
mapb_so
Convenience factory for aMapswithStringkeys andObjectvalues.This is a shortcut for
Maps.create(String. .class , Object.class )Example:
Map<String,Object>
map = mapb() .add("foo" , 1) .add("bar" , 2) .build();This builder supports optional filtering to automatically exclude unwanted values:
// Build a map excluding null and empty values Map<String,Object>map = mapb().filtered() .add("foo" ,null )// Excluded .add("bar" ,"" )// Excluded .add("baz" ,"value" )// Included .build();- Returns:
- A new map builder.
- See Also:
-
filteredBeanPropertyMap
Convenience factory for a filtered, sorted, fluent map withStringkeys andObjectvalues.This is a shortcut for
Maps.create(String. .class , Object.class ).filtered().sorted().buildFluent()This is typically used for creating property maps in
toString() methods that need to be sorted and filtered.Example:
// In a properties() method return filteredBeanPropertyMap() .a("name" ,name ) .a("value" ,value ); -
mapb
Convenience factory for aMaps.- Type Parameters:
K- The key type.V- The value type.- Parameters:
keyType- The key type.valueType- The value type.- Returns:
- A new map builder.
-
mape
Shortcut for creating an empty map of the specified types.- Type Parameters:
K- The key type.V- The value type.- Parameters:
keyType- The key type class.valueType- The value type class.- Returns:
- An empty unmodifiable map.
-
mape
Shortcut for creating an empty map with generic types.This is a convenience method that creates an empty unmodifiable map without requiring explicit type parameters. The types will be inferred from usage context.
- Type Parameters:
K- The key type.V- The value type.- Returns:
- An empty unmodifiable map.
-
mapn
Returns a null map.- Type Parameters:
K- The key type.V- The value type.- Parameters:
keyType- The key type class.valueType- The value type class.- Returns:
null .
-
mapOf
Convenience method for creating aLinkedHashMap.- Type Parameters:
K- The key type.V- The value type.- Parameters:
keyType- The key type.valueType- The value type.- Returns:
- A new modifiable map.
-
na
Returnsnull for the specified array type.- Type Parameters:
T- The component type.- Parameters:
type- The component type class.- Returns:
null .
-
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.
-
reverse
Reverses the elements in the specified array in-place.- Type Parameters:
E- The element type.- Parameters:
array- The array to reverse.- Returns:
- The same array, reversed.
-
reverse
Returns a reversed view of the specified list.The returned list is a live view that reflects changes to the original list.
- Type Parameters:
E- The element type.- Parameters:
list- The list to reverse.- Returns:
- A reversed view of the list.
-
rstream
Returns a reverse stream of the specified list.Example:
List<String>
list =list ("a" ,"b" ,"c" ); Stream<String>reversed =rstream (list );// Produces stream: "c", "b", "a" - Type Parameters:
T- The element type.- Parameters:
value- The list to stream in reverse order. Can benull .- Returns:
- A stream of the list elements in reverse order, or an empty stream if the list is
null or empty.
-
set
Shortcut for creating a modifiable set out of an array of values.- Type Parameters:
T- The element type.- Parameters:
values- The values to add to the set.- Returns:
- A modifiable LinkedHashSet containing the specified values.
-
sete
Returns an empty immutable set.This is a convenience method that returns
Collections.emptySet().Example:
Set<String>
empty = CollectionUtils.sete ();// Returns Collections.emptySet() - Type Parameters:
T- The element type.- Returns:
- An empty immutable set.
- See Also:
-
setb
Convenience factory for aSets.- Type Parameters:
E- The element type.- Parameters:
type- The element type.- Returns:
- A new set builder.
-
setOf
Convenience method for creating aLinkedHashSet.- Type Parameters:
E- The element type.- Parameters:
elementType- The element type.values- The values to initialize the set with.- Returns:
- A new modifiable set.
-
shorts
Shortcut for creating a short array.Accepts int values and converts them to shorts, eliminating the need for explicit short casts.
Example:
short []myArray =shorts (1, 2, 3);- Parameters:
value- The int values to convert to shorts. Values outside the short range (-32768 to 32767) will be truncated.- Returns:
- A new short array.
-
sortedList
Convenience method for creating anArrayListand 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.
-
sortedList
Convenience method for creating anArrayListand 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 anArrayListand sorting it.- Type Parameters:
E- The element type.- Parameters:
values- The values to initialize the list with.- Returns:
- A new modifiable list.
-
sortedMap
Convenience method for creating aTreeMap.- Type Parameters:
K- The key type.V- The value type.- Returns:
- A new modifiable set.
-
sortedSet
Convenience method for creating aTreeSet.Note:
null values in the varargs array are automatically filtered out becauseTreeSetdoes not supportnull elements.- Type Parameters:
E- The element type.- Parameters:
values- The values to initialize the set with.
Can containnull values (they will be filtered out).- Returns:
- A new modifiable set.
-
stream
Returns a stream of the specified array.Gracefully handles
null arrays by returning an empty stream.Example:
String[]
array = {"a" ,"b" ,"c" };// Prints "a", "b", "c" stream (array ).forEach(System.out ::println);// Handles null gracefully - returns empty stream stream (null ).forEach(System.out ::println);// Prints nothing - Type Parameters:
T- The element type.- Parameters:
array- The array to stream. Can benull .- Returns:
- A stream of the array elements, or an empty stream if the array is
null .
-
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 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 set inCollections.unmodifiableSet(Set).- Type Parameters:
E- The element type.- Parameters:
value- The set to wrap.- Returns:
- The wrapped set.
-
toArray
Converts the specified collection to an array.Works on both object and primitive arrays.
- Type Parameters:
E- The element type.- Parameters:
c- The collection to convert to an array.elementType- The component type of the collection.- Returns:
- A new array.
-
toList
Creates anArrayListcopy from a collection.- Type Parameters:
E- The element type.- Parameters:
value- The collection to copy from.- Returns:
- A new modifiable list.
-
toList
Creates anArrayListcopy 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.
-
toList
Converts various collection-like objects to aList.This utility method enables testing of any collection-like object by converting it to a List that can be passed to methods such as TestUtils.assertList().
Supported Input Types:
- List: Returns the input unchanged
- Iterable: Any collection, set, queue, etc. (converted to List preserving order)
- Iterator: Converts iterator contents to List
- Enumeration: Converts enumeration contents to List
- Stream: Converts stream contents to List (stream is consumed)
- Map: Converts map entries to List of Map.Entry objects
- Array: Converts any array type (including primitive arrays) to List
Usage Examples:
// Test a Set Set<String>mySet = Set.of("a" ,"b" ,"c" ); assertList(toList(mySet ),"a" ,"b" ,"c" );// Test an array String[]myArray = {"x" ,"y" ,"z" }; assertList(toList(myArray ),"x" ,"y" ,"z" );// Test a primitive array int []numbers = {1, 2, 3}; assertList(toList(numbers ),"1" ,"2" ,"3" );// Test a Stream Stream<String>myStream = Stream.of("foo" ,"bar" ); assertList(toList(myStream ),"foo" ,"bar" );// Test a Map (converted to entries) Map<String,Integer>myMap = Map.of("a" , 1,"b" , 2); assertList(toList(myMap ),"a=1" ,"b=2" );// Test any Iterable collection Queue<String>myQueue = new LinkedList<>(List.of("first" ,"second" )); assertList(toList(myQueue ),"first" ,"second" );Integration with Testing:
This method is specifically designed to work with testing frameworks to provide a unified testing approach for all collection-like types. Instead of having separate assertion methods for arrays, sets, and other collections, you can convert them all to Lists and use standard list assertion methods.
- Parameters:
o- The object to convert to a List. Must not be null and must be a supported collection-like type.- Returns:
- A
Listcontaining the elements from the input object. - Throws:
IllegalArgumentException- if the input object cannot be converted to a List.- See Also:
-
toList
Converts the specified array to anArrayList - Type Parameters:
E- The element type.- Parameters:
array- The array to convert.elementType- The type of objects in the array. It must match the actual component type in the array.- Returns:
- A new
ArrayList
-
toObjectList
Recursively converts the specified array into a list of objects.- Parameters:
array- The array to convert.- Returns:
- A new
ArrayList
-
toSet
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.
-
toSet
Converts the specified array to aSet .The order of the entries in the set are the same as the array.
- Type Parameters:
T- The entry type of the array.- Parameters:
array- The array being wrapped in aSet interface.- Returns:
- The new set.
-
toSortedSet
Creates a newTreeSetfrom 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.
-
toSortedSet
Creates a newTreeSetfrom 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.
-
toSortedSet
Creates a newTreeSetcontaining a copy of the specified set.- Type Parameters:
T- The element type.- Parameters:
copyFrom- The set to copy from.- Returns:
- A new
TreeSet, ornull if the set wasnull .
-
toStream
Converts an array to a stream of objects.- Parameters:
array- The array to convert.- Returns:
- A new stream.
-
toStringArray
Converts the specified collection to an array of strings.Entries are converted to strings using
Object.toString().null values remainnull .- Parameters:
c- The collection to convert.- Returns:
- The collection as a string array.
-
traverse
Traverses all elements in the specified object and executes a consumer for it.- Type Parameters:
T- The element type.- Parameters:
o- The object to traverse.c- The consumer of the objects.
-
u
Creates an unmodifiable view of the specified list.This is a null-safe wrapper around
Collections.unmodifiableList(List).- Type Parameters:
T- The element type.- Parameters:
value- The list to make unmodifiable. Can be null.- Returns:
- An unmodifiable view of the list, or null if the input was null.
-
u
Creates an unmodifiable view of the specified map.This is a null-safe wrapper around
Collections.unmodifiableMap(Map).- Type Parameters:
K- The key type.V- The value type.- Parameters:
value- The map to make unmodifiable. Can be null.- Returns:
- An unmodifiable view of the map, or null if the input was null.
-
u
Creates an unmodifiable view of the specified set.This is a null-safe wrapper around
Collections.unmodifiableSet(Set).- Type Parameters:
T- The element type.- Parameters:
value- The set to make unmodifiable. Can be null.- Returns:
- An unmodifiable view of the set, or null if the input was null.
-