Class CollectionUtils

java.lang.Object
io.micronaut.core.util.CollectionUtils

public class CollectionUtils extends Object

Utility methods for working with Collection types

.
Since:
1.0
Author:
Graeme Rocher
  • Constructor Details

    • CollectionUtils

      public CollectionUtils()
  • Method Details

    • concat

      public static <E> Set<E> concat(Set<E> set, E element)
      The method will merge the set and element into a new set.
      Type Parameters:
      E - The element type
      Parameters:
      set - The set
      element - The element
      Returns:
      The new set
      Since:
      4.0.0
    • concat

      public static <E> Set<E> concat(Set<E> set1, Collection<E> collection)
      The method will merge two sets into a new set.
      Type Parameters:
      E - The element type
      Parameters:
      set1 - The first set
      collection - The second collection
      Returns:
      The new set
      Since:
      4.0.0
    • concat

      public static <E> List<E> concat(List<E> list, E element)
      The method will merge the list and element into a new list.
      Type Parameters:
      E - The element type
      Parameters:
      list - The list
      element - The element
      Returns:
      The new list
      Since:
      4.0.0
    • concat

      public static <E> List<E> concat(List<E> list1, Collection<E> collection)
      The method will merge two list into a new list.
      Type Parameters:
      E - The element type
      Parameters:
      list1 - The first list
      collection - The second collection
      Returns:
      The new list
      Since:
      4.0.0
    • newHashSet

      public static <E> HashSet<E> newHashSet(int size)
      Create new HashSet sized to fit all the elements of the size provided.
      Type Parameters:
      E - The element type
      Parameters:
      size - The size to fit all the elements
      Returns:
      a new HashSet with reallocated size
      Since:
      4.0.0
    • newLinkedHashSet

      public static <E> LinkedHashSet<E> newLinkedHashSet(int size)
      Create new LinkedHashSet sized to fit all the elements of the size provided.
      Type Parameters:
      E - The element type
      Parameters:
      size - The size to fit all the elements
      Returns:
      a new LinkedHashSet with reallocated size
      Since:
      4.0.0
    • newHashMap

      public static <K, V> HashMap<K,V> newHashMap(int size)
      Create new HashMap sized to fit all the elements of the size provided.
      Type Parameters:
      K - The key type
      V - The value type
      Parameters:
      size - The size to fit all the elements
      Returns:
      a new HashMap with reallocated size
      Since:
      4.0.0
    • newLinkedHashMap

      public static <K, V> LinkedHashMap<K,V> newLinkedHashMap(int size)
      Create new LinkedHashMap sized to fit all the elements of the size provided.
      Type Parameters:
      K - The key type
      V - The value type
      Parameters:
      size - The size to fit all the elements
      Returns:
      a new LinkedHashMap with reallocated size
      Since:
      4.0.0
    • isIterableOrMap

      public static boolean isIterableOrMap(Class<?> type)
      Is the given type an iterable or map type.
      Parameters:
      type - The type
      Returns:
      True if it is iterable or map
      Since:
      2.0.0
    • isEmpty

      public static boolean isEmpty(@Nullable @Nullable Map map)
      Null safe empty check.
      Parameters:
      map - The map
      Returns:
      True if it is empty or null
    • isNotEmpty

      public static boolean isNotEmpty(@Nullable @Nullable Map map)
      Null safe not empty check.
      Parameters:
      map - The map
      Returns:
      True if it is not null and not empty
    • isEmpty

      public static boolean isEmpty(@Nullable @Nullable Collection collection)
      Null safe empty check.
      Parameters:
      collection - The collection
      Returns:
      True if it is empty or null
    • isNotEmpty

      public static boolean isNotEmpty(@Nullable @Nullable Collection collection)
      Null safe not empty check.
      Parameters:
      collection - The collection
      Returns:
      True if it is not null and not empty
    • convertCollection

      public static <T> Optional<Iterable<T>> convertCollection(Class<? extends Iterable<T>> iterableType, Collection<T> collection)

      Attempts to convert a collection to the given iterabable type

      .
      Type Parameters:
      T - The collection generic type
      Parameters:
      iterableType - The iterable type
      collection - The collection
      Returns:
      An Optional of the converted type
    • mapOf

      public static Map mapOf(Object... values)
      Create a LinkedHashMap from an array of values.
      Parameters:
      values - The values
      Returns:
      The created map
    • iteratorToSet

      public static <T> Set<T> iteratorToSet(Iterator<T> iterator)
      Convert an Iterator to a Set.
      Type Parameters:
      T - The type
      Parameters:
      iterator - The iterator
      Returns:
      The set
    • enumerationToSet

      public static <T> Set<T> enumerationToSet(Enumeration<T> enumeration)
      Convert an Enumeration to a Set.
      Type Parameters:
      T - The type
      Parameters:
      enumeration - The iterator
      Returns:
      The set
    • enumerationToIterable

      @NonNull public static <T> @NonNull Iterable<T> enumerationToIterable(@Nullable @Nullable Enumeration<T> enumeration)
      Convert an Enumeration to a Iterable.
      Type Parameters:
      T - The type
      Parameters:
      enumeration - The iterator
      Returns:
      The set
    • setOf

      public static <T> Set<T> setOf(T... objects)
      Creates a set of the given objects.
      Type Parameters:
      T - The type
      Parameters:
      objects - The objects
      Returns:
      The set
    • toString

      public static String toString(Iterable<?> iterable)
      Produce a string representation of the given iterable.
      Parameters:
      iterable - The iterable
      Returns:
      The string representation
    • toString

      public static String toString(String delimiter, Iterable<?> iterable)
      Produce a string representation of the given iterable.
      Parameters:
      delimiter - The delimiter
      iterable - The iterable
      Returns:
      The string representation
    • iterableToList

      public static <T> List<T> iterableToList(Iterable<T> iterable)
      Converts an Iterable to a List.
      Type Parameters:
      T - The generic type
      Parameters:
      iterable - The iterable
      Returns:
      The list
    • iterableToSet

      public static <T> Set<T> iterableToSet(Iterable<T> iterable)
      Converts an Iterable to a Set.
      Type Parameters:
      T - The generic type
      Parameters:
      iterable - The iterable
      Returns:
      The set
    • unmodifiableList

      @NonNull public static <T> @NonNull List<T> unmodifiableList(@Nullable @Nullable List<T> list)
      Type Parameters:
      T - The generic type
      Parameters:
      list - The list
      Returns:
      A non-null unmodifiable list
    • last

      @Nullable public static <T> T last(@NonNull @NonNull Collection<T> collection)
      Returns the last element of a collection.
      Type Parameters:
      T - The generic type
      Parameters:
      collection - The collection
      Returns:
      The last element of a collection or null
    • enumSet

      @NonNull public static <E extends Enum<E>> @NonNull EnumSet<E> enumSet(@NonNull @NonNull E... enums)
      Create an enum set from an array. NOTE: At least one item is required
      Type Parameters:
      E - The enum type
      Parameters:
      enums - The array of enums
      Returns:
      The enum set
      Since:
      4.6