Class CollectionHelper

java.lang.Object
io.github.torand.javacommons.collection.CollectionHelper

public final class CollectionHelper extends Object
Helper functions for collections
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> List<T>
    asList(Iterable<T> items)
    Creates a list of specified iterable items.
    static <T> List<T>
    asList(T... items)
    Creates a list of specified item array.
    static <T> List<T>
    asNonEmptyList(T first, T... others)
    Creates a list of at least one, possibly multiple items.
    static <T> List<T>
    concat(Iterable<T> first, Iterable<T> second)
    Concatenates the specified iterables into a single list.
    static <T> List<T>
    concat(Iterable<T> first, T... second)
    Concatenates the elements of the specified iterable and array into a single list.
    static <T> Stream<T>
    concatStream(Iterable<T> first, Iterable<T> second)
    Creates a concatenated stream from the elements in the specified iterables.
    static <T> boolean
    containsAny(Iterable<T> iterable, Predicate<T> predicate)
    Tests whether the specified iterable contains an element satisfying a predicate.
    static <T> boolean
    containsOneOf(Iterable<T> iterable, T... items)
    Tests whether the specified iterable contains one of the specified items.
    static <T> T
    headOf(Iterable<T> iterable)
    Returns the first element of the specified iterable.
    static boolean
    isEmpty(Collection<?> collection)
    Returns whether the specified collection is null or contains no elements.
    static boolean
    isEmpty(Map<?,?> map)
    Returns whether the specified map is null or contains no elements.
    static boolean
    nonEmpty(Collection<?> collection)
    Returns whether the specified collection contains at least one element.
    static boolean
    nonEmpty(Map<?,?> map)
    Returns whether the specified map contains at least one element.
    static <T> Stream<T>
    streamSafely(Iterable<T> iterable)
    Creates a stream from the elements in the specified iterable.
    static <T> Stream<T>
    streamSafely(Iterator<T> iterator)
    Creates a stream from the elements in the specified iterator.
    static <T> Stream<T>
    streamSafely(T... items)
    Creates a stream from the elements in the specified array.
    static <T> T
    tailOf(Iterable<T> iterable)
    Returns the last element of the specified iterable.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • asList

      public static <T> List<T> asList(Iterable<T> items)
      Creates a list of specified iterable items.
      Type Parameters:
      T - the item type.
      Parameters:
      items - the item iterable.
      Returns:
      the list.
    • asList

      @SafeVarargs public static <T> List<T> asList(T... items)
      Creates a list of specified item array.
      Type Parameters:
      T - the item type.
      Parameters:
      items - the item array.
      Returns:
      the list.
    • asNonEmptyList

      @SafeVarargs public static <T> List<T> asNonEmptyList(T first, T... others)
      Creates a list of at least one, possibly multiple items.
      Type Parameters:
      T - the item type.
      Parameters:
      first - the first item, which cannot be null.
      others - the additional items, which can be null or empty.
      Returns:
      the list.
    • isEmpty

      public static boolean isEmpty(Collection<?> collection)
      Returns whether the specified collection is null or contains no elements.
      Parameters:
      collection - the collection.
      Returns:
      true if the collection is null or contains no elements; else false.
    • isEmpty

      public static boolean isEmpty(Map<?,?> map)
      Returns whether the specified map is null or contains no elements.
      Parameters:
      map - the map.
      Returns:
      true if the map is null or contains no elements; else false.
    • nonEmpty

      public static boolean nonEmpty(Collection<?> collection)
      Returns whether the specified collection contains at least one element.
      Parameters:
      collection - the collection.
      Returns:
      true if the collection contains at least one element; else false.
    • nonEmpty

      public static boolean nonEmpty(Map<?,?> map)
      Returns whether the specified map contains at least one element.
      Parameters:
      map - the map.
      Returns:
      true if the map contains at least one element; else false.
    • streamSafely

      public static <T> Stream<T> streamSafely(Iterable<T> iterable)
      Creates a stream from the elements in the specified iterable. If the iterable is null or contains no elements, an empty stream is returned.
      Type Parameters:
      T - the element type.
      Parameters:
      iterable - the iterable.
      Returns:
      the stream.
    • streamSafely

      public static <T> Stream<T> streamSafely(Iterator<T> iterator)
      Creates a stream from the elements in the specified iterator. If the iterator is null or contains no elements, an empty stream is returned.
      Type Parameters:
      T - the element type.
      Parameters:
      iterator - the iterator.
      Returns:
      the stream.
    • streamSafely

      @SafeVarargs public static <T> Stream<T> streamSafely(T... items)
      Creates a stream from the elements in the specified array. If the array is null or contains no elements, an empty stream is returned.
      Type Parameters:
      T - the item type.
      Parameters:
      items - the items.
      Returns:
      the stream.
    • concatStream

      public static <T> Stream<T> concatStream(Iterable<T> first, Iterable<T> second)
      Creates a concatenated stream from the elements in the specified iterables.
      Type Parameters:
      T - the element type.
      Parameters:
      first - the first iterable.
      second - the second iterable.
      Returns:
      the stream.
    • concat

      public static <T> List<T> concat(Iterable<T> first, Iterable<T> second)
      Concatenates the specified iterables into a single list.
      Type Parameters:
      T - the element type.
      Parameters:
      first - the first iterable.
      second - the second iterable.
      Returns:
      the list.
    • concat

      @SafeVarargs public static <T> List<T> concat(Iterable<T> first, T... second)
      Concatenates the elements of the specified iterable and array into a single list.
      Type Parameters:
      T - the element type.
      Parameters:
      first - the iterable.
      second - the array.
      Returns:
      the list.
    • headOf

      public static <T> T headOf(Iterable<T> iterable)
      Returns the first element of the specified iterable.
      Type Parameters:
      T - the element type.
      Parameters:
      iterable - the iterable.
      Returns:
      the first element of the iterable.
    • tailOf

      public static <T> T tailOf(Iterable<T> iterable)
      Returns the last element of the specified iterable.
      Type Parameters:
      T - the element type.
      Parameters:
      iterable - the iterable.
      Returns:
      the lastelement of the iterable.
    • containsAny

      public static <T> boolean containsAny(Iterable<T> iterable, Predicate<T> predicate)
      Tests whether the specified iterable contains an element satisfying a predicate.
      Type Parameters:
      T - the element type.
      Parameters:
      iterable - the iterable.
      predicate - the predicate.
      Returns:
      true if the iterable contains an element satisfying the predicate; else false.
    • containsOneOf

      public static <T> boolean containsOneOf(Iterable<T> iterable, T... items)
      Tests whether the specified iterable contains one of the specified items.
      Type Parameters:
      T - the element type.
      Parameters:
      iterable - the iterable.
      items - the items to look for.
      Returns:
      true if the iterable contains one of the specified items; else false.