Package jetbrains.buildServer.util
Class CollectionsUtil
- java.lang.Object
-
- jetbrains.buildServer.util.CollectionsUtil
-
public class CollectionsUtil extends java.lang.Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
CollectionsUtil.EnumerationIteratorAdaptor<T>
static class
CollectionsUtil.FilteringEnumeration<T>
static class
CollectionsUtil.FilteringIterable<T>
static class
CollectionsUtil.FilteringIterator<T>
-
Field Summary
Fields Modifier and Type Field Description static Filter
ACCEPT_ALL
static Converter
SAME
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static <T> Filter<T>
acceptAllFilter()
Type safe accept all filterstatic <T> boolean
addIfNotNull(java.util.Collection<T> collection, T object)
Adds an element to the collection unless the element is null.static <T,Q>
java.util.List<T>applyOrder(Q[] sortedIds, java.util.List<T> allElements, java.util.Map<Q,T> idToElementMap)
Accepts elements and reorders them according to provided partial order of the elements.static <T> java.util.Map<T,T>
asMap(T... args)
Creates map from a given even number of parametersstatic <T> java.lang.String
asString(java.util.Collection<T> collection, int maxElems)
static <T> int
binarySearch(java.util.List<? extends T> list, com.intellij.openapi.util.Condition<? super T> condition)
Returns the index of the first element for which condition is true.
It is supposed for the list that some index K exists such as:
1.static <SourceType>
java.util.List<SourceType>collectUniqueObjectList(java.util.List<SourceType> list)
static <T> boolean
contains(java.lang.Iterable<T> iterable, Filter<? super T> filter)
Checks whether giveniterable
has at least one element satisfying thefilter
static <Result,Source>
java.util.List<Result>convertAndFilterNulls(java.lang.Iterable<Source> source, Converter<Result,Source> converter)
Converts the collection (or any iterable) excluding thenull
s afterwards.static <ResultType,SourceType>
java.util.List<ResultType>convertCollection(java.lang.Iterable<? extends SourceType> source, Converter<ResultType,SourceType> converter)
Create collection of type ResultType from collection of elements of type SourceType.static <ResultType,SourceType>
java.util.List<ResultType>convertCollection(java.util.Collection<? extends SourceType> source, Converter<ResultType,SourceType> converter)
Create collection of type ResultType from collection of elements of type SourceType.static <K,V,K2,V2>
voidconvertMap(java.util.Map<? extends K,? extends V> source, java.util.Map<? super K2,? super V2> target, Converter<K2,K> keyConverter, Converter<V2,V> valueConverter)
Converts `Map<K, V>` into `Map<K2, V2>` coercing keys and values given respective convertersstatic <ResultType,SourceType>
java.util.Set<ResultType>convertSet(java.lang.Iterable<? extends SourceType> source, Converter<ResultType,SourceType> converter)
Create set of type ResultType from collection of elements of type SourceType.static <ResultType,SourceType>
java.util.Set<ResultType>convertSet(java.util.Collection<? extends SourceType> source, Converter<ResultType,SourceType> converter)
Create set of type ResultType from collection of elements of type SourceType.static <T extends java.lang.Enum<T>>
java.util.EnumSet<T>copyOf(java.lang.Class<T> type, java.util.Collection<T> from)
static <T> void
copyWithPadding(java.util.Collection<T> source, T[] target, T paddingValue)
Copies values from the source collection to the target array.static <K,V>
intcount(java.util.Map<K,V> map, Filter<K> keyFilter, Filter<V> valueFilter)
counts a number of entries in map that matches given filters (by &&)static <K,V>
java.util.Map<K,V>emptyHashMapIfNull(java.util.Map<K,V> map)
static <T> java.util.List<T>
ensureModifiable(java.util.List<T> items)
static <ResultType,SourceType>
java.util.List<ResultType>filterAndConvertCollection(java.lang.Iterable<? extends SourceType> source, Converter<ResultType,SourceType> converter, Filter<SourceType> filter)
Create collection of type ResultType from collection of elements of type SourceType.static <ResultType,ResultCollection extends java.util.Collection<ResultType>,SourceType>
ResultCollectionfilterAndConvertCollection(java.lang.Iterable<? extends SourceType> sources, ResultCollection collection, Converter<ResultType,SourceType> converter, Filter<SourceType> filter)
static <ResultType,SourceType>
java.util.List<ResultType>filterAndConvertCollection(java.util.Collection<? extends SourceType> source, Converter<ResultType,SourceType> converter, Filter<SourceType> filter)
Create collection of type ResultType from collection of elements of type SourceType.static <SourceType>
java.util.List<SourceType>filterCollection(java.lang.Iterable<? extends SourceType> source, Filter<SourceType> filter)
Elements are filtered basing on Filter interface.static <SourceType>
java.util.List<SourceType>filterCollection(java.util.Collection<? extends SourceType> source, Filter<SourceType> filter)
Elements are filtered basing on Filter interface.static <K,V>
java.util.Map<K,V>filterMap(java.util.Map<K,V> map, Filter<K> keyFilter, Filter<V> valueFilter)
Creates a copy of map filtering it by values filter, order is preservedstatic <K,V>
java.util.Map<K,V>filterMapByKeys(java.util.Map<K,V> map, Filter<K> filter)
Creates a copy of a map filtering it by keys filter, order is preservedstatic <K,V>
java.util.Map<K,V>filterMapByValues(java.util.Map<K,V> map, Filter<V> filter)
Creates a copy of map filtering it by values filter, order is preservedstatic <T> java.util.List<T>
filterNulls(java.util.Collection<T> collection)
Filters the collection, excluding allnull
s from it.static <T> T
findFirst(java.lang.Iterable<T> iterable, Filter<? super T> filter)
Searches a collection and returns first element which fulfills given filter functionstatic <T> T
findFirst(java.util.Iterator<T> iterator, Filter<? super T> filter)
Traverses with the iterator and returns first element which fulfills given filter function.static <T> java.util.Set<T>
flatten(java.util.Collection<com.intellij.openapi.util.Pair<T,T>> data)
creates set of pair's first and secondsstatic <T> java.util.Set<T>
flatten(java.util.Map<T,T> data)
creates set of pair's first and secondsstatic <K,V>
java.util.Map<K,java.util.List<V>>groupBy(java.util.Collection<V> collection, Converter<K,V> converter)
Groups values in collection according to some computed attribute.static <T extends java.lang.Comparable<T>>
voidinsertWithSorting(T o, java.util.List<T> list)
Insert an object into target collection into a position so collection remains sorted ascendingstatic <T> void
insertWithSorting(T o, java.util.List<T> list, java.util.Comparator<T> comp)
Insert an object into target collection into a position so collection remains sorted ascendingstatic <T> java.util.Set<T>
intersect(java.util.Collection<T> set1, java.util.Collection<T> set2)
Intersection: items present in both sets simultaneously.static <T> java.util.Collection<T>
join(java.util.Collection<T>... a)
Merges two collectionsstatic <T> java.util.Collection<T>
join(java.util.Collection<T> a, java.util.Collection<T> b)
Merges two collectionsstatic <T> java.util.Collection<T>
join(java.util.Collection<T> a, java.util.Collection<T> b, java.util.Collection<T> c)
Merges two collectionsstatic <T> java.util.Set<T>
minus(java.util.Collection<T> minuend, java.util.Collection<T> subtrahend)
Differs: items from the first collection excluding items from the second collection.static <T> java.util.SortedSet<T>
minus(java.util.SortedSet<T> minuend, java.util.Set<T> subtrahend)
Differs: items from the first (sorted) set excluding items from the second set.static <T> void
removeDuplicates(java.util.Collection<T> collection)
Removes the duplicates from the provided collection of elements.static <T> java.util.Collection<T>
removeDuplicatesPreservingOrder(java.util.Collection<T> collection)
The same asremoveDuplicates(java.util.Collection)
, but preserves the order of non duplicated elements.static <T,Coll extends java.lang.Iterable<T>>
CollremoveNulls(Coll collection)
static void
retainMatched(java.util.Collection<java.lang.String> stringCollection, java.util.regex.Pattern pattern)
static <T> java.lang.Iterable<T>
reversed(java.util.List<T> list)
Creates an iterable over a list looping in reverse direction.static <T> java.util.Set<T>
setOf(T... args)
Creates a set from given parametersstatic <T extends java.lang.Comparable<? super T>>
booleansortedListsIntersect(java.util.List<T> list1, java.util.List<T> list2)
Accepts two sorted lists and returns true if they have at least one common element, or false otherwisestatic <T> java.util.List<java.util.List<T>>
split(java.util.List<T> toSplit, int numParts)
Split one collection into a collection of smaller collectionsstatic <T> java.util.stream.Stream<T>
streamOf(java.util.Collection<T> collection)
Creates a stream of nullable collectionstatic java.lang.String
to_s(java.util.Collection c)
Returns a debug-oriented representation of the collection, when all elements are joined with "\r\n"static <T> T[]
toArray(java.util.Collection<T> collection, java.lang.Class<T> clazz)
Converts the given collection to an array.static <T> java.util.ArrayList<T>
toArrayList(java.util.Collection<T> items)
static java.lang.String[]
toStringArray(java.util.Collection<java.lang.String> strings)
Converts the given collection of string to an array.static <T> java.util.Iterator<T>
toTypedIterator(java.lang.Class<T> clazz, java.util.Iterator data)
Converts not-typed iterator to a typed iterator with lazy type checkstatic <T> java.util.Collection<T>
unite(java.util.Collection<? extends T>... col)
Deprecated.static <K,E>
java.util.Map<K,java.util.Set<E>>unmodifiable(java.util.Map<K,java.util.Set<E>> map)
-
-
-
Method Detail
-
acceptAllFilter
@NotNull public static <T> Filter<T> acceptAllFilter()
Type safe accept all filter- Type Parameters:
T
- type- Returns:
- filter accepting all values
- Since:
- 7.1
-
flatten
@NotNull public static <T> java.util.Set<T> flatten(@NotNull java.util.Collection<com.intellij.openapi.util.Pair<T,T>> data)
creates set of pair's first and seconds- Since:
- 8.1
-
flatten
@NotNull public static <T> java.util.Set<T> flatten(@NotNull java.util.Map<T,T> data)
creates set of pair's first and seconds- Since:
- 8.1
-
filterAndConvertCollection
public static <ResultType,SourceType> java.util.List<ResultType> filterAndConvertCollection(java.util.Collection<? extends SourceType> source, @NotNull Converter<ResultType,SourceType> converter, @NotNull Filter<SourceType> filter)
Create collection of type ResultType from collection of elements of type SourceType. Elements are filtered basing on Filter interface and conversion from SourceType to ResultType is performed using Converter interface.- Parameters:
source
- source collectionconverter
- converter from one element to anotherfilter
- filter which allows to skip some elements of the source collection- Returns:
- converted and filtered collection
-
filterAndConvertCollection
public static <ResultType,SourceType> java.util.List<ResultType> filterAndConvertCollection(java.lang.Iterable<? extends SourceType> source, @NotNull Converter<ResultType,SourceType> converter, @NotNull Filter<SourceType> filter)
Create collection of type ResultType from collection of elements of type SourceType. Elements are filtered basing on Filter interface and conversion from SourceType to ResultType is performed using Converter interface.- Parameters:
source
- source collectionconverter
- converter from one element to anotherfilter
- filter which allows to skip some elements of the source collection- Returns:
- converted and filtered collection
-
filterAndConvertCollection
public static <ResultType,ResultCollection extends java.util.Collection<ResultType>,SourceType> ResultCollection filterAndConvertCollection(java.lang.Iterable<? extends SourceType> sources, @NotNull ResultCollection collection, @NotNull Converter<ResultType,SourceType> converter, @NotNull Filter<SourceType> filter)
-
join
public static <T> java.util.Collection<T> join(java.util.Collection<T>... a)
Merges two collections- Type Parameters:
T
- type of collection elements- Parameters:
a
- collections to join- Returns:
- merged collection
- Since:
- 5.1
-
join
public static <T> java.util.Collection<T> join(@NotNull java.util.Collection<T> a, @NotNull java.util.Collection<T> b)
Merges two collections- Type Parameters:
T
- type of collection elements- Parameters:
a
- collectionb
- collection- Returns:
- merged collection
- Since:
- 5.1
-
join
public static <T> java.util.Collection<T> join(@NotNull java.util.Collection<T> a, @NotNull java.util.Collection<T> b, @NotNull java.util.Collection<T> c)
Merges two collections- Type Parameters:
T
- type of collection elements- Parameters:
a
- collectionb
- collectionc
- collection- Returns:
- merged collection
- Since:
- 5.1
-
filterCollection
public static <SourceType> java.util.List<SourceType> filterCollection(java.util.Collection<? extends SourceType> source, @NotNull Filter<SourceType> filter)
Elements are filtered basing on Filter interface.- Parameters:
source
- source collectionfilter
- filter which allows to skip some elements of the source collection- Returns:
- filtered collection
-
filterCollection
public static <SourceType> java.util.List<SourceType> filterCollection(java.lang.Iterable<? extends SourceType> source, @NotNull Filter<SourceType> filter)
Elements are filtered basing on Filter interface.- Parameters:
source
- source collectionfilter
- filter which allows to skip some elements of the source collection- Returns:
- filtered collection
-
convertCollection
public static <ResultType,SourceType> java.util.List<ResultType> convertCollection(java.util.Collection<? extends SourceType> source, Converter<ResultType,SourceType> converter)
Create collection of type ResultType from collection of elements of type SourceType. Elements conversion from SourceType to ResultType is performed using Converter interface.- Parameters:
source
- source collectionconverter
- converter from one element to another- Returns:
- converted and filtered collection
-
convertCollection
public static <ResultType,SourceType> java.util.List<ResultType> convertCollection(java.lang.Iterable<? extends SourceType> source, Converter<ResultType,SourceType> converter)
Create collection of type ResultType from collection of elements of type SourceType. Elements conversion from SourceType to ResultType is performed using Converter interface.- Parameters:
source
- source collectionconverter
- converter from one element to another- Returns:
- converted and filtered collection
-
convertSet
public static <ResultType,SourceType> java.util.Set<ResultType> convertSet(java.util.Collection<? extends SourceType> source, Converter<ResultType,SourceType> converter)
Create set of type ResultType from collection of elements of type SourceType. Elements conversion from SourceType to ResultType is performed using Converter interface.- Parameters:
source
- source collectionconverter
- converter from one element to another- Returns:
- converted and filtered collection
-
convertSet
public static <ResultType,SourceType> java.util.Set<ResultType> convertSet(java.lang.Iterable<? extends SourceType> source, Converter<ResultType,SourceType> converter)
Create set of type ResultType from collection of elements of type SourceType. Elements conversion from SourceType to ResultType is performed using Converter interface.- Parameters:
source
- source collectionconverter
- converter from one element to another- Returns:
- converted and filtered collection
-
collectUniqueObjectList
public static <SourceType> java.util.List<SourceType> collectUniqueObjectList(java.util.List<SourceType> list)
-
findFirst
@Nullable public static <T> T findFirst(java.lang.Iterable<T> iterable, Filter<? super T> filter)
Searches a collection and returns first element which fulfills given filter function- Parameters:
iterable
- collection of elementsfilter
- filter to satisfy- Returns:
- first element for which filter function returns true or null if no such element exists
- Since:
- 7.0
-
findFirst
@Nullable public static <T> T findFirst(@NotNull java.util.Iterator<T> iterator, @NotNull Filter<? super T> filter)
Traverses with the iterator and returns first element which fulfills given filter function. The iterator state will be changed after this method use!- Parameters:
iterator
- iteratorfilter
- filter to satisfy- Returns:
- first element for which filter function returns true or null if no such element exists
- Since:
- 2021.2
-
contains
public static <T> boolean contains(java.lang.Iterable<T> iterable, Filter<? super T> filter)
Checks whether giveniterable
has at least one element satisfying thefilter
- Parameters:
iterable
- collection to check- Returns:
- true if iterable has elements satisfying the filter, else otherwise
- Since:
- 8.1
-
insertWithSorting
public static <T extends java.lang.Comparable<T>> void insertWithSorting(@NotNull T o, @NotNull java.util.List<T> list)
Insert an object into target collection into a position so collection remains sorted ascending- Parameters:
o
- object to insertlist
- list of objects which should be sorted. All items should be added to the list using this function
-
insertWithSorting
public static <T> void insertWithSorting(@NotNull T o, @NotNull java.util.List<T> list, @NotNull java.util.Comparator<T> comp)
Insert an object into target collection into a position so collection remains sorted ascending- Parameters:
o
- object to insertlist
- list of objects which should be sorted. All items should be added to the list using this function and using same comparatorcomp
- comparator for the objects in the list
-
to_s
@NotNull public static java.lang.String to_s(@NotNull java.util.Collection c)
Returns a debug-oriented representation of the collection, when all elements are joined with "\r\n"- Parameters:
c
- collection to process- Returns:
- see above
-
split
@NotNull public static <T> java.util.List<java.util.List<T>> split(@NotNull java.util.List<T> toSplit, int numParts)
Split one collection into a collection of smaller collections- Type Parameters:
T
- collection items' base type- Parameters:
toSplit
- collection to splitnumParts
- number of parts to have- Returns:
- collection of small collections
-
retainMatched
public static void retainMatched(@Nullable java.util.Collection<java.lang.String> stringCollection, @NotNull java.util.regex.Pattern pattern)
-
minus
@NotNull public static <T> java.util.Set<T> minus(java.util.Collection<T> minuend, @NotNull java.util.Collection<T> subtrahend)
Differs: items from the first collection excluding items from the second collection.- Type Parameters:
T
- item type.- Parameters:
minuend
- first collection.subtrahend
- second collection.- Returns:
- the remainder of differs;
if the minuend is a kind of
SortedSet
the result will be an instance ofSortedSet
with the same comparator.
-
minus
@NotNull public static <T> java.util.SortedSet<T> minus(@NotNull java.util.SortedSet<T> minuend, @NotNull java.util.Set<T> subtrahend)
Differs: items from the first (sorted) set excluding items from the second set.- Type Parameters:
T
- item type.- Parameters:
minuend
- first set.subtrahend
- second set.- Returns:
- the remainder of differs; with the comparator from minuend.
- Since:
- 8.0
-
intersect
@NotNull public static <T> java.util.Set<T> intersect(@Nullable java.util.Collection<T> set1, @Nullable java.util.Collection<T> set2)
Intersection: items present in both sets simultaneously.- Type Parameters:
T
- item type.- Parameters:
set1
- first set.set2
- second set.- Returns:
- the intersection of both sets.
-
sortedListsIntersect
public static <T extends java.lang.Comparable<? super T>> boolean sortedListsIntersect(@NotNull java.util.List<T> list1, @NotNull java.util.List<T> list2)
Accepts two sorted lists and returns true if they have at least one common element, or false otherwise- Parameters:
list1
- - first listlist2
- - second list- Returns:
- see above
- Since:
- 2018.1.2
-
removeNulls
@NotNull public static <T,Coll extends java.lang.Iterable<T>> Coll removeNulls(@NotNull Coll collection)
-
filterNulls
@NotNull public static <T> java.util.List<T> filterNulls(@NotNull java.util.Collection<T> collection)
Filters the collection, excluding allnull
s from it.- Type Parameters:
T
- elements' type- Parameters:
collection
- the collection to filter- Returns:
- a filtered collection
- Since:
- 7.0
- See Also:
convertCollection(Iterable, Converter)
,convertAndFilterNulls(Iterable, Converter)
-
convertAndFilterNulls
@NotNull public static <Result,Source> java.util.List<Result> convertAndFilterNulls(@NotNull java.lang.Iterable<Source> source, @NotNull Converter<Result,Source> converter)
Converts the collection (or any iterable) excluding thenull
s afterwards.- Type Parameters:
Result
- result typeSource
- source type- Parameters:
source
- the source iterableconverter
- the converter- Returns:
- a converter collection without
null
s - Since:
- 7.1
- See Also:
convertCollection(Iterable, Converter)
-
unite
@NotNull @Deprecated public static <T> java.util.Collection<T> unite(@NotNull java.util.Collection<? extends T>... col)
Deprecated.- Parameters:
col
- collection(s)- Returns:
- Returns a new collection containing all the elements from the given collections
- Since:
- 7.0
-
applyOrder
public static <T,Q> java.util.List<T> applyOrder(@NotNull Q[] sortedIds, @NotNull java.util.List<T> allElements, @NotNull java.util.Map<Q,T> idToElementMap)
Accepts elements and reorders them according to provided partial order of the elements. Elements must be identifiable by string id. New order of elements is defined as the sorted array of identifiers. Note that array of identifiers size may not be the same as the size of the whole collection.- Parameters:
sortedIds
- new order of identifiersallElements
- elements to sortidToElementMap
- map from element id to element- Returns:
- reordered elements
-
asMap
@NotNull public static <T> java.util.Map<T,T> asMap(@NotNull T... args)
Creates map from a given even number of parameters- Type Parameters:
T
- type of parameters- Parameters:
args
- parameters as sequence of key, value- Returns:
- map
- Since:
- 7.0
-
setOf
@NotNull public static <T> java.util.Set<T> setOf(@NotNull T... args)
Creates a set from given parameters- Type Parameters:
T
- type of element- Parameters:
args
- elements of set- Returns:
- set
- Since:
- 7.0
-
removeDuplicates
public static <T> void removeDuplicates(@NotNull java.util.Collection<T> collection)
Removes the duplicates from the provided collection of elements. Notes:- If the
collection
is ordered (e.g.java.util.List
), the result order is not preserved. - The elements are required to support
hashCode
andequals
methods.
- Type Parameters:
T
- any type able to store inHashSet
- Parameters:
collection
- the collection to remove duplicates from
- If the
-
removeDuplicatesPreservingOrder
@NotNull public static <T> java.util.Collection<T> removeDuplicatesPreservingOrder(@NotNull java.util.Collection<T> collection)
The same asremoveDuplicates(java.util.Collection)
, but preserves the order of non duplicated elements. In some cases may return the samecollection
instance (but without duplicates).- Type Parameters:
T
- any type able to store inHashSet
- Parameters:
collection
- the collection to remove duplicates from- Returns:
- a filtered collection
- Since:
- 8.0
-
binarySearch
public static <T> int binarySearch(@NotNull java.util.List<? extends T> list, @NotNull com.intellij.openapi.util.Condition<? super T> condition)
Returns the index of the first element for which condition is true.
It is supposed for the list that some index K exists such as:
1. 0 <= K <= list size
2. condition is false for all indexes those are less than K
3. condition is true for all indexes those are equal or greater than K- Returns:
- K
- Since:
- 6.5.2
-
toTypedIterator
@NotNull public static <T> java.util.Iterator<T> toTypedIterator(@NotNull java.lang.Class<T> clazz, @NotNull java.util.Iterator data)
Converts not-typed iterator to a typed iterator with lazy type check- Type Parameters:
T
- type of elements- Parameters:
clazz
- class of itemsdata
- not-typed iterator- Returns:
- lazy wrapper for a given iterator of given type
- Since:
- 7.0
-
filterMapByKeys
@NotNull public static <K,V> java.util.Map<K,V> filterMapByKeys(@NotNull java.util.Map<K,V> map, @NotNull Filter<K> filter)
Creates a copy of a map filtering it by keys filter, order is preserved- Type Parameters:
K
- typeV
- type- Parameters:
map
- mapfilter
- keys filter- Returns:
- copy of map with subset of values
- Since:
- 7.1
-
filterMapByValues
@NotNull public static <K,V> java.util.Map<K,V> filterMapByValues(@NotNull java.util.Map<K,V> map, @NotNull Filter<V> filter)
Creates a copy of map filtering it by values filter, order is preserved- Type Parameters:
K
- typeV
- type- Parameters:
map
- mapfilter
- values filter- Returns:
- copy of map with subset of values
- Since:
- 7.1
-
filterMap
@NotNull public static <K,V> java.util.Map<K,V> filterMap(@NotNull java.util.Map<K,V> map, @NotNull Filter<K> keyFilter, @NotNull Filter<V> valueFilter)
Creates a copy of map filtering it by values filter, order is preserved- Type Parameters:
K
- typeV
- type- Parameters:
map
- mapkeyFilter
- keys filtervalueFilter
- values filter- Returns:
- copy of map with subset of values
- Since:
- 7.1
-
count
public static <K,V> int count(@NotNull java.util.Map<K,V> map, @NotNull Filter<K> keyFilter, @NotNull Filter<V> valueFilter)
counts a number of entries in map that matches given filters (by &&)- Type Parameters:
K
- typeV
- type- Parameters:
map
- mapkeyFilter
- keys filtervalueFilter
- values filter- Returns:
- number of (key,value) pairs matching both filters
- Since:
- 7.1
-
groupBy
@NotNull public static <K,V> java.util.Map<K,java.util.List<V>> groupBy(@NotNull java.util.Collection<V> collection, @NotNull Converter<K,V> converter)
Groups values in collection according to some computed attribute. Note: 'null' elements from collection will be stored in group with 'null' key- Type Parameters:
K
- grouping attributeV
- value to group- Parameters:
collection
- collection to groupconverter
- converter which computes attribute to group by- Returns:
- map where key is grouping attribute and value is list of values corresponding to this attribute
- Since:
- 8.0
-
convertMap
public static <K,V,K2,V2> void convertMap(@NotNull java.util.Map<? extends K,? extends V> source, @NotNull java.util.Map<? super K2,? super V2> target, @NotNull Converter<K2,K> keyConverter, @NotNull Converter<V2,V> valueConverter)
Converts `Map<K, V>` into `Map<K2, V2>` coercing keys and values given respective converters- Parameters:
source
- source-maptarget
- target-mapkeyConverter
- keys convertervalueConverter
- values converter
-
toArray
public static <T> T[] toArray(@NotNull java.util.Collection<T> collection, @NotNull java.lang.Class<T> clazz)
Converts the given collection to an array.- Type Parameters:
T
- items class.- Parameters:
collection
- the collection to convert.clazz
- items class.- Returns:
- the created array.
- Since:
- 9.0
-
toStringArray
public static java.lang.String[] toStringArray(@NotNull java.util.Collection<java.lang.String> strings)
Converts the given collection of string to an array.- Parameters:
strings
- the collection to convert.- Returns:
- created array of string.
- Since:
- 9.0
-
ensureModifiable
@NotNull public static <T> java.util.List<T> ensureModifiable(@NotNull java.util.List<T> items)
-
toArrayList
@NotNull public static <T> java.util.ArrayList<T> toArrayList(@NotNull java.util.Collection<T> items)
-
emptyHashMapIfNull
public static <K,V> java.util.Map<K,V> emptyHashMapIfNull(java.util.Map<K,V> map)
-
addIfNotNull
public static <T> boolean addIfNotNull(@NotNull java.util.Collection<T> collection, @Nullable T object)
Adds an element to the collection unless the element is null.
-
reversed
@NotNull public static <T> java.lang.Iterable<T> reversed(@NotNull java.util.List<T> list)
Creates an iterable over a list looping in reverse direction. Useful in foreach constructions.- Parameters:
list
- - list to be processed- Returns:
- an iterable over a list looping in reverse direction. Useful in foreach constructions
-
streamOf
@NotNull public static <T> java.util.stream.Stream<T> streamOf(@Nullable java.util.Collection<T> collection)
Creates a stream of nullable collection- Parameters:
collection
- nullable collection- Returns:
- empty stream if collection is null,
collection.stream()
otherwise
-
asString
@NotNull public static <T> java.lang.String asString(@NotNull java.util.Collection<T> collection, int maxElems)
-
copyOf
public static <T extends java.lang.Enum<T>> java.util.EnumSet<T> copyOf(@NotNull java.lang.Class<T> type, @NotNull java.util.Collection<T> from)
-
unmodifiable
@NotNull public static <K,E> java.util.Map<K,java.util.Set<E>> unmodifiable(@NotNull java.util.Map<K,java.util.Set<E>> map)
-
copyWithPadding
public static <T> void copyWithPadding(@NotNull java.util.Collection<T> source, @NotNull T[] target, @NotNull T paddingValue)
Copies values from the source collection to the target array. If target array is bigger than the source then the rest of the array is filled with the padding value.- Type Parameters:
T
-- Parameters:
source
- source collectiontarget
- target arraypaddingValue
- padding value- Since:
- 2023.11
-
-