Class CollectionUtils

java.lang.Object
org.springframework.util.CollectionUtils
org.springframework.data.gemfire.util.CollectionUtils

public abstract class CollectionUtils extends org.springframework.util.CollectionUtils
Abstract utility class used to operate on Java Collections Framework and classes.
Since:
1.7.0
See Also:
  • Constructor Details

    • CollectionUtils

      public CollectionUtils()
  • Method Details

    • addAll

      @NonNull public static <E, T extends Collection<E>> T addAll(@NonNull T collection, @Nullable Iterable<E> iterable)
      Adds all elements from the given Iterable to the Collection.
      Type Parameters:
      E - Class type of the elements in the Collection and Iterable.
      T - concrete Class type of the Collection.
      Parameters:
      collection - Collection in which to add the elements from the Iterable.
      iterable - Iterable containing the elements to add to the Collection.
      Returns:
      the given Collection.
      Throws:
      IllegalArgumentException - if Collection is null.
      See Also:
    • asSet

      @SafeVarargs @NonNull public static <T> Set<T> asSet(@NonNull T... elements)
      Returns an unmodifiable Set containing the elements from the given object array.
      Type Parameters:
      T - Class type of the elements.
      Parameters:
      elements - array of objects to add to the Set.
      Returns:
      an unmodifiable Set containing the elements from the given object array.
    • containsAny

      public static boolean containsAny(@Nullable Collection<?> collection, @Nullable Object... elements)
      Null-safe method to determines whether the given Collection contains any elements from the given array.
      Parameters:
      collection - Collection to evaluate
      elements - array of elements to evaluate.
      Returns:
      a boolean value indicating whether the collection contains at least 1 element from the given array.
      See Also:
    • emptyIterable

      @NonNull public static <T> Iterable<T> emptyIterable()
      Returns an empty Iterable object.
      Type Parameters:
      T - Class type of the elements in the Iterable.
      Returns:
      an empty Iterable.
      See Also:
    • iterable

      @NonNull public static <T> Iterable<T> iterable(@Nullable Enumeration<T> enumeration)
      Adapts the given Enumeration as an Iterable object for use within a for each loop.
      Type Parameters:
      T - the class type of the Enumeration elements.
      Parameters:
      enumeration - the Enumeration to adapt as an Iterable object.
      Returns:
      an Iterable instance of the Enumeration.
      See Also:
    • iterable

      @NonNull public static <T> Iterable<T> iterable(@Nullable Iterator<T> iterator)
      Adapts the given Iterator as an Iterable object for use within a for each loop.
      Type Parameters:
      T - the class type of the Iterator elements.
      Parameters:
      iterator - the Iterator to adapt as an Iterable object.
      Returns:
      an Iterable instance of the Iterator.
      See Also:
    • nullSafeCollection

      @NonNull public static <T> Collection<T> nullSafeCollection(@Nullable Collection<T> collection)
      Null-safe operation returning the given Collection if not null or an empty Collection (implemented with List) if null.
      Type Parameters:
      T - Class type of the Collection elements.
      Parameters:
      collection - Collection to evaluate.
      Returns:
      the given Collection if not null or return an empty Collection (implemented with List).
      See Also:
    • nullSafeEnumeration

      @NonNull public static <T> Enumeration<T> nullSafeEnumeration(@Nullable Enumeration<T> enumeration)
      Null-safe operation returning the given Enumeration if not null or an empty Enumeration if null.
      Type Parameters:
      T - type of elements contained in the Enumeration.
      Parameters:
      enumeration - Enumeration to evaluate.
      Returns:
      the given Enumeration if not null or an empty Enumeration.
      See Also:
    • nullSafeIterable

      @NonNull public static <T> Iterable<T> nullSafeIterable(@Nullable Iterable<T> iterable)
      A null-safe operation returning the original Iterable object if non-null or a default, empty Iterable implementation if null.
      Type Parameters:
      T - the class type of the iterable elements.
      Parameters:
      iterable - the Iterable object evaluated for a null reference.
      Returns:
      the Iterable object if not null or a default, empty Iterable implementation otherwise.
      See Also:
    • nullSafeIterable

      @Nullable public static <E, T extends Iterable<E>> T nullSafeIterable(@Nullable T iterable, @Nullable T defaultIterable)
      Returns the given Iterable if not null or empty, otherwise returns the defaultIterable.
      Type Parameters:
      T - concrete Class type of the Iterable.
      E - Class type of the elements in the Iterables.
      Parameters:
      iterable - Iterable to evaluate.
      defaultIterable - Iterable to return if the given iterable is null or empty.
      Returns:
      iterable if not null or empty otherwise return defaultIterable.
      See Also:
    • nullSafeIterator

      @NonNull public static <T> Iterator<T> nullSafeIterator(@Nullable Iterator<T> iterator)
      Null-safe operation returning the given Iterator if not null or an empty Iterator if null.
      Type Parameters:
      T - type of elements contained in the Iterator.
      Parameters:
      iterator - Iterator to evaluate.
      Returns:
      the given Iterator if not null or an empty Iterator.
      See Also:
    • nullSafeList

      @NonNull public static <T> List<T> nullSafeList(@Nullable List<T> list)
      Null-safe operation returning the given List if not null or an empty List if null.
      Type Parameters:
      T - Class type of the List elements.
      Parameters:
      list - List to evaluate.
      Returns:
      the given List if not null or an empty List.
      See Also:
    • nullSafeMap

      @NonNull public static <K, V> Map<K,V> nullSafeMap(@Nullable Map<K,V> map)
      Null-safe operation returning the given Map if not null or an empty Map if null.
      Type Parameters:
      K - Class type of the Map's keys.
      V - Class type of the Map's values.
      Parameters:
      map - Map to evaluate.
      Returns:
      the given Map if not null or an empty Map.
      See Also:
    • nullSafeSet

      @NonNull public static <T> Set<T> nullSafeSet(@Nullable Set<T> set)
      Null-safe operation returning the given Set if not null or an empty Set if null.
      Type Parameters:
      T - Class type of the Set elements.
      Parameters:
      set - Set to evaluate.
      Returns:
      the given Set if not null or an empty Set.
      See Also:
    • nullSafeIsEmpty

      public static boolean nullSafeIsEmpty(@Nullable Collection<?> collection)
      Determines whether the given Collection is empty.
      Parameters:
      collection - Collection to evaluate.
      Returns:
      a boolean value indicating whether the given Collection is empty.
      See Also:
    • nullSafeIsEmpty

      public static boolean nullSafeIsEmpty(@Nullable Iterable<?> iterable)
      Determines whether the given Iterable is empty.
      Parameters:
      iterable - Iterable to evaluate.
      Returns:
      a boolean value indicating whether the given Iterable is empty.
      See Also:
    • nullSafeIsEmpty

      public static boolean nullSafeIsEmpty(@Nullable Map<?,?> map)
      Determines whether the given Map is empty.
      Parameters:
      map - Map to evaluate.
      Returns:
      a boolean value indicating whether the given Map is empty.
      See Also:
    • nullSafeSize

      public static int nullSafeSize(@Nullable Collection<?> collection)
      Determines the Collection.size() of the given Collection.
      Parameters:
      collection - Collection to evaluate.
      Returns:
      the Collection.size() of the given Collection.
      See Also:
    • nullSafeSize

      public static long nullSafeSize(@Nullable Iterable iterable)
      Determines the size of the give Iterable.
      Parameters:
      iterable - Iterable to evaluate.
      Returns:
      the size indicating the number of elements contained by the given Iterable. If Iterable is null, then returns 0.
      See Also:
    • nullSafeSize

      public static int nullSafeSize(@Nullable Map<?,?> map)
      Determines the Map.size() of the given Map.
      Parameters:
      map - Map to evaluate.
      Returns:
      the Map.size() of the given Map.
      See Also:
    • sort

      @NonNull public static <T extends Comparable<T>> List<T> sort(@NonNull List<T> list)
      Sors the elements of the given List by their natural, Comparable ordering.
      Type Parameters:
      T - Comparable class type of the collection elements.
      Parameters:
      list - List of elements to sort.
      Returns:
      the List sorted.
      See Also:
    • subList

      @NonNull public static <T> List<T> subList(@NonNull List<T> source, int... indices)
      Returns a sub-list of elements from the given List based on the provided indices.
      Type Parameters:
      T - Class type of the elements in the list.
      Parameters:
      source - List from which the elements of the sub-list is constructed.
      indices - array of indexes in the source List to the elements used to construct the sub-list.
      Returns:
      a sub-list of elements from the given List based on the provided indices.
      Throws:
      IndexOutOfBoundsException - if the array of indexes contains an index that is not within the bounds of the list.
      NullPointerException - if either the list or indexes are null.
      See Also:
    • toString

      @NonNull public static String toString(@Nullable Map<?,?> map)
      Returns a String representation of the given Map.
      Parameters:
      map - Map represent as a String.
      Returns:
      a String describing the given Map.
      See Also:
      • newSortedMap(Map)
      • Map