Class CollectionUtils


  • @OpenApiAll
    public class CollectionUtils
    extends java.lang.Object
    Collection related utility methods.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static int HASH_SET_THRESHOLD  
    • Constructor Summary

      Constructors 
      Constructor Description
      CollectionUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static <T> java.util.Collection<T> append​(java.util.Collection<T> target, java.util.Collection<? extends T> source, boolean allowDuplication)
      Append source to target collection.
      static <T> java.util.Collection<T> append​(java.util.Collection<T> target, java.util.Iterator<? extends T> sourceIterator, boolean allowDuplication)
      Append source to target collection.
      static <T> java.util.Collection<T> append​(java.util.Collection<T> target, java.util.Iterator<? extends T> sourceIterator, boolean allowDuplication, boolean allowNulls)
      Append source to target collection.
      static <T> java.util.Collection<T> append​(java.util.Collection<T> target, T[] source, boolean allowDuplication)
      Append source array to target collection.
      static boolean contains​(java.util.Iterator it, java.lang.Object o)
      Checks if given iterator contains given object.
      static boolean containsAny​(java.util.Collection location, java.util.Collection what)
      Returns true if location contains any element from what
      static <S,​T,​C extends java.util.Collection<T>>
      C
      convert​(java.util.Collection<S> source, C target, Converter<? super S,​? super T> converter)
      Convert one (source) collection into another (target) collection.
      static <C extends java.util.Collection<E>,​E>
      C
      createCollection​(java.lang.Class<C> collection, java.util.Iterator<E> it)
      Creates given type collection and fills it with objects from given iterator
      static <T> java.util.Collection<T> createFromStringRepresentation​(java.lang.String string, java.lang.String delimiter, Converter<java.lang.String,​T> converter)
      Create collection from string.
      static <T> java.lang.String createStringRepresentation​(java.util.Collection<T> collection, java.lang.String delimiter, Converter<T,​java.lang.String> converter)
      Create string representation of given collection.
      static boolean equals​(java.util.Collection<?> collection1, java.util.Collection<?> collection2)
      Checks if both collections are equal - size the same and elements by order are the same
      static <T> java.util.List<T> filterByClassType​(java.util.Collection<?> input, java.lang.Class<T> type)
      Filters and casts collection from one type to another
      static void leaveElements​(java.util.List elements, int numberToLeave)
      Leaves a given number of first elements in the collection.
      static java.lang.String mapToString​(java.util.Map<java.lang.String,​java.lang.String> map)
      Serializes map to string.
      static <T> java.util.Collection<T> notDublicatedList​(java.util.Collection<T> collection)
      static <T> java.util.Collection<T> notDuplicatedList​(java.util.Collection<T> collection)
      Removes duplicates from given collection.
      static <T> void setFirstValue​(java.util.Collection<T> collection, T value)
      Set first value of the given collection.
      static <T> void setSingleValue​(java.util.Collection<T> collection, T value)
      Removes all values from a given collection and adds a new one.
      static java.util.Map<java.lang.String,​java.lang.String> stringToMap​(java.lang.String input)
      Deserialize map from string.
      static <T> java.util.Collection<T> toOptimizedContainsCollection​(java.util.Collection<T> collection)
      Wrap the specified collection into containable collection that ensures optimized {{Collection.contains()}} performance.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CollectionUtils

        public CollectionUtils()
    • Method Detail

      • convert

        public static <S,​T,​C extends java.util.Collection<T>> C convert​(java.util.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> java.util.Collection<T> append​(java.util.Collection<T> target,
                                                         java.util.Collection<? extends T> source,
                                                         boolean allowDuplication)
        Append source to target collection.
        Parameters:
        target - target
        source - source
        allowDuplication - do not check for already added element if true
        Returns:
        appended collection
      • append

        public static <T> java.util.Collection<T> append​(java.util.Collection<T> target,
                                                         java.util.Iterator<? extends T> sourceIterator,
                                                         boolean allowDuplication)
        Append source to target collection.
        Parameters:
        target - target
        sourceIterator - source iterator.
        allowDuplication - do not check for already added element if true
        Returns:
        appended collection
      • append

        public static <T> java.util.Collection<T> append​(java.util.Collection<T> target,
                                                         java.util.Iterator<? extends T> sourceIterator,
                                                         boolean allowDuplication,
                                                         boolean allowNulls)
        Append source to target collection.
        Parameters:
        target - target
        sourceIterator - source iterator.
        allowDuplication - do not check for already added element if true
        allowNulls - allows nulls
        Returns:
        appended collection
      • append

        public static <T> java.util.Collection<T> append​(java.util.Collection<T> target,
                                                         T[] source,
                                                         boolean allowDuplication)
        Append source array to target collection.
        Parameters:
        target - target
        source - source
        allowDuplication - do not check for already added element if true
        Returns:
        appended vector
      • contains

        public static boolean contains​(java.util.Iterator it,
                                       java.lang.Object o)
        Checks if given iterator contains given object.
        Parameters:
        it - the iterator.
        o - the object.
        Returns:
        true, if it contains o.
      • containsAny

        public static boolean containsAny​(java.util.Collection location,
                                          java.util.Collection what)
        Returns true if location contains any element from what
        Parameters:
        location - location collection
        what - what collection.
        Returns:
        boolean.
      • createCollection

        public static <C extends java.util.Collection<E>,​E> C createCollection​(java.lang.Class<C> collection,
                                                                                     java.util.Iterator<E> it)
        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

        public static <T> java.util.Collection<T> notDuplicatedList​(java.util.Collection<T> collection)
        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
        public static <T> java.util.Collection<T> notDublicatedList​(java.util.Collection<T> collection)
        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.
      • createStringRepresentation

        public static <T> java.lang.String createStringRepresentation​(java.util.Collection<T> collection,
                                                                      java.lang.String delimiter,
                                                                      Converter<T,​java.lang.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> java.util.Collection<T> createFromStringRepresentation​(java.lang.String string,
                                                                                 java.lang.String delimiter,
                                                                                 Converter<java.lang.String,​T> converter)
        Create collection from string.
        Parameters:
        string - string representation.
        delimiter - values delimiter.
        converter - convert string to object.
        Returns:
        collection.
      • leaveElements

        public static void leaveElements​(java.util.List elements,
                                         int numberToLeave)
        Leaves a given number of first elements in the collection.
        Parameters:
        elements - elements to filter.
        numberToLeave - number of elements to leave.
      • mapToString

        public static java.lang.String mapToString​(java.util.Map<java.lang.String,​java.lang.String> map)
        Serializes map to string.
        Parameters:
        map - map to serialize.
        Returns:
        string representation of the map.
      • stringToMap

        public static java.util.Map<java.lang.String,​java.lang.String> stringToMap​(java.lang.String input)
        Deserialize map from string.
        Parameters:
        input - input string.
        Returns:
        map.
      • filterByClassType

        public static <T> java.util.List<T> filterByClassType​(java.util.Collection<?> input,
                                                              java.lang.Class<T> type)
        Filters and casts collection from one type to another
        Type Parameters:
        T - type
        Parameters:
        input - input collection
        type - desired output collection type
        Returns:
        new typed list
      • setSingleValue

        public static <T> void setSingleValue​(java.util.Collection<T> collection,
                                              T value)
        Removes all values from a given collection and adds a new one.
        Parameters:
        collection - collection
        value - a new value
      • setFirstValue

        public static <T> void setFirstValue​(java.util.Collection<T> collection,
                                             @CheckForNull
                                             T value)
        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 - collection
        value - a new value
      • toOptimizedContainsCollection

        public static <T> java.util.Collection<T> toOptimizedContainsCollection​(java.util.Collection<T> collection)
        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.
      • equals

        public static boolean equals​(java.util.Collection<?> collection1,
                                     java.util.Collection<?> collection2)
        Checks if both collections are equal - size the same and elements by order are the same
        Parameters:
        collection1 - first collection
        collection2 - second collection
        Returns:
        true if size the same and elements by order are the same