Package com.nomagic.utils
Class CollectionUtils
java.lang.Object
com.nomagic.utils.CollectionUtils
Collection related utility methods.
- 
Constructor Summary
Constructors - 
Method Summary
Modifier and TypeMethodDescriptionstatic <T> voidaddIfNotNull(T element, Collection<T> collection) Adds the given element to the collection unless it is null.static <T> Collection<T>append(Collection<T> target, Collection<? extends T> source, boolean allowDuplication) Append source to target collection.static <T> Collection<T>append(Collection<T> target, Iterator<? extends T> sourceIterator, boolean allowDuplication) Append source to target collection.static <T> Collection<T>append(Collection<T> target, Iterator<? extends T> sourceIterator, boolean allowDuplication, boolean allowNulls) Append source to target collection.static <T> Collection<T>append(Collection<T> target, T[] source, boolean allowDuplication) Append source array to target collection.static booleanChecks if given iterator contains given object.static booleancontainsAny(Collection location, Collection what) Returns true if location contains any element from whatstatic <S,T, C extends Collection<T>> 
Cconvert(Collection<S> source, C target, Converter<? super S, ? super T> converter) Convert one (source) collection into another (target) collection.static <T> List<T>copyAndAdd(List<T> list, Collection<T> toAdd) Creates copy of the given list, or new list if null is provided.static <T> List<T>copyAndAdd(List<T> list, T element) Creates copy of the given list, or new list if null is provided.static <C extends Collection<E>,E> 
CcreateCollection(Class<C> collection, Iterator<E> it) Creates given type collection and fills it with objects from given iteratorstatic <T> Collection<T>createFromStringRepresentation(String string, String delimiter, Converter<String, T> converter) Create collection from string.static <T> StringcreateStringRepresentation(Collection<T> collection, String delimiter, Converter<T, String> converter) Create string representation of given collection.static booleanequals(Collection<?> collection1, Collection<?> collection2) Checks if both collections are equal - size the same and elements by order are the samestatic <T> List<T>filterByClassType(Collection<?> input, Class<T> type) Filters and casts collection from one type to anotherstatic <T> Stream<T>filterByClassType(Stream<?> stream, Class<T> type) Filter and map according given class.static <T> Tfind(Collection<T> input, Predicate<T> predicate) Looks for object matching given predicate.static <T> TfindByClassType(Collection<?> input, Class<T> type) Looks for object of give class in the given collection.static voidleaveElements(List elements, int numberToLeave) Leaves a given number of first elements in the collection.static StringmapToString(Map<String, String> map) Serializes map to string.static <T> Collection<T>notDublicatedList(Collection<T> collection) Deprecated, for removal: This API element is subject to removal in a future version.static <T> Collection<T>notDuplicatedList(Collection<T> collection) Removes duplicates from given collection.static <T> voidsetFirstValue(Collection<T> collection, T value) Set first value of the given collection.static <T> voidsetSingleValue(Collection<T> collection, T value) Removes all values from a given collection and adds a new one.static <T> booleanstartsWith(Collection<T> prefixCollection, Collection<T> collection) Checks if collection starts with prefixCollection Example: collection is "A.B.C", prefixCollection is "A.B".stringToMap(String input) Deserialize map from string.static <T> Collection<T>toOptimizedCacheCollection(Collection<T> collection) Optimized for 1) minimal memory footprint, for the stored cache data to be as small as possible 2) contains() performance, while preserving iteration order and performancestatic <T> Collection<T>toOptimizedContainsCollection(Collection<T> collection) Wrap the specified collection into containable collection that ensures optimized {{Collection.contains()}} performance. 
- 
Constructor Details
- 
CollectionUtils
public CollectionUtils() 
 - 
 - 
Method Details
- 
convert
public static <S,T, C convertC extends Collection<T>> (Collection<S> source, C target, Converter<? super S, ? super T> converter) Convert one (source) collection into another (target) collection. Converts each source element and adds converted element to target collection.- Type Parameters:
 S- source collection element type.T- target collection element type.C- target collection type.- Parameters:
 source- source collection.target- target collection.converter- converts each element.- Returns:
 - target collection.
 
 - 
append
public static <T> Collection<T> append(Collection<T> target, Collection<? extends T> source, boolean allowDuplication) Append source to target collection.- Parameters:
 target- targetsource- sourceallowDuplication- do not check for already added element if true- Returns:
 - appended collection
 
 - 
append
public static <T> Collection<T> append(Collection<T> target, Iterator<? extends T> sourceIterator, boolean allowDuplication) Append source to target collection.- Parameters:
 target- targetsourceIterator- source iterator.allowDuplication- do not check for already added element if true- Returns:
 - appended collection
 
 - 
append
public static <T> Collection<T> append(Collection<T> target, Iterator<? extends T> sourceIterator, boolean allowDuplication, boolean allowNulls) Append source to target collection.- Parameters:
 target- targetsourceIterator- source iterator.allowDuplication- do not check for already added element if trueallowNulls- allows nulls- Returns:
 - appended collection
 
 - 
append
Append source array to target collection.- Parameters:
 target- targetsource- sourceallowDuplication- do not check for already added element if true- Returns:
 - appended vector
 
 - 
contains
Checks if given iterator contains given object.- Parameters:
 it- the iterator.o- the object.- Returns:
 - true, if it contains o.
 
 - 
containsAny
Returns true if location contains any element from what- Parameters:
 location- location collectionwhat- what collection.- Returns:
 - boolean.
 
 - 
createCollection
Creates given type collection and fills it with objects from given iterator- Parameters:
 collection- type collection.it- The given iterator.- Returns:
 - the result collection.
 
 - 
notDuplicatedList
Removes duplicates from given collection. After remove one element will be in collection only one time.- Parameters:
 collection- collection to operate with (makes sense when col are list (sets cant have identical elements)).- Returns:
 - collection.
 
 - 
notDublicatedList
@Deprecated(forRemoval=true) public static <T> Collection<T> notDublicatedList(Collection<T> collection) Deprecated, for removal: This API element is subject to removal in a future version.Removes duplicates from given collection. After remove one element will be in collection only one time.- Parameters:
 collection- collection to operate with. Makes sense when collection is list (set cant have identical elements).- Returns:
 - collection.
 
 - 
createStringRepresentation
public static <T> String createStringRepresentation(Collection<T> collection, String delimiter, Converter<T, String> converter) Create string representation of given collection.- Parameters:
 collection- collection.delimiter- values delimiter.converter- convert object to string.- Returns:
 - string representation of collection.
 
 - 
createFromStringRepresentation
public static <T> Collection<T> createFromStringRepresentation(String string, String delimiter, Converter<String, T> converter) Create collection from string.- Parameters:
 string- string representation.delimiter- values delimiter.converter- convert string to object.- Returns:
 - collection.
 
 - 
leaveElements
Leaves a given number of first elements in the collection.- Parameters:
 elements- elements to filter.numberToLeave- number of elements to leave.
 - 
mapToString
Serializes map to string.- Parameters:
 map- map to serialize.- Returns:
 - string representation of the map.
 
 - 
stringToMap
Deserialize map from string.- Parameters:
 input- input string.- Returns:
 - map.
 
 - 
filterByClassType
Filters and casts collection from one type to another- Type Parameters:
 T- type- Parameters:
 input- input collectiontype- desired output collection type- Returns:
 - new typed list
 
 - 
filterByClassType
Filter and map according given class.- Parameters:
 stream- stream to filer and maptype- desired output stream type- Returns:
 - filtered and mapped stream
 
 - 
findByClassType
Looks for object of give class in the given collection.- Type Parameters:
 T- type- Parameters:
 input- input collectiontype- desired output collection type- Returns:
 - found object
 
 - 
find
Looks for object matching given predicate.- Parameters:
 input- input collectionpredicate- predicate to test- Returns:
 - found object
 
 - 
setSingleValue
Removes all values from a given collection and adds a new one.- Parameters:
 collection- collectionvalue- a new value
 - 
setFirstValue
Set first value of the given collection. If collection is empty, adds a new value. If value is null, removes first value from collection.- Parameters:
 collection- collectionvalue- a new value
 - 
toOptimizedContainsCollection
Wrap the specified collection into containable collection that ensures optimized {{Collection.contains()}} performance. The provided collection should not change while the wrapped collection is used.- Parameters:
 collection- a collection to wrap into containable collection- Returns:
 - return wrapped collection.
 
 - 
toOptimizedCacheCollection
Optimized for 1) minimal memory footprint, for the stored cache data to be as small as possible 2) contains() performance, while preserving iteration order and performanceReturned collection in unmodifiable
 - 
equals
Checks if both collections are equal - size the same and elements by order are the same- Parameters:
 collection1- first collectioncollection2- second collection- Returns:
 - true if size the same and elements by order are the same
 
 - 
addIfNotNull
Adds the given element to the collection unless it is null.- Parameters:
 element- nullable element to addcollection- modifiable collection
 - 
copyAndAdd
Creates copy of the given list, or new list if null is provided. Then adds the given element and returns the copy.- Parameters:
 element- element to addlist- nullable list to create copy of- Returns:
 - copied and appended list
 
 - 
copyAndAdd
Creates copy of the given list, or new list if null is provided. Then adds the given collection at the end and returns the copy.- Parameters:
 toAdd- collection to addlist- nullable list to create copy of- Returns:
 - copied and appended list
 
 - 
startsWith
Checks if collection starts with prefixCollection Example: collection is "A.B.C", prefixCollection is "A.B". Result would be true.- Parameters:
 prefixCollection- collection that is used to check if other collections starts with itcollection- collection to check- Returns:
 - true if starts with prefixCollection, else false
 
 
 - 
 
notDuplicatedList(java.util.Collection)