Package de.aristaflow.adept2.util
Class CollectionTools
- java.lang.Object
-
- de.aristaflow.adept2.util.CollectionTools
-
public final class CollectionTools extends Object
Tool methods for collections
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCollectionTools.EnumerationIterator<T>An iterator wrapping anEnumerationin anIterator.static classCollectionTools.FilteringIterator<T>static classCollectionTools.IdentityMap<K,V>A map considering equalling keys and values based on object identity instead ofObject.equals(Object).static classCollectionTools.IteratorEnumeration<T>An enumeration wrapping one or several iterators (and enumerations), i.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> Set<T>castOrCreateSet(Collection<T> coll)Casts the designated collection toSetif appropriate or creates a newHashSethaving the contents of the designated collection.static <T> booleancontains(T object, Enumeration<? extends T> enumeration)Gets whether the designated enumeration contains the designated object.static <T> booleancontains(T object, Iterator<? extends T> iterator)Gets whether the designated iterator contains the designated object.static <T> ArrayList<T>createArrayList(int size)Creates a new array list of the designated initial size.static <E> Set<E>identitySet()Gets a new set equalling elements based on object identity instead ofObject.equals(Object).static <E> Set<E>identitySet(int initialCapacity)Gets a new set equalling elements based on object identity instead ofObject.equals(Object).static <K,V>
Map<V,Collection<K>>inverseMap(Map<? extends K,? extends V> sMap)Creates a new map containing the entries of the designated map but inverse.static <T> Collection<Collection<T>>permutate(Collection<T> toPerm, boolean duplicates)Gets all permutations of the designated collection.static <T> List<T>removeNullElements(Collection<? extends T> coll)Gets a list reflecting the designated collection but withoutnullelements.static <K,V>
Map<K,V>replaceWithEmptyOrCopy(Map<K,V> map)Replaces an empty map by the EMPTY_MAP constant if its size is zero, or copies its contents into a newHashMap.static <T> Collection<T>sortCollection(Collection<? extends T> unsorted, Comparator<? super T> comp)Creates a new sorted collection containing the contents of the designated collection whereas sorting is delegated to the designated comparator.protected static <S,T,C extends Collection<T>>
CtransformColl(Collection<? extends S> sColl, Function<Integer,C> collCreator, Function<S,T> transformator, Supplier<T> nullSupplier)Transforms each object of the designated collection using the designated transformator.static <S,T>
HashSet<T>transformColl(Collection<? extends S> sColl, Function<S,T> transformator)Transforms each object of the designated collection using the designated transformator.static <S,T>
HashSet<T>transformColl(Collection<? extends S> sColl, Function<S,T> transformator, Supplier<T> nullSupplier)Transforms each object of the designated collection using the designated transformator.static <S,TK,TV,R extends Map<TK,TV>>
RtransformCollToMap(Collection<S> sColl, Function<Integer,R> creator, Function<S,Pair<TK,TV>> entryTrans)Transforms each entry of the designated collection to a map entry by transforming each element to an intermediate pair using the designated transformator.static <S,TK,TV>
HashMap<TK,TV>transformCollToMap(Collection<S> sColl, Function<S,Pair<TK,TV>> entryTrans)Transforms each entry of the designated collection to a map entry by transforming each element to an intermediate pair using the designated transformator.static <S,T>
HashSet<T>transformIterable(Iterable<? extends S> sIter, Function<S,T> transformator)Transforms each object of the designatedIterableusing the designated transformator.static <S,T>
HashSet<T>transformIterable(Iterable<? extends S> sIter, Function<S,T> transformator, Supplier<T> nullSupplier)Transforms each object of the designatedIterableusing the designated transformator.protected static <S,T,C extends Collection<T>>
CtransformIterable(Iterable<? extends S> sIter, Supplier<C> collCreator, Function<S,T> transformator, Supplier<T> nullSupplier)Transforms each object of the designatedIterableusing the designated transformator.static <S,TK,TV>
HashMap<TK,TV>transformIterableToMap(Iterable<S> sIter, Function<S,Pair<TK,TV>> entryTrans)Transforms each element of the designatedIterableto a map entry by transforming each element to an intermediate pair using the designated transformator.static <S,TK,TV,R extends Map<TK,TV>>
RtransformIterableToMap(Iterable<S> sIter, Supplier<R> creator, Function<S,Pair<TK,TV>> entryTrans)Transforms each element of the designatedIterableto a map entry by transforming each element to an intermediate pair using the designated transformator.static <S,T>
ArrayList<T>transformList(Iterable<? extends S> sIter, Function<S,T> transformator)Transforms each object of the designatedIterableusing the designated transformator.static <S,T>
ArrayList<T>transformList(Iterable<? extends S> sIter, Function<S,T> transformator, Supplier<T> nullSupplier)Transforms each object of the designatedIterableusing the designated transformator.static <S,T>
ArrayList<T>transformList(Collection<? extends S> sColl, Function<S,T> transformator)Transforms each object of the designated collection using the designated transformator.static <S,T>
ArrayList<T>transformList(Collection<? extends S> sColl, Function<S,T> transformator, Supplier<T> nullSupplier)Transforms each object of the designated collection using the designated transformator.static <SK,TK,SV,TV,R extends Map<TK,TV>>
RtransformMap(Map<? extends SK,? extends SV> sMap, Function<Integer,R> creator, Function<SK,TK> keyTrans, Function<SV,TV> valueTrans)Transforms each key and each value object of the designated map using the designated transformators.static <SK,TK,SV,TV,R extends Map<TK,TV>>
RtransformMap(Map<? extends SK,? extends SV> sMap, Function<Integer,R> creator, Function<SK,TK> keyTrans, Function<SV,TV> valueTrans, Supplier<TK> keyNullSupplier, Supplier<TV> valueNullSupplier)Transforms each key and each value object of the designated map using the designated transformators.static <SK,TK,SV,TV>
HashMap<TK,TV>transformMap(Map<? extends SK,? extends SV> sMap, Function<SK,TK> keyTrans, Function<SV,TV> valueTrans)Transforms each key and each value object of the designated map using the designated transformators.static <SK,TK,SV,TV>
HashMap<TK,TV>transformMap(Map<? extends SK,? extends SV> sMap, Function<SK,TK> keyTrans, Function<SV,TV> valueTrans, Supplier<TK> keyNullSupplier, Supplier<TV> valueNullSupplier)Transforms each key and each value object of the designated map using the designated transformators.static <SK,SV,T>
List<T>transformMapToList(Map<SK,SV> sMap, Function<Map.Entry<SK,SV>,T> entryTrans)Transforms each entry of the designated map to a list of instances of a combining type using the designated transformator.
-
-
-
Method Detail
-
replaceWithEmptyOrCopy
public static <K,V> Map<K,V> replaceWithEmptyOrCopy(Map<K,V> map)
Replaces an empty map by the EMPTY_MAP constant if its size is zero, or copies its contents into a newHashMap.- Parameters:
map- The map to be either replaced by EMPTY_MAP or to be copied.- Returns:
- The static EMPTY_MAP object or a fresh copy of the map's contents.
-
removeNullElements
public static <T> List<T> removeNullElements(Collection<? extends T> coll)
Gets a list reflecting the designated collection but withoutnullelements. If the collection itself isnull,nullwill be returned.- Parameters:
coll- The collection for which to get a list but withoutnullelements.- Returns:
- A list reflecting the designated collection but without
nullelements (ornull).
-
createArrayList
public static <T> ArrayList<T> createArrayList(int size)
Creates a new array list of the designated initial size. If the size is ludicrously big, the default size will be used. This prevents problems with creating arrays which exceed the VM size for an array.- Parameters:
size- The initial size of the array list.- Returns:
- An array with the designated initial size or the default size in case the designated size is too big.
-
transformColl
public static <S,T> HashSet<T> transformColl(Collection<? extends S> sColl, Function<S,T> transformator)
Transforms each object of the designated collection using the designated transformator. If an element of the collection isnull, this will not be transformed butnullwill be used instead.- Parameters:
sColl- The collection of objects which to transform ornull.transformator- The transformator which to apply to each element. This must not benull.- Returns:
- A collection (set) with the transformed objects or
null. - See Also:
ObjectTools.transformNullSafe(Object, Function)
-
transformColl
public static <S,T> HashSet<T> transformColl(Collection<? extends S> sColl, Function<S,T> transformator, Supplier<T> nullSupplier)
Transforms each object of the designated collection using the designated transformator. If an element of the collection isnull, the designatednullSupplier(Supplier) will be called.- Parameters:
sColl- The collection of objects which to transform ornull.transformator- The transformator which to apply to each element. This must not benull.nullSupplier- The supplier providing the object to be used fornull. If this isnull,nullwill be used as replacing value.- Returns:
- A collection (set) with the transformed objects or
null. - See Also:
ObjectTools.transformNullSafe(Object, Function)
-
transformList
public static <S,T> ArrayList<T> transformList(Collection<? extends S> sColl, Function<S,T> transformator)
Transforms each object of the designated collection using the designated transformator. If an element of the collection isnull, this will not be transformed butnullwill be used instead.- Parameters:
sColl- The collection of objects which to transform ornull.transformator- The transformator which to apply to each element. This must not benull.- Returns:
- A list with the transformed objects or
null. - See Also:
ObjectTools.transformNullSafe(Object, Function)
-
transformList
public static <S,T> ArrayList<T> transformList(Collection<? extends S> sColl, Function<S,T> transformator, Supplier<T> nullSupplier)
Transforms each object of the designated collection using the designated transformator. If an element of the collection isnull, the designatednullSupplier(Supplier) will be called.- Parameters:
sColl- The collection of objects which to transform ornull.transformator- The transformator which to apply to each element. This must not benull.nullSupplier- The supplier providing the object to be used fornull. If this isnull,nullwill be used as replacing value.- Returns:
- A list with the transformed objects or
null. - See Also:
ObjectTools.transformNullSafe(Object, Function)
-
transformColl
protected static <S,T,C extends Collection<T>> C transformColl(Collection<? extends S> sColl, Function<Integer,C> collCreator, Function<S,T> transformator, Supplier<T> nullSupplier)
Transforms each object of the designated collection using the designated transformator. If an element of the collection isnull, the designatednullSupplier(Supplier) will be called. For creating the returned collection the designatedcollCreator(Function) will be used.- Parameters:
sColl- The collection of objects which to transform ornull.collCreator- The creator for the returned collection accepting the collection size. This must not benull.transformator- The transformator which to apply to each element. This must not benull.nullSupplier- The supplier providing the object to be used fornull. If this isnull,nullwill be used as replacing value.- Returns:
- A collection (set) with the transformed objects or
null. - See Also:
ObjectTools.transformNullSafe(Object, Function)
-
transformIterable
public static <S,T> HashSet<T> transformIterable(Iterable<? extends S> sIter, Function<S,T> transformator)
Transforms each object of the designatedIterableusing the designated transformator. If an element of theIterableisnull, this will not be transformed butnullwill be used instead.- Parameters:
sIter- TheIterableof objects which to transform ornull.transformator- The transformator which to apply to each element. This must not benull.- Returns:
- A collection (set) with the transformed objects or
null. - See Also:
ObjectTools.transformNullSafe(Object, Function)
-
transformIterable
public static <S,T> HashSet<T> transformIterable(Iterable<? extends S> sIter, Function<S,T> transformator, Supplier<T> nullSupplier)
Transforms each object of the designatedIterableusing the designated transformator. If an element of theIterableisnull, the designatednullSupplier(Supplier) will be called.- Parameters:
sIter- TheIterableof objects which to transform ornull.transformator- The transformator which to apply to each element. This must not benull.nullSupplier- The supplier providing the object to be used fornull. If this isnull,nullwill be used as replacing value.- Returns:
- A collection (set) with the transformed objects or
null. - See Also:
ObjectTools.transformNullSafe(Object, Function)
-
transformList
public static <S,T> ArrayList<T> transformList(Iterable<? extends S> sIter, Function<S,T> transformator)
Transforms each object of the designatedIterableusing the designated transformator. If an element of theIterableisnull, this will not be transformed butnullwill be used instead.- Parameters:
sIter- TheIterableof objects which to transform ornull.transformator- The transformator which to apply to each element. This must not benull.- Returns:
- A list with the transformed objects or
null. - See Also:
ObjectTools.transformNullSafe(Object, Function)
-
transformList
public static <S,T> ArrayList<T> transformList(Iterable<? extends S> sIter, Function<S,T> transformator, Supplier<T> nullSupplier)
Transforms each object of the designatedIterableusing the designated transformator. If an element of theIterableisnull, the designatednullSupplier(Supplier) will be called.- Parameters:
sIter- TheIterableof objects which to transform ornull.transformator- The transformator which to apply to each element. This must not benull.nullSupplier- The supplier providing the object to be used fornull. If this isnull,nullwill be used as replacing value.- Returns:
- A list with the transformed objects or
null. - See Also:
ObjectTools.transformNullSafe(Object, Function)
-
transformIterable
protected static <S,T,C extends Collection<T>> C transformIterable(Iterable<? extends S> sIter, Supplier<C> collCreator, Function<S,T> transformator, Supplier<T> nullSupplier)
Transforms each object of the designatedIterableusing the designated transformator. If an element of theIterableisnull, the designatednullSupplier(Supplier) will be called. For creating the returned collection the designatedcollCreator(Supplier) will be used.- Parameters:
sIter- TheIterableof objects which to transform ornull.collCreator- The creator for the returned collection. This must not benull.transformator- The transformator which to apply to each element. This must not benull.nullSupplier- The supplier providing the object to be used fornull. If this isnull,nullwill be used as replacing value.- Returns:
- A collection (set) with the transformed objects or
null. - See Also:
ObjectTools.transformNullSafe(Object, Function)
-
transformMap
public static <SK,TK,SV,TV> HashMap<TK,TV> transformMap(Map<? extends SK,? extends SV> sMap, Function<SK,TK> keyTrans, Function<SV,TV> valueTrans)
Transforms each key and each value object of the designated map using the designated transformators. If a key or a value element of the collection isnull, this will not be transformed butnullused instead. Keys and values will be transformed separately.- Parameters:
sMap- The map of objects which to transform ornull.keyTrans- The transformator which to apply to each key element. This must not benull.valueTrans- The transformator which to apply to each value element. This must not benull.- Returns:
- A map with the transformed key and value objects or
null. - See Also:
ObjectTools.transformNullSafe(Object, Function)
-
transformMap
public static <SK,TK,SV,TV> HashMap<TK,TV> transformMap(Map<? extends SK,? extends SV> sMap, Function<SK,TK> keyTrans, Function<SV,TV> valueTrans, Supplier<TK> keyNullSupplier, Supplier<TV> valueNullSupplier)
Transforms each key and each value object of the designated map using the designated transformators. If a key or a value element of the collection isnull, this will not be transformed butnullused instead. Keys and values will be transformed separately.- Parameters:
sMap- The map of objects which to transform ornull.keyTrans- The transformator which to apply to each key element. This must not benull.valueTrans- The transformator which to apply to each value element. This must not benull.keyNullSupplier- The supplier providing the object to be used fornull.valueNullSupplier- The supplier providing the object to be used fornull.- Returns:
- A map with the transformed key and value objects or
null. - See Also:
ObjectTools.transformNullSafe(Object, Function)
-
transformMap
public static <SK,TK,SV,TV,R extends Map<TK,TV>> R transformMap(Map<? extends SK,? extends SV> sMap, Function<Integer,R> creator, Function<SK,TK> keyTrans, Function<SV,TV> valueTrans)
Transforms each key and each value object of the designated map using the designated transformators. If a key or a value element of the collection isnull, this will not be transformed butnullused instead. Keys and values will be transformed separately. The returned map will be created by the designated creator.- Parameters:
sMap- The map of objects which to transform ornull.creator- The creator for the new map accepting the map size.keyTrans- The transformator which to apply to each key element. This must not benull.valueTrans- The transformator which to apply to each value element. This must not benull.- Returns:
- A map with the transformed key and value objects or
null. - See Also:
ObjectTools.transformNullSafe(Object, Function)
-
transformMap
public static <SK,TK,SV,TV,R extends Map<TK,TV>> R transformMap(Map<? extends SK,? extends SV> sMap, Function<Integer,R> creator, Function<SK,TK> keyTrans, Function<SV,TV> valueTrans, Supplier<TK> keyNullSupplier, Supplier<TV> valueNullSupplier)
Transforms each key and each value object of the designated map using the designated transformators. If a key or a value element of the collection isnull, the corresponding supplier will be called. Keys and values will be transformed separately. The returned map will be created by the designated creator.- Parameters:
sMap- The map of objects which to transform ornull.creator- The creator for the new map accepting the map size.keyTrans- The transformator which to apply to each key element. This must not benull.valueTrans- The transformator which to apply to each value element. This must not benull.keyNullSupplier- The supplier providing the object to be used fornull. If this isnull,nullwill be used as replacing key.valueNullSupplier- The supplier providing the object to be used fornull. If this isnull,nullwill be used as replacing value.- Returns:
- A map with the transformed key and value objects or
null. - See Also:
ObjectTools.transformNullSafe(Object, Function)
-
transformCollToMap
public static <S,TK,TV> HashMap<TK,TV> transformCollToMap(Collection<S> sColl, Function<S,Pair<TK,TV>> entryTrans)
Transforms each entry of the designated collection to a map entry by transforming each element to an intermediate pair using the designated transformator. This transformator has to consider an entry beingnull. If it returnsnull, the corresponding element will be ignored. If it returns a pair containingnull, this will be taken over. Make sure that all pairs have a unique first element!- Parameters:
sColl- The collection of objects which to transform ornull.entryTrans- The transformator which to apply to each collection element. This must not benull. It has to handle collection elements beingnull.- Returns:
- A map of entries being created by the designated transformator for every collection
element or
null.
-
transformCollToMap
public static <S,TK,TV,R extends Map<TK,TV>> R transformCollToMap(Collection<S> sColl, Function<Integer,R> creator, Function<S,Pair<TK,TV>> entryTrans)
Transforms each entry of the designated collection to a map entry by transforming each element to an intermediate pair using the designated transformator. This transformator has to consider an entry beingnull. If it returnsnull, the corresponding element will be ignored. If it returns a pair containingnull, this will be taken over. Make sure that all pairs have a unique first element!
The returned map will be created by the designated creator.- Parameters:
sColl- The collection of objects which to transform ornull.creator- The creator for the new map accepting the map size.entryTrans- The transformator which to apply to each collection element. This must not benull. It has to handle collection elements beingnull.- Returns:
- A map of entries being created by the designated transformator for every collection
element or
null.
-
transformIterableToMap
public static <S,TK,TV> HashMap<TK,TV> transformIterableToMap(Iterable<S> sIter, Function<S,Pair<TK,TV>> entryTrans)
Transforms each element of the designatedIterableto a map entry by transforming each element to an intermediate pair using the designated transformator. This transformator has to consider an entry beingnull. If it returnsnull, the corresponding element will be ignored. If it returns a pair containingnull, this will be taken over. Make sure that all pairs have a unique first element!- Parameters:
sIter- TheIterableof objects which to transform ornull.entryTrans- The transformator which to apply to eachIterableelement. This must not benull. It has to handleIterableelements beingnull.- Returns:
- A map of entries being created by the designated transformator for every
Iterableelement ornull.
-
transformIterableToMap
public static <S,TK,TV,R extends Map<TK,TV>> R transformIterableToMap(Iterable<S> sIter, Supplier<R> creator, Function<S,Pair<TK,TV>> entryTrans)
Transforms each element of the designatedIterableto a map entry by transforming each element to an intermediate pair using the designated transformator. This transformator has to consider an entry beingnull. If it returnsnull, the corresponding element will be ignored. If it returns a pair containingnull, this will be taken over. Make sure that all pairs have a unique first element!
The returned map will be created by the designated creator.- Parameters:
sIter- TheIterableof objects which to transform ornull.creator- The creator for the new map.entryTrans- The transformator which to apply to each element of theIterable. This must not benull. It has to handleIterableelements beingnull.- Returns:
- A map of entries being created by the designated transformator for every
Iterableelement ornull.
-
transformMapToList
public static <SK,SV,T> List<T> transformMapToList(Map<SK,SV> sMap, Function<Map.Entry<SK,SV>,T> entryTrans)
Transforms each entry of the designated map to a list of instances of a combining type using the designated transformator. This transformator has to consider a key or a value element of the collection beingnull.- Parameters:
sMap- The map of objects which to transform ornull.entryTrans- The transformator which to apply to each map entry element. This must not benull. It has to handle keys or values beingnull.- Returns:
- A list of instances created by the designated transformator for every map entry or
null.
-
inverseMap
public static <K,V> Map<V,Collection<K>> inverseMap(Map<? extends K,? extends V> sMap)
Creates a new map containing the entries of the designated map but inverse. Since different keys in the designated map may have the same value, the inverse map has several values per key.- Type Parameters:
K- The type of the keys which will become the values.V- The type of the values which will become the keys.- Parameters:
sMap- The map for which to get a reverse mapping. This may benull.- Returns:
- A new map containing the entries of the designated map but inverse.
-
sortCollection
public static <T> Collection<T> sortCollection(Collection<? extends T> unsorted, Comparator<? super T> comp)
Creates a new sorted collection containing the contents of the designated collection whereas sorting is delegated to the designated comparator.
Note that elements that are considered equal by the designated comparator will only be once in the returned collection!- Parameters:
unsorted- The collection containing the elements unsorted. This must not benull.comp- The comparator for sorting the elements. This must not benull.- Returns:
- A sorted collection containing the designated elements (except for elements considered equal by the designated comparator).
-
contains
public static <T> boolean contains(T object, Enumeration<? extends T> enumeration)Gets whether the designated enumeration contains the designated object. This uses#equals(Object).- Type Parameters:
T- The type of object which to (not) find in the designated enumeration.- Parameters:
object- The object which to check for whether it is in the designated enumeration.enumeration- The enumeration which to check for the presence of the designated object.- Returns:
- Whether the designated enumeration contains the designated object.
-
contains
public static <T> boolean contains(T object, Iterator<? extends T> iterator)Gets whether the designated iterator contains the designated object. This uses#equals(Object).- Type Parameters:
T- The type of object which to (not) find in the designated iterator.- Parameters:
object- The object which to check for whether it is in the designated iterator.iterator- The iterator which to check for the presence of the designated object.- Returns:
- Whether the designated iterator contains the designated object.
-
castOrCreateSet
public static <T> Set<T> castOrCreateSet(Collection<T> coll)
Casts the designated collection toSetif appropriate or creates a newHashSethaving the contents of the designated collection.- Type Parameters:
T- The type of the elements of the collection/the set.- Parameters:
coll- The collection which to cast or transform toSet.- Returns:
- The designated collection cast as
Setor a newSethaving the same content.
-
identitySet
public static <E> Set<E> identitySet()
Gets a new set equalling elements based on object identity instead ofObject.equals(Object).- Type Parameters:
E- The type of the elements of the set.- Returns:
- A new set equalling elements based on object identity instead of
Object.equals(Object).
-
identitySet
public static <E> Set<E> identitySet(int initialCapacity)
Gets a new set equalling elements based on object identity instead ofObject.equals(Object).- Type Parameters:
E- The type of the elements of the set.- Parameters:
initialCapacity- The initial capacity of the created set.- Returns:
- A new set equalling elements based on object identity instead of
Object.equals(Object).
-
permutate
public static <T> Collection<Collection<T>> permutate(Collection<T> toPerm, boolean duplicates)
Gets all permutations of the designated collection. Duplicates may be allowed or not, a duplicate is the "same" (with respect to the elements of the collection) collection as result of different permutations. This happens in case the designated permutation contains equal elements.- Type Parameters:
T- The type of elements of the collection.- Parameters:
toPerm- The elements which to permutate.duplicates- Whether the resulting (outer) collection should contain collections with the same element (order) or not. Duplicates occur in case the designated collection contains equal elements.- Returns:
- All permutations of the designated collection, optionally containing duplicate collections.
-
-