Class CollectionMethods
java.lang.Object
org.klojang.util.CollectionMethods
Methods extending the Java Collection framework.
-
Method Summary
Modifier and TypeMethodDescriptionstatic List<?> Converts the specified value to aList.static <T,U> List <U> collectionToList(Collection<? extends T> src, Function<? super T, ? extends U> converter) Shortcut method.static <K,V> Map <K, V> collectionToMap(Collection<V> src, Function<? super V, ? extends K> keyExtractor) Shortcut method.static <T,U> Set <U> collectionToSet(Collection<? extends T> src, Function<? super T, ? extends U> converter) Shortcut method.static <K0,V0, K1, V1>
Map<K1, V1> deepFreeze(Map<K0, V0> src, Function<Map.Entry<K0, V0>, Map.Entry<K1, V1>> entryConverter) Returns an unmodifiableMapwhere both keys and values of the inputMaphave been converted using the specifiedFunction.static <T> TfindFirst(Collection<T> collection, Predicate<? super T> test) Shortcut method.freeze(List<? extends T> src, org.klojang.check.fallible.FallibleFunction<? super T, ? extends U, E> converter) Returns an unmodifiableListcontaining the values that result from applying the specified function to the source list's elements.static <K,V0, V1> Map <K, V1> freeze(Map<K, V0> src, BiFunction<? super K, ? super V0, ? extends V1> valueConverter) Returns an unmodifiableMapwhere the values of the inputMaphave been converted using the specifiedBiFunction.static <K,V0, V1> Map <K, V1> Returns an unmodifiableMapwhere the values of the inputMaphave been converted using the specifiedFunction.freeze(Set<? extends T> src, org.klojang.check.fallible.FallibleFunction<? super T, ? extends U, E> converter) Returns an unmodifiableSetcontaining the values that result from applying the specified function to the source set's elements.static <T> Stringimplode(Collection<T> collection) PHP-style implode method, concatenating the collection elements using ", " (comma-space) as separator.static <T> Stringimplode(Collection<T> collection, int limit) PHP-style implode method, concatenating at mostlimitcollection elements using ", " (comma-space) as separator.static <T> Stringimplode(Collection<T> collection, String separator) PHP-style implode method, concatenating the collection elements with the specified separator.static <T> Stringimplode(Collection<T> collection, String separator, int limit) PHP-style implode method, concatenating at mostlimitcollection elements using the specified separator.static <T> Stringimplode(Collection<T> collection, Function<T, String> stringifier) PHP-style implode method, concatenating at mostlimitcollection elements using ", " (comma-space) as separator.static <T> Stringimplode(Collection<T> collection, Function<T, String> stringifier, String separator) PHP-style implode method, concatenating at mostlimitcollection elements using ", " (comma-space) as separator.static <T> Stringimplode(Collection<T> collection, Function<T, String> stringifier, String separator, int from, int to) PHP-style implode method.static <E> List<E> initializedList(int size, E initVal) Returns a fixed-size, mutableListwith all elements initialized to the specified value.static <E> List<E> initializedList(int size, Supplier<E> initValSupplier) Returns a fixed-size, mutableListwith all elements initialized to values provided by aSupplier.static <K,V> Map <K, V> initializedMap(Object... kvPairs) Returns aHashMapinitialized with the specified key-value pairs.static booleanisNullRepellent(Collection<?> c) Returnstrueif the provided collection is a null-repellent collection.static booleanisNullRepellent(Map<?, ?> m) Returnstrueif the provided map is a null-repellent map.static <E> List<E> newArrayList(int capacity, E... initialValues) Returns a newArrayListinitialized with the specified values.static <K,V> Map <K, V> newHashMap(int size, Class<K> keyType, Class<V> valueType, Object... kvPairs) Returns aHashMapinitialized with the specified key-value pairs.saturatedEnumMap(Class<K> enumClass, V... values) Returns anEnumMapcontaining all enum constants.static <T> List<T> Returns a sublist of the provided list starting with elementfromand containing at mostlengthelements.static <K,V> Map <V, K> Returns a newMapwhere keys and values of the input map have traded places.static <K,V> Map <V, K> Returns a newMapwhere keys and values of the input map have traded places.static <K,V> Map <V, K> swapAndFreeze(Map<K, V> map) Returns an unmodifiableMapwhere keys and values of the input map have traded places.
-
Method Details
-
asList
Converts the specified value to a
List.- if the value already is a
List, it is returned as-is. - if the value is a
Collection, it is converted to anArrayListusing the standard Collections Framework conversion mechanism — by passing it to the constructor ofArrayList - if the value is an instance of
Object[], it is converted usingArrays.asList - if the value is an array of a primitive type, it is converted using
ArrayMethods.asList - in any other case (which includes the value being
null), the value is converted usingCollections.singletonList
In other words, this method takes the shortest route to turn the value into a
Listand there is no guarantee about what type ofListis returned. The returned list will always contain at least one element, but it may benull.- Parameters:
value- the value to convert- Returns:
- the value converted to a
List - See Also:
- if the value already is a
-
initializedList
Returns a fixed-size, mutableListwith all elements initialized to the specified value. The initialization value must not benull.- Type Parameters:
E- the type of the elements- Parameters:
size- the desired size of theListinitVal- the initial value of the list elements (must not benull)- Returns:
- a fixed-size, mutable, initialized
List
-
initializedList
Returns a fixed-size, mutableListwith all elements initialized to values provided by aSupplier.- Type Parameters:
E- the type of the elements- Parameters:
size- the desired size of theListinitValSupplier- the supplier of the initial values- Returns:
- a fixed-size, mutable, initialized
List
-
newArrayList
Returns a newArrayListinitialized with the specified values. The values are allowed to benull. The initial capacity will always be at least the length of theinitialValuesarray, whatever the value of thecapacityargument.- Type Parameters:
E- the type of the list elements- Parameters:
capacity- the initial capacity of the listinitialValues- the values to add to the list- Returns:
- a new
ArrayListinitialized with the specified values.
-
initializedMap
Returns aHashMapinitialized with the specified key-value pairs. Both keys and values are allowed to benull. The map will be tightly sized to the number of key-value pairs.- Type Parameters:
K- the type of the keysV- the type of the values- Parameters:
kvPairs- an array alternating between keys and values- Returns:
- a
HashMapinitialized with the specified key-value pairs
-
newHashMap
public static <K,V> Map<K,V> newHashMap(int size, Class<K> keyType, Class<V> valueType, Object... kvPairs) Returns aHashMapinitialized with the specified key-value pairs. Both keys and values are allowed to benull. Keys will be checked for uniqueness. Keys and values will be type-checked againstkeyTypeandvalueType, respectively.- Type Parameters:
K- the type of the keysV- the type of the values- Parameters:
size- the expected number of map entries. No rehashing will take place until that number is reached. If you specify a number less than the number of key-value pairs (half the length of the varargs array), it will be taken as a multiplier. For example, 2 would mean that you expect the map to grow to about twice the specified number of key-value pairs.keyType- the class of the keysvalueType- the class of the valueskvPairs- an array alternating between keys and values- Returns:
- a
HashMapinitialized with the specified key-value pairs
-
saturatedEnumMap
public static <K extends Enum<K>, V> EnumMap<K,V> saturatedEnumMap(Class<K> enumClass, V... values) throws IllegalArgumentException Returns anEnumMapcontaining all enum constants. The number of values must exactly equal the number of enum constants, and they are assigned according to ordinal number. This method throws anIllegalArgumentExceptionif the number of values is not exactly equal to the number of constants in the enum class, or if any of the values isnull.- Type Parameters:
K- the key typeV- the value type- Parameters:
enumClass- the enum classvalues- the values to associate with the enum constants- Returns:
- a fully-occupied
EnumMapwith no null-values - Throws:
IllegalArgumentException- ifenumClassorValuesis null, or if any of the provided values is null, or is the number of values is not exactly equals to the number of enum constants
-
sublist
Returns a sublist of the provided list starting with elementfromand containing at mostlengthelements. The returned list is backed by the original list, so changing its elements will affect the original list as well. Ifoffsetis negative, it is taken relative to the end of the list. Iflengthis negative, the sublist is taken in the opposite direction — that is, the element atoffsetnow becomes the last element of the sublist.- Type Parameters:
T- the type of the elements- Parameters:
list- theListto extract a sublist fromoffset- the start index if the sublist (however, see above)length- the length of the sublist- Returns:
- a sublist of the provided list
-
swap
Returns a newMapwhere keys and values of the input map have traded places. The specifiedMapmust not contain duplicate values. AnIllegalArgumentExceptionis thrown if it does.- Type Parameters:
K- the type of the keys in the original map, and of the values in the returned mapV- the type of the values in the original map, and of the keys in the returned map- Parameters:
map- the source map- Returns:
- a new
Mapwhere keys and values are swapped
-
swap
public static <K,V> Map<V,K> swap(Map<K, V> map, Supplier<? extends Map<V, throws org.klojang.check.extra.DuplicateValueExceptionK>> mapFactory) Returns a newMapwhere keys and values of the input map have traded places. The specifiedMapmust not contain duplicate values. ADuplicateValueExceptionis thrown if it does.nullkeys and values are allowed, however.- Type Parameters:
K- the type of the keys in the original map, and of the values in the returned mapV- the type of the values in the original map, and of the keys in the returned map- Parameters:
map- the source mapmapFactory- a supplier of aMapinstance- Returns:
- a new
Mapwhere keys and values are swapped - Throws:
org.klojang.check.extra.DuplicateValueException- if the map contains duplicate values
-
swapAndFreeze
Returns an unmodifiableMapwhere keys and values of the input map have traded places. The specifiedMapmust not containnullkeys,nullvalues or duplicate values. AnIllegalArgumentExceptionis thrown if it does.- Type Parameters:
K- the type of the keys in the original map, and of the values in the returned mapV- the type of the values in the original map, and of the keys in the returned map- Parameters:
map- the source map- Returns:
- a new
Mapwhere keys and values are swapped
-
freeze
public static <K,V0, Map<K,V1> V1> freeze(Map<K, V0> src, Function<? super V0, ? extends V1> valueConverter) Returns an unmodifiableMapwhere the values of the inputMaphave been converted using the specifiedFunction. The specifiedMapmust not containnullkeys,nullvalues or duplicate values. AnIllegalArgumentExceptionis thrown if it does.- Type Parameters:
K- the type of the keys of the input and outputMapV0- the type of the values of the inputMapV1- the type of the values of the outputMap- Parameters:
src- the inputMapvalueConverter- aFunctionthat converts the values of the inputMap- Returns:
- an unmodifiable
Mapwhere the values of the inputMaphave been converted using the specifiedFunction
-
freeze
public static <K,V0, Map<K,V1> V1> freeze(Map<K, V0> src, BiFunction<? super K, ? super V0, ? extends V1> valueConverter) Returns an unmodifiableMapwhere the values of the inputMaphave been converted using the specifiedBiFunction. This method passes both the key and the value to the converter function so you can make the conversion key-dependent, or so you can mention the key if the conversion fails.- Type Parameters:
K- the type of the keys of the input and outputMapV0- the type of the values of the inputMapV1- the type of the values of the outputMap- Parameters:
src- the inputMapvalueConverter- aFunctionthat converts the values of the inputMap- Returns:
- an unmodifiable
Mapwhere the values of the inputMaphave been converted using the specifiedFunction
-
deepFreeze
public static <K0,V0, Map<K1,K1, V1> V1> deepFreeze(Map<K0, V0> src, Function<Map.Entry<K0, V0>, Map.Entry<K1, V1>> entryConverter) Returns an unmodifiableMapwhere both keys and values of the inputMaphave been converted using the specifiedFunction. The output map may be smaller than the input map if the conversion function does not generate unique keys.- Type Parameters:
K0- The type of the keys in the input mapV0- the type of the values in the input mapK1- the type of the keys in the output mapV1- the type of the values in the output map- Parameters:
src- the inputMapentryConverter- aFunctionthat produces a new entry from the original entry- Returns:
- an unmodifiable
Mapwhere the values of the inputMaphave been converted using the specifiedFunction
-
freeze
public static <T, U, E extends Throwable> List<U> freeze(List<? extends T> src, org.klojang.check.fallible.FallibleFunction<? super T, ? extends U, E> converter) throws E Returns an unmodifiableListcontaining the values that result from applying the specified function to the source list's elements. The conversion function is allowed to throw a checked exception.- Type Parameters:
T- the type of the elements in the source listU- the type of the elements in the returned listE- the type of exception thrown if the conversion fails- Parameters:
src- the source listconverter- the conversion function- Returns:
- an unmodifiable
Listcontaining the values that result from applying the specified function to the source list's elements - Throws:
E- the exception potentially being thrown from the conversion function
-
freeze
public static <T, U, E extends Throwable> Set<U> freeze(Set<? extends T> src, org.klojang.check.fallible.FallibleFunction<? super T, ? extends U, E> converter) throws E Returns an unmodifiableSetcontaining the values that result from applying the specified function to the source set's elements. The conversion function is allowed to throw a checked exception.- Type Parameters:
T- the type of the elements in the source setU- the type of the elements in the returned setE- the type of exception thrown if the conversion fails- Parameters:
src- the source setconverter- the conversion function- Returns:
- an unmodifiable
Setcontaining the values that result from applying the - Throws:
E- the exception potentially being thrown from the conversion function
-
collectionToList
public static <T,U> List<U> collectionToList(Collection<? extends T> src, Function<? super T, ? extends U> converter) Shortcut method. Returns an unmodifiable list using:src.stream().map(converter).collect(toUnmodifiableList());- Type Parameters:
T- the type of the elements in the source setU- the type of the elements in the returned list- Parameters:
src- the source listconverter- the conversion function- Returns:
- an unmodifiable
Listcontaining the values that result from applying the specified function to the source collection's elements
-
collectionToSet
public static <T,U> Set<U> collectionToSet(Collection<? extends T> src, Function<? super T, ? extends U> converter) Shortcut method. Returns an unmodifiable set using:src.stream().map(converter).collect(toUnmodifiableSet());- Type Parameters:
T- the type of the elements in the source setU- the type of the elements in the returned list- Parameters:
src- the source listconverter- the conversion function- Returns:
- an unmodifiable
Setcontaining the values that result from applying the specified function to the source collection's elements
-
collectionToMap
public static <K,V> Map<K,V> collectionToMap(Collection<V> src, Function<? super V, ? extends K> keyExtractor) Shortcut method. Returns an unmodifiable map from the specified collection using:src.stream().collect(toUnmodifiableMap(keyExtractor, Function.identity()))- Type Parameters:
K- the type of the keysV- the type of the values and the list elements- Parameters:
src- theListto convert.keyExtractor- the key-extraction function- Returns:
- an unmodifiable
Map
-
findFirst
Shortcut method. Returns the first element that passes the specified test, or elsenull. Shortcut forcollection.stream().filter(test).findFirst().orElse(null).- Type Parameters:
T- the type of the elements in the collection- Parameters:
collection- the collection to searchtest- the test- Returns:
- the first element that passes the specified test, or else
null
-
implode
PHP-style implode method, concatenating the collection elements using ", " (comma-space) as separator.- Type Parameters:
T- the type of the elements- Parameters:
collection- the collection to implode- Returns:
- a concatenation of the elements in the collection.
- See Also:
-
implode
PHP-style implode method, concatenating the collection elements with the specified separator.- Type Parameters:
T- the type of the elements- Parameters:
collection- the collection to implodeseparator- the string used to separate the elements- Returns:
- a concatenation of the elements in the collection.
- See Also:
-
implode
PHP-style implode method, concatenating at mostlimitcollection elements using ", " (comma-space) as separator.- Type Parameters:
T- the type of the elements- Parameters:
collection- the collection to implodelimit- the maximum number of elements to collect. Specify -1 for no maximum. Specifying a number greater than the length of the collection is OK. It will be clamped to the collection length.- Returns:
- a concatenation of the elements in the collection.
- See Also:
-
implode
PHP-style implode method, concatenating at mostlimitcollection elements using ", " (comma-space) as separator.- Type Parameters:
T- the type of the elements- Parameters:
collection- the collection to implodestringifier- aFunctionthat converts the collection elements to strings- Returns:
- a concatenation of the elements in the collection.
- See Also:
-
implode
public static <T> String implode(Collection<T> collection, Function<T, String> stringifier, String separator) PHP-style implode method, concatenating at mostlimitcollection elements using ", " (comma-space) as separator.- Type Parameters:
T- the type of the elements- Parameters:
collection- the collection to implodestringifier- aFunctionthat converts the collection elements to stringsseparator- the string used to separate the elements- Returns:
- a concatenation of the elements in the collection.
- See Also:
-
implode
PHP-style implode method, concatenating at mostlimitcollection elements using the specified separator.- Type Parameters:
T- the type of the elements- Parameters:
collection- the collection to implodeseparator- the string used to separate the elementslimit- the maximum number of elements to collect. Specify -1 for no maximum. Specifying a number greater than the length of the collection is OK. It will be clamped to the collection length.- Returns:
- a concatenation of the elements in the collection.
- See Also:
-
implode
public static <T> String implode(Collection<T> collection, Function<T, String> stringifier, String separator, int from, int to) PHP-style implode method.- Type Parameters:
T- the type of the elements- Parameters:
collection- the collection to implodestringifier- aFunctionthat converts the collection elements to stringsseparator- the string used to separate the elementsfrom- the index of the element to begin the concatenation with (inclusive)to- the index of the element to end the concatenation with (exclusive). The specified number will be clamped tocollection.size()(i.e. it's OK to specify a number greater thancollection.size()). You can specify -1 as a shorthand forcollection.size().- Returns:
- a concatenation of the elements in the collection.
- See Also:
-
isNullRepellent
Returnstrueif the provided collection is a null-repellent collection. Null-repellent collections are those obtained viaList.of(...)andSet.of(...). Note: if this method returnstrue, the collection is guaranteed to be null-repellent, but if this method returnsfalse, it does not mean that the collection is guaranteed to allownullvalues. It just means that the collection is not known to be null-repellent.- Parameters:
c- the collection to inspect- Returns:
trueif the provided collection is a null-repellent collection
-
isNullRepellent
Returnstrueif the provided map is a null-repellent map. Note: if this method returnstrue, the map is guaranteed to be null-repellent, but if this method returnsfalse, it does not mean that the map is guaranteed to allownullvalues. It just means that the map is not known to be null-repellent.- Parameters:
m- the collection to inspect- Returns:
trueif the provided collection is a null-repellent collection
-