Class Iterables

java.lang.Object
com.landawn.abacus.util.Iterables

public final class Iterables extends Object

Note: This class includes codes copied from Apache Commons Lang, Google Guava and other open source projects under the Apache License 2.0. The methods copied from other libraries/frameworks/projects may be modified in this class.

This is a utility class for iterable data structures, including Collection/Array/Iterator.

The methods in this class should only read the input Collection/Array/Iterator parameters, not modify them.

See Also:
  • Method Details

    • min

      @SafeVarargs public static u.OptionalChar min(char... a)
      Returns OptionalChar.empty() if the specified Array/Collection is null or empty.
      Parameters:
      a -
      Returns:
    • min

      @SafeVarargs public static u.OptionalByte min(byte... a)
      Returns OptionalByte.empty() if the specified Array/Collection is null or empty.
      Parameters:
      a -
      Returns:
    • min

      @SafeVarargs public static u.OptionalShort min(short... a)
      Returns OptionalShort.empty() if the specified Array/Collection is null or empty.
      Parameters:
      a -
      Returns:
    • min

      @SafeVarargs public static u.OptionalInt min(int... a)
      Returns OptionalInt.empty() if the specified Array/Collection is null or empty.
      Parameters:
      a -
      Returns:
    • min

      @SafeVarargs public static u.OptionalLong min(long... a)
      Returns OptionalLong.empty() if the specified Array/Collection is null or empty.
      Parameters:
      a -
      Returns:
    • min

      @SafeVarargs public static u.OptionalFloat min(float... a)
      Returns OptionalFloat.empty() if the specified Array/Collection is null or empty.
      Parameters:
      a -
      Returns:
    • min

      @SafeVarargs public static u.OptionalDouble min(double... a)
      Returns OptionalDouble.empty() if the specified Array/Collection is null or empty.
      Parameters:
      a -
      Returns:
    • min

      public static <T extends Comparable<? super T>> u.Nullable<T> min(T[] a)
      Returns Nullable.empty() if the specified Array/Collection is null or empty.
      Type Parameters:
      T -
      Parameters:
      a -
      Returns:
    • min

      public static <T> u.Nullable<T> min(T[] a, Comparator<? super T> cmp)
      Returns Nullable.empty() if the specified Array/Collection is null or empty.
      Type Parameters:
      T -
      Parameters:
      a -
      cmp -
      Returns:
    • min

      public static <T extends Comparable<? super T>> u.Nullable<T> min(Iterable<? extends T> c)
      Returns Nullable.empty() if the specified Array/Collection is null or empty.
      Type Parameters:
      T -
      Parameters:
      c -
      Returns:
    • min

      public static <T> u.Nullable<T> min(Iterable<? extends T> c, Comparator<? super T> cmp)
      Returns Nullable.empty() if the specified Array/Collection is null or empty.
      Type Parameters:
      T -
      Parameters:
      c -
      cmp -
      Returns:
    • min

      public static <T extends Comparable<? super T>> u.Nullable<T> min(Iterator<? extends T> iter)
      Returns Nullable.empty() if the specified Array/Collection is null or empty.
      Type Parameters:
      T -
      Parameters:
      iter -
      Returns:
    • min

      public static <T> u.Nullable<T> min(Iterator<? extends T> iter, Comparator<? super T> cmp)
      Returns Nullable.empty() if the specified Array/Collection is null or empty.
      Type Parameters:
      T -
      Parameters:
      iter -
      cmp -
      Returns:
    • minBy

      public static <T> u.Nullable<T> minBy(T[] a, Function<? super T,? extends Comparable> keyMapper)
      Returns Nullable.empty() if the specified Array/Collection is null or empty.
      Type Parameters:
      T -
      Parameters:
      a -
      keyMapper -
      Returns:
    • minBy

      public static <T> u.Nullable<T> minBy(Iterable<? extends T> c, Function<? super T,? extends Comparable> keyMapper)
      Returns Nullable.empty() if the specified Array/Collection is null or empty.
      Type Parameters:
      T -
      Parameters:
      c -
      keyMapper -
      Returns:
    • minBy

      public static <T> u.Nullable<T> minBy(Iterator<? extends T> iter, Function<? super T,? extends Comparable> keyMapper)
      Returns Nullable.empty() if the specified Array/Collection is null or empty.
      Type Parameters:
      T -
      Parameters:
      iter -
      keyMapper -
      Returns:
    • minInt

      @Beta public static <T, E extends Exception> u.OptionalInt minInt(T[] a, Throwables.ToIntFunction<? super T,E> valueExtractor) throws E
      Returns the minimum int value extracted from the specified array a by valueExtractor, or defaultValue if a is null or empty.
      Type Parameters:
      T -
      E -
      Parameters:
      a -
      defaultValue -
      Returns:
      Throws:
      E
    • minLong

      @Beta public static <T, E extends Exception> u.OptionalLong minLong(T[] a, Throwables.ToLongFunction<? super T,E> valueExtractor) throws E
      Returns the minimum long value extracted from the specified array a by valueExtractor, or defaultValue if a is null or empty.
      Type Parameters:
      T -
      E -
      Parameters:
      a -
      defaultValue -
      Returns:
      Throws:
      E
    • minDouble

      @Beta public static <T, E extends Exception> u.OptionalDouble minDouble(T[] a, Throwables.ToDoubleFunction<? super T,E> valueExtractor) throws E
      Returns the minimum double value extracted from the specified array a by valueExtractor, or defaultValue if a is null or empty.
      Type Parameters:
      T -
      E -
      Parameters:
      a -
      defaultValue -
      Returns:
      Throws:
      E
    • max

      @SafeVarargs public static u.OptionalChar max(char... a)
      Returns OptionalChar.empty() if the specified Array/Collection is null or empty.
      Parameters:
      a -
      Returns:
    • max

      @SafeVarargs public static u.OptionalByte max(byte... a)
      Returns OptionalByte.empty() if the specified Array/Collection is null or empty.
      Parameters:
      a -
      Returns:
    • max

      @SafeVarargs public static u.OptionalShort max(short... a)
      Returns OptionalShort.empty() if the specified Array/Collection is null or empty.
      Parameters:
      a -
      Returns:
    • max

      @SafeVarargs public static u.OptionalInt max(int... a)
      Returns OptionalInt.empty() if the specified Array/Collection is null or empty.
      Parameters:
      a -
      Returns:
    • max

      @SafeVarargs public static u.OptionalLong max(long... a)
      Returns OptionalLong.empty() if the specified Array/Collection is null or empty.
      Parameters:
      a -
      Returns:
    • max

      @SafeVarargs public static u.OptionalFloat max(float... a)
      Returns OptionalFloat.empty() if the specified Array/Collection is null or empty.
      Parameters:
      a -
      Returns:
    • max

      @SafeVarargs public static u.OptionalDouble max(double... a)
      Returns OptionalDouble.empty() if the specified Array/Collection is null or empty.
      Parameters:
      a -
      Returns:
    • max

      public static <T extends Comparable<? super T>> u.Nullable<T> max(T[] a)
      Returns Nullable.empty() if the specified Array/Collection is null or empty.
      Type Parameters:
      T -
      Parameters:
      a -
      Returns:
    • max

      public static <T> u.Nullable<T> max(T[] a, Comparator<? super T> cmp)
      Returns Nullable.empty() if the specified Array/Collection is null or empty.
      Type Parameters:
      T -
      Parameters:
      a -
      cmp -
      Returns:
    • max

      public static <T extends Comparable<? super T>> u.Nullable<T> max(Iterable<? extends T> c)
      Returns Nullable.empty() if the specified Array/Collection is null or empty.
      Type Parameters:
      T -
      Parameters:
      c -
      Returns:
    • max

      public static <T> u.Nullable<T> max(Iterable<? extends T> c, Comparator<? super T> cmp)
      Returns Nullable.empty() if the specified Array/Collection is null or empty.
      Type Parameters:
      T -
      Parameters:
      c -
      cmp -
      Returns:
    • max

      public static <T extends Comparable<? super T>> u.Nullable<T> max(Iterator<? extends T> iter)
      Returns Nullable.empty() if the specified Array/Collection is null or empty.
      Type Parameters:
      T -
      Parameters:
      iter -
      Returns:
    • max

      public static <T> u.Nullable<T> max(Iterator<? extends T> iter, Comparator<? super T> cmp)
      Returns Nullable.empty() if the specified Array/Collection is null or empty.
      Type Parameters:
      T -
      Parameters:
      iter -
      cmp -
      Returns:
    • maxBy

      public static <T> u.Nullable<T> maxBy(T[] a, Function<? super T,? extends Comparable> keyMapper)
      Returns Nullable.empty() if the specified Array/Collection is null or empty.
      Type Parameters:
      T -
      Parameters:
      a -
      keyMapper -
      Returns:
    • maxBy

      public static <T> u.Nullable<T> maxBy(Iterable<? extends T> c, Function<? super T,? extends Comparable> keyMapper)
      Returns Nullable.empty() if the specified Array/Collection is null or empty.
      Type Parameters:
      T -
      Parameters:
      c -
      keyMapper -
      Returns:
    • maxBy

      public static <T> u.Nullable<T> maxBy(Iterator<? extends T> iter, Function<? super T,? extends Comparable> keyMapper)
      Returns Nullable.empty() if the specified Array/Collection is null or empty.
      Type Parameters:
      T -
      Parameters:
      iter -
      keyMapper -
      Returns:
    • maxInt

      @Beta public static <T, E extends Exception> u.OptionalInt maxInt(T[] a, Throwables.ToIntFunction<? super T,E> valueExtractor) throws E
      Returns the maximum int value extracted from the specified array a by valueExtractor, or defaultValue if a is null or empty.
      Type Parameters:
      T -
      E -
      Parameters:
      a -
      defaultValue -
      Returns:
      Throws:
      E
    • maxLong

      @Beta public static <T, E extends Exception> u.OptionalLong maxLong(T[] a, Throwables.ToLongFunction<? super T,E> valueExtractor) throws E
      Returns the maximum long value extracted from the specified array a by valueExtractor, or defaultValue if a is null or empty.
      Type Parameters:
      T -
      E -
      Parameters:
      a -
      defaultValue -
      Returns:
      Throws:
      E
    • maxDouble

      @Beta public static <T, E extends Exception> u.OptionalDouble maxDouble(T[] a, Throwables.ToDoubleFunction<? super T,E> valueExtractor) throws E
      Returns the maximum double value extracted from the specified array a by valueExtractor, or defaultValue if a is null or empty.
      Type Parameters:
      T -
      E -
      Parameters:
      a -
      defaultValue -
      Returns:
      Throws:
      E
    • minMax

      public static <T extends Comparable<? super T>> u.Optional<Pair<T,T>> minMax(T[] a)
      Type Parameters:
      T -
      Parameters:
      a -
      Returns:
    • minMax

      public static <T> u.Optional<Pair<T,T>> minMax(T[] a, Comparator<? super T> cmp)
      Type Parameters:
      T -
      Parameters:
      a -
      cmp -
      Returns:
    • minMax

      public static <T extends Comparable<? super T>> u.Optional<Pair<T,T>> minMax(Iterable<? extends T> c)
      Type Parameters:
      T -
      Parameters:
      c -
      Returns:
    • minMax

      public static <T> u.Optional<Pair<T,T>> minMax(Iterable<? extends T> c, Comparator<? super T> cmp)
      Type Parameters:
      T -
      Parameters:
      c -
      cmp -
      Returns:
    • minMax

      public static <T extends Comparable<? super T>> u.Optional<Pair<T,T>> minMax(Iterator<? extends T> iter)
      Type Parameters:
      T -
      Parameters:
      iter -
      Returns:
      See Also:
    • minMax

      public static <T> u.Optional<Pair<T,T>> minMax(Iterator<? extends T> iter, Comparator<? super T> cmp)
      Type Parameters:
      T -
      Parameters:
      iter -
      cmp -
      Returns:
      See Also:
    • median

      public static <T extends Comparable<? super T>> u.Nullable<T> median(T[] a)
      Returns Nullable.empty() if the specified Array/Collection is null or empty.
      Type Parameters:
      T -
      Parameters:
      a -
      Returns:
    • median

      public static <T> u.Nullable<T> median(T[] a, Comparator<? super T> cmp)
      Returns Nullable.empty() if the specified Array/Collection is null or empty.
      Type Parameters:
      T -
      Parameters:
      a -
      cmp -
      Returns:
    • median

      public static <T extends Comparable<? super T>> u.Nullable<T> median(Collection<? extends T> c)
      Returns Nullable.empty() if the specified Array/Collection is null or empty.
      Type Parameters:
      T -
      Parameters:
      c -
      Returns:
    • median

      public static <T> u.Nullable<T> median(Collection<? extends T> c, Comparator<? super T> cmp)
      Returns Nullable.empty() if the specified Array/Collection is null or empty.
      Type Parameters:
      T -
      Parameters:
      c -
      cmp -
      Returns:
    • medianBy

      public static <T> u.Nullable<T> medianBy(T[] a, Function<? super T,? extends Comparable> keyMapper)
      Returns Nullable.empty() if the specified Array/Collection is null or empty.
      Type Parameters:
      T -
      Parameters:
      a -
      keyMapper -
      Returns:
    • medianBy

      public static <T> u.Nullable<T> medianBy(Collection<? extends T> c, Function<? super T,? extends Comparable> keyMapper)
      Returns Nullable.empty() if the specified Array/Collection is null or empty.
      Type Parameters:
      T -
      Parameters:
      c -
      keyMapper -
      Returns:
    • kthLargest

      public static <T extends Comparable<? super T>> u.Nullable<T> kthLargest(T[] a, int k)
      Returns Nullable.empty() if the specified Array/Collection is null or empty, or its length/size is less than k.
      Type Parameters:
      T -
      Parameters:
      a -
      k -
      Returns:
    • kthLargest

      public static <T> u.Nullable<T> kthLargest(T[] a, int k, Comparator<? super T> cmp)
      Returns Nullable.empty() if the specified Array/Collection is null or empty, or its length/size is less than k.
      Type Parameters:
      T -
      Parameters:
      a -
      k -
      cmp -
      Returns:
    • kthLargest

      public static <T extends Comparable<? super T>> u.Nullable<T> kthLargest(Collection<? extends T> c, int k)
      Returns Nullable.empty() if the specified Array/Collection is null or empty, or its length/size is less than k.
      Type Parameters:
      T -
      Parameters:
      c -
      k -
      Returns:
    • kthLargest

      public static <T> u.Nullable<T> kthLargest(Collection<? extends T> c, int k, Comparator<? super T> cmp)
      Returns Nullable.empty() if the specified Array/Collection is null or empty, or its length/size is less than k.
      Type Parameters:
      T -
      Parameters:
      c -
      k -
      cmp -
      Returns:
    • sumInt

      public static <T extends Number> u.OptionalInt sumInt(Iterable<? extends T> c)
      Returns OptionalLong.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      Parameters:
      c -
      Returns:
    • sumInt

      public static <T, E extends Exception> u.OptionalInt sumInt(Iterable<? extends T> c, Throwables.ToIntFunction<? super T,E> func) throws E
      Returns OptionalLong.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      E -
      Parameters:
      c -
      func -
      Returns:
      Throws:
      E - the e
    • sumIntToLong

      public static <T extends Number> u.OptionalLong sumIntToLong(Iterable<? extends T> c)
      Returns OptionalLong.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      Parameters:
      c -
      Returns:
    • sumIntToLong

      public static <T, E extends Exception> u.OptionalLong sumIntToLong(Iterable<? extends T> c, Throwables.ToIntFunction<? super T,E> func) throws E
      Returns OptionalLong.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      E -
      Parameters:
      c -
      func -
      Returns:
      Throws:
      E - the e
    • sumLong

      public static <T extends Number> u.OptionalLong sumLong(Iterable<? extends T> c)
      Returns OptionalLong.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      Parameters:
      c -
      Returns:
    • sumLong

      public static <T, E extends Exception> u.OptionalLong sumLong(Iterable<? extends T> c, Throwables.ToLongFunction<? super T,E> func) throws E
      Returns OptionalLong.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      E -
      Parameters:
      c -
      func -
      Returns:
      Throws:
      E - the e
    • sumDouble

      public static <T extends Number> u.OptionalDouble sumDouble(Iterable<? extends T> c)
      Returns OptionalDouble.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      Parameters:
      c -
      Returns:
    • sumDouble

      public static <T, E extends Exception> u.OptionalDouble sumDouble(Iterable<? extends T> c, Throwables.ToDoubleFunction<? super T,E> func) throws E
      Returns OptionalDouble.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      E -
      Parameters:
      c -
      func -
      Returns:
      Throws:
      E - the e
    • sumBigInteger

      public static u.Optional<BigInteger> sumBigInteger(Iterable<? extends BigInteger> c)
      Parameters:
      c -
      Returns:
    • sumBigInteger

      public static <T, E extends Exception> u.Optional<BigInteger> sumBigInteger(Iterable<? extends T> c, Throwables.Function<? super T,BigInteger,E> func) throws E
      Type Parameters:
      T -
      E -
      Parameters:
      c -
      func -
      Returns:
      Throws:
      E - the e
    • sumBigDecimal

      public static u.Optional<BigDecimal> sumBigDecimal(Iterable<? extends BigDecimal> c)
      Parameters:
      c -
      Returns:
    • sumBigDecimal

      public static <T, E extends Exception> u.Optional<BigDecimal> sumBigDecimal(Iterable<? extends T> c, Throwables.Function<? super T,BigDecimal,E> func) throws E
      Type Parameters:
      T -
      E -
      Parameters:
      c -
      func -
      Returns:
      Throws:
      E - the e
    • averageInt

      public static <T extends Number> u.OptionalDouble averageInt(T[] a)
      Returns OptionalDouble.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      Parameters:
      a -
      Returns:
    • averageInt

      public static <T extends Number> u.OptionalDouble averageInt(T[] a, int fromIndex, int toIndex)
      Returns OptionalDouble.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      Parameters:
      a -
      fromIndex -
      toIndex -
      Returns:
    • averageInt

      public static <T, E extends Exception> u.OptionalDouble averageInt(T[] a, Throwables.ToIntFunction<? super T,E> func) throws E
      Returns OptionalDouble.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      E -
      Parameters:
      a -
      func -
      Returns:
      Throws:
      E - the e
    • averageInt

      public static <T, E extends Exception> u.OptionalDouble averageInt(T[] a, int fromIndex, int toIndex, Throwables.ToIntFunction<? super T,E> func) throws E
      Returns OptionalDouble.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      E -
      Parameters:
      a -
      fromIndex -
      toIndex -
      func -
      Returns:
      Throws:
      E - the e
    • averageInt

      public static <T extends Number> u.OptionalDouble averageInt(Collection<? extends T> c, int fromIndex, int toIndex)
      Returns OptionalDouble.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      Parameters:
      c -
      fromIndex -
      toIndex -
      Returns:
    • averageInt

      public static <T, E extends Exception> u.OptionalDouble averageInt(Collection<? extends T> c, int fromIndex, int toIndex, Throwables.ToIntFunction<? super T,E> func) throws E
      Returns OptionalDouble.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      E -
      Parameters:
      c -
      fromIndex -
      toIndex -
      func -
      Returns:
      Throws:
      E - the e
    • averageInt

      public static <T extends Number> u.OptionalDouble averageInt(Iterable<? extends T> c)
      Returns OptionalDouble.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      Parameters:
      c -
      Returns:
    • averageInt

      public static <T, E extends Exception> u.OptionalDouble averageInt(Iterable<? extends T> c, Throwables.ToIntFunction<? super T,E> func) throws E
      Returns OptionalDouble.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      E -
      Parameters:
      c -
      func -
      Returns:
      Throws:
      E - the e
    • averageLong

      public static <T extends Number> u.OptionalDouble averageLong(T[] a)
      Returns OptionalDouble.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      Parameters:
      a -
      Returns:
    • averageLong

      public static <T extends Number> u.OptionalDouble averageLong(T[] a, int fromIndex, int toIndex)
      Returns OptionalDouble.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      Parameters:
      a -
      fromIndex -
      toIndex -
      Returns:
    • averageLong

      public static <T, E extends Exception> u.OptionalDouble averageLong(T[] a, Throwables.ToLongFunction<? super T,E> func) throws E
      Returns OptionalDouble.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      E -
      Parameters:
      a -
      func -
      Returns:
      Throws:
      E - the e
    • averageLong

      public static <T, E extends Exception> u.OptionalDouble averageLong(T[] a, int fromIndex, int toIndex, Throwables.ToLongFunction<? super T,E> func) throws E
      Returns OptionalDouble.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      E -
      Parameters:
      a -
      fromIndex -
      toIndex -
      func -
      Returns:
      Throws:
      E - the e
    • averageLong

      public static <T extends Number> u.OptionalDouble averageLong(Collection<? extends T> c, int fromIndex, int toIndex)
      Returns OptionalDouble.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      Parameters:
      c -
      fromIndex -
      toIndex -
      Returns:
    • averageLong

      public static <T, E extends Exception> u.OptionalDouble averageLong(Collection<? extends T> c, int fromIndex, int toIndex, Throwables.ToLongFunction<? super T,E> func) throws E
      Returns OptionalDouble.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      E -
      Parameters:
      c -
      fromIndex -
      toIndex -
      func -
      Returns:
      Throws:
      E - the e
    • averageLong

      public static <T extends Number> u.OptionalDouble averageLong(Iterable<? extends T> c)
      Returns OptionalDouble.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      Parameters:
      c -
      Returns:
    • averageLong

      public static <T, E extends Exception> u.OptionalDouble averageLong(Iterable<? extends T> c, Throwables.ToLongFunction<? super T,E> func) throws E
      Returns OptionalDouble.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      E -
      Parameters:
      c -
      func -
      Returns:
      Throws:
      E - the e
    • averageDouble

      public static <T extends Number> u.OptionalDouble averageDouble(T[] a)
      Returns OptionalDouble.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      Parameters:
      a -
      Returns:
    • averageDouble

      public static <T extends Number> u.OptionalDouble averageDouble(T[] a, int fromIndex, int toIndex)
      Returns OptionalDouble.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      Parameters:
      a -
      fromIndex -
      toIndex -
      Returns:
    • averageDouble

      public static <T, E extends Exception> u.OptionalDouble averageDouble(T[] a, Throwables.ToDoubleFunction<? super T,E> func) throws E
      Returns OptionalDouble.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      E -
      Parameters:
      a -
      func -
      Returns:
      Throws:
      E - the e
    • averageDouble

      public static <T, E extends Exception> u.OptionalDouble averageDouble(T[] a, int fromIndex, int toIndex, Throwables.ToDoubleFunction<? super T,E> func) throws E
      Returns OptionalDouble.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      E -
      Parameters:
      a -
      fromIndex -
      toIndex -
      func -
      Returns:
      Throws:
      E - the e
    • averageDouble

      public static <T extends Number> u.OptionalDouble averageDouble(Collection<? extends T> c, int fromIndex, int toIndex)
      Returns OptionalDouble.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      Parameters:
      c -
      fromIndex -
      toIndex -
      Returns:
    • averageDouble

      public static <T, E extends Exception> u.OptionalDouble averageDouble(Collection<? extends T> c, int fromIndex, int toIndex, Throwables.ToDoubleFunction<? super T,E> func) throws E
      Returns OptionalDouble.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      E -
      Parameters:
      c -
      fromIndex -
      toIndex -
      func -
      Returns:
      Throws:
      E - the e
    • averageDouble

      public static <T extends Number> u.OptionalDouble averageDouble(Iterable<? extends T> c)
      Returns OptionalDouble.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      Parameters:
      c -
      Returns:
    • averageDouble

      public static <T, E extends Exception> u.OptionalDouble averageDouble(Iterable<? extends T> c, Throwables.ToDoubleFunction<? super T,E> func) throws E
      Returns OptionalDouble.empty() if the specified Array/Collection is null or empty, or fromIndex == toIndex.
      Type Parameters:
      T -
      E -
      Parameters:
      c -
      func -
      Returns:
      Throws:
      E - the e
    • averageBigInteger

      public static u.Optional<BigDecimal> averageBigInteger(Iterable<? extends BigInteger> c)
      Parameters:
      c -
      Returns:
    • averageBigInteger

      public static <T, E extends Exception> u.Optional<BigDecimal> averageBigInteger(Iterable<? extends T> c, Throwables.Function<? super T,BigInteger,E> func) throws E
      Type Parameters:
      T -
      E -
      Parameters:
      c -
      func -
      Returns:
      Throws:
      E - the e
    • averageBigDecimal

      public static u.Optional<BigDecimal> averageBigDecimal(Iterable<? extends BigDecimal> c)
      Parameters:
      c -
      Returns:
    • averageBigDecimal

      public static <T, E extends Exception> u.Optional<BigDecimal> averageBigDecimal(Iterable<? extends T> c, Throwables.Function<? super T,BigDecimal,E> func) throws E
      Type Parameters:
      T -
      E -
      Parameters:
      c -
      func -
      Returns:
      Throws:
      E - the e
    • indexOf

      public static u.OptionalInt indexOf(Object[] a, Object objToFind)
      Parameters:
      a -
      objToFind -
      Returns:
      See Also:
    • indexOf

      public static u.OptionalInt indexOf(Collection<?> c, Object objToFind)
      Parameters:
      c -
      objToFind -
      Returns:
      See Also:
    • lastIndexOf

      public static u.OptionalInt lastIndexOf(Object[] a, Object objToFind)
      Parameters:
      a -
      objToFind -
      Returns:
      See Also:
    • lastIndexOf

      public static u.OptionalInt lastIndexOf(Collection<?> c, Object objToFind)
      Parameters:
      c -
      objToFind -
      Returns:
      See Also:
    • findFirstOrLast

      public static <T, E extends Exception, E2 extends Exception> u.Nullable<T> findFirstOrLast(T[] a, Throwables.Predicate<? super T,E> predicateForFirst, Throwables.Predicate<? super T,E2> predicateForLast) throws E, E2
      Find first or last.
      Type Parameters:
      T -
      E -
      E2 -
      Parameters:
      a -
      predicateForFirst -
      predicateForLast -
      Returns:
      the nullable
      Throws:
      E - the e
      E2 - the e2
    • findFirstOrLast

      public static <T, E extends Exception, E2 extends Exception> u.Nullable<T> findFirstOrLast(Collection<? extends T> c, Throwables.Predicate<? super T,E> predicateForFirst, Throwables.Predicate<? super T,E2> predicateForLast) throws E, E2
      Find first or last.
      Type Parameters:
      T -
      E -
      E2 -
      Parameters:
      c -
      predicateForFirst -
      predicateForLast -
      Returns:
      the nullable
      Throws:
      E - the e
      E2 - the e2
    • findFirstOrLastIndex

      public static <T, E extends Exception, E2 extends Exception> u.OptionalInt findFirstOrLastIndex(T[] a, Throwables.Predicate<? super T,E> predicateForFirst, Throwables.Predicate<? super T,E2> predicateForLast) throws E, E2
      Find first or last index.
      Type Parameters:
      T -
      E -
      E2 -
      Parameters:
      a -
      predicateForFirst -
      predicateForLast -
      Returns:
      the optional int
      Throws:
      E - the e
      E2 - the e2
    • findFirstOrLastIndex

      public static <T, E extends Exception, E2 extends Exception> u.OptionalInt findFirstOrLastIndex(Collection<? extends T> c, Throwables.Predicate<? super T,E> predicateForFirst, Throwables.Predicate<? super T,E2> predicateForLast) throws E, E2
      Type Parameters:
      T -
      E -
      E2 -
      Parameters:
      c -
      predicateForFirst -
      predicateForLast -
      Returns:
      Throws:
      E
      E2
    • findFirstAndLast

      public static <T, E extends Exception> Pair<u.Nullable<T>,u.Nullable<T>> findFirstAndLast(T[] a, Throwables.Predicate<? super T,E> predicate) throws E
      Find first and last.
      Type Parameters:
      T -
      E -
      Parameters:
      a -
      predicate -
      Returns:
      the pair
      Throws:
      E - the e
    • findFirstAndLast

      public static <T, E extends Exception, E2 extends Exception> Pair<u.Nullable<T>,u.Nullable<T>> findFirstAndLast(T[] a, Throwables.Predicate<? super T,E> predicateForFirst, Throwables.Predicate<? super T,E2> predicateForLast) throws E, E2
      Find first and last.
      Type Parameters:
      T -
      E -
      E2 -
      Parameters:
      a -
      predicateForFirst -
      predicateForLast -
      Returns:
      the pair
      Throws:
      E - the e
      E2 - the e2
    • findFirstAndLast

      public static <T, E extends Exception> Pair<u.Nullable<T>,u.Nullable<T>> findFirstAndLast(Collection<? extends T> c, Throwables.Predicate<? super T,E> predicate) throws E
      Find first and last.
      Type Parameters:
      T -
      E -
      Parameters:
      c -
      predicate -
      Returns:
      the pair
      Throws:
      E - the e
    • findFirstAndLast

      public static <T, E extends Exception, E2 extends Exception> Pair<u.Nullable<T>,u.Nullable<T>> findFirstAndLast(Collection<? extends T> c, Throwables.Predicate<? super T,E> predicateForFirst, Throwables.Predicate<? super T,E2> predicateForLast) throws E, E2
      Find first and last.
      Type Parameters:
      T -
      E -
      E2 -
      Parameters:
      c -
      predicateForFirst -
      predicateForLast -
      Returns:
      the pair
      Throws:
      E - the e
      E2 - the e2
    • findFirstAndLastIndex

      public static <T, E extends Exception> Pair<u.OptionalInt,u.OptionalInt> findFirstAndLastIndex(T[] a, Throwables.Predicate<? super T,E> predicate) throws E
      Find first and last index.
      Type Parameters:
      T -
      E -
      Parameters:
      a -
      predicate -
      Returns:
      the pair
      Throws:
      E - the e
    • findFirstAndLastIndex

      public static <T, E extends Exception, E2 extends Exception> Pair<u.OptionalInt,u.OptionalInt> findFirstAndLastIndex(T[] a, Throwables.Predicate<? super T,E> predicateForFirst, Throwables.Predicate<? super T,E2> predicateForLast) throws E, E2
      Find first and last index.
      Type Parameters:
      T -
      E -
      E2 -
      Parameters:
      a -
      predicateForFirst -
      predicateForLast -
      Returns:
      the pair
      Throws:
      E - the e
      E2 - the e2
    • findFirstAndLastIndex

      public static <T, E extends Exception> Pair<u.OptionalInt,u.OptionalInt> findFirstAndLastIndex(Collection<? extends T> c, Throwables.Predicate<? super T,E> predicate) throws E
      Find first and last index.
      Type Parameters:
      T -
      E -
      Parameters:
      c -
      predicate -
      Returns:
      the pair
      Throws:
      E - the e
    • findFirstAndLastIndex

      public static <T, E extends Exception, E2 extends Exception> Pair<u.OptionalInt,u.OptionalInt> findFirstAndLastIndex(Collection<? extends T> c, Throwables.Predicate<? super T,E> predicateForFirst, Throwables.Predicate<? super T,E2> predicateForLast) throws E, E2
      Find first and last index.
      Type Parameters:
      T -
      E -
      E2 -
      Parameters:
      c -
      predicateForFirst -
      predicateForLast -
      Returns:
      the pair
      Throws:
      E - the e
      E2 - the e2
    • union

      public static <E> Iterables.SetView<E> union(Set<? extends E> set1, Set<? extends E> set2)
      Returns an unmodifiable view of the union of two sets. The returned set contains all elements that are contained in either backing set. Iterating over the returned set iterates first over all the elements of set1, then over each element of set2, in order, that is not contained in set1.

      Results are undefined if set1 and set2 are sets based on different equivalence relations (as HashSet, TreeSet, and the Map.keySet() of an IdentityHashMap all are).

      Type Parameters:
      E -
      Parameters:
      set1 -
      set2 -
      Returns:
    • intersection

      public static <E> Iterables.SetView<E> intersection(Set<E> set1, Set<?> set2)
      Returns an unmodifiable view of the intersection of two sets. The returned set contains all elements that are contained by both backing sets. The iteration order of the returned set matches that of set1.

      Results are undefined if set1 and set2 are sets based on different equivalence relations (as HashSet, TreeSet, and the keySet of an IdentityHashMap all are).

      Note: The returned view performs slightly better when set1 is the smaller of the two sets. If you have reason to believe one of your sets will generally be smaller than the other, pass it first. Unfortunately, since this method sets the generic type of the returned set based on the type of the first set passed, this could in rare cases force you to make a cast, for example:

      
       Set<Object> aFewBadObjects = ...
       Set<String> manyBadStrings = ...
      
       // impossible for a non-String to be in the intersection
       SuppressWarnings("unchecked")
       Set<String> badStrings = (Set) Sets.intersection(
           aFewBadObjects, manyBadStrings);
       

      This is unfortunate, but should come up only very rarely.

      Type Parameters:
      E -
      Parameters:
      set1 -
      set2 -
      Returns:
    • difference

      public static <E> Iterables.SetView<E> difference(Set<E> set1, Set<?> set2)
      Returns an unmodifiable view of the difference of two sets. The returned set contains all elements that are contained by set1 and not contained by set2. set2 may also contain elements not present in set1; these are simply ignored. The iteration order of the returned set matches that of set1.

      Results are undefined if set1 and set2 are sets based on different equivalence relations (as HashSet, TreeSet, and the keySet of an IdentityHashMap all are).

      Type Parameters:
      E -
      Parameters:
      set1 -
      set2 -
      Returns:
    • symmetricDifference

      public static <E> Iterables.SetView<E> symmetricDifference(Set<? extends E> set1, Set<? extends E> set2)
      Returns an unmodifiable view of the symmetric difference of two sets. The returned set contains all elements that are contained in either set1 or set2 but not in both. The iteration order of the returned set is undefined.

      Results are undefined if set1 and set2 are sets based on different equivalence relations (as HashSet, TreeSet, and the keySet of an IdentityHashMap all are).

      Type Parameters:
      E -
      Parameters:
      set1 -
      set2 -
      Returns:
      Since:
      3.0
    • subSet

      public static <K extends Comparable<? super K>> NavigableSet<K> subSet(NavigableSet<K> set, Range<K> range)
      Type Parameters:
      K -
      Parameters:
      set -
      range -
      Returns:
    • powerSet

      public static <E> Set<Set<E>> powerSet(Set<E> set)
      Note: copy from Google Guava under Apache License v2.
      Returns the set of all possible subsets of set. For example, powerSet(ImmutableSet.of(1, 2)) returns the set {{}, {1}, {2}, {1, 2}}.

      Elements appear in these subsets in the same iteration order as they appeared in the input set. The order in which these subsets appear in the outer set is undefined. Note that the power set of the empty set is not the empty set, but a one-element set containing the empty set.

      The returned set and its constituent sets use equals to decide whether two elements are identical, even if the input set uses a different concept of equivalence.

      Performance notes: while the power set of a set with size n is of size 2^n, its memory usage is only O(n). When the power set is constructed, the input set is merely copied. Only as the power set is iterated are the individual subsets created, and these subsets themselves occupy only a small constant amount of memory.

      Type Parameters:
      E -
      Parameters:
      set - the set of elements to construct a power set from
      Returns:
      the sets the
      Throws:
      IllegalArgumentException - if set has more than 30 unique elements (causing the power set size to exceed the int range)
      NullPointerException - if set is or contains null
      See Also:
    • rollup

      public static <T> List<List<T>> rollup(Collection<? extends T> c)
      Type Parameters:
      T -
      Parameters:
      c -
      Returns:
      the list
    • permutations

      public static <E> Collection<List<E>> permutations(Collection<E> elements)
      Note: copy from Google Guava under Apache License v2.
      Returns a Collection of all the permutations of the specified Collection.

      Notes: This is an implementation of the Plain Changes algorithm for permutations generation, described in Knuth's "The Art of Computer Programming", Volume 4, Chapter 7, Section 7.2.1.2.

      If the input list contains equal elements, some of the generated permutations will be equal.

      An empty collection has only one permutation, which is an empty list.

      Type Parameters:
      E -
      Parameters:
      elements - the original collection whose elements have to be permuted.
      Returns:
      an immutable Collection containing all the different permutations of the original collection.
      Throws:
      NullPointerException - if the specified collection is null or has any null elements.
    • orderedPermutations

      public static <E extends Comparable<? super E>> Collection<List<E>> orderedPermutations(Collection<E> elements)
      Note: copy from Google Guava under Apache License v2.
      Returns a Collection of all the permutations of the specified Iterable.

      Notes: This is an implementation of the algorithm for Lexicographical Permutations Generation, described in Knuth's "The Art of Computer Programming", Volume 4, Chapter 7, Section 7.2.1.2. The iteration order follows the lexicographical order. This means that the first permutation will be in ascending order, and the last will be in descending order.

      Duplicate elements are considered equal. For example, the list [1, 1] will have only one permutation, instead of two. This is why the elements have to implement Comparable.

      An empty iterable has only one permutation, which is an empty list.

      This method is equivalent to Collections2.orderedPermutations(list, Ordering.natural()).

      Type Parameters:
      E -
      Parameters:
      elements - the original iterable whose elements have to be permuted.
      Returns:
      an immutable Collection containing all the different permutations of the original iterable.
      Throws:
      NullPointerException - if the specified iterable is null or has any null elements.
    • orderedPermutations

      public static <E> Collection<List<E>> orderedPermutations(Collection<E> elements, Comparator<? super E> comparator)
      Note: copy from Google Guava under Apache License v2.
      Returns a Collection of all the permutations of the specified Iterable using the specified Comparator for establishing the lexicographical ordering.

      Examples:

         
      
         for (List<String> perm : orderedPermutations(asList("b", "c", "a"))) {
           println(perm);
         }
         // -> ["a", "b", "c"]
         // -> ["a", "c", "b"]
         // -> ["b", "a", "c"]
         // -> ["b", "c", "a"]
         // -> ["c", "a", "b"]
         // -> ["c", "b", "a"]
      
         for (List<Integer> perm : orderedPermutations(asList(1, 2, 2, 1))) {
           println(perm);
         }
         // -> [1, 1, 2, 2]
         // -> [1, 2, 1, 2]
         // -> [1, 2, 2, 1]
         // -> [2, 1, 1, 2]
         // -> [2, 1, 2, 1]
         // -> [2, 2, 1, 1]

      Notes: This is an implementation of the algorithm for Lexicographical Permutations Generation, described in Knuth's "The Art of Computer Programming", Volume 4, Chapter 7, Section 7.2.1.2. The iteration order follows the lexicographical order. This means that the first permutation will be in ascending order, and the last will be in descending order.

      Elements that compare equal are considered equal and no new permutations are created by swapping them.

      An empty iterable has only one permutation, which is an empty list.

      Type Parameters:
      E -
      Parameters:
      elements - the original iterable whose elements have to be permuted.
      comparator - a comparator for the iterable's elements.
      Returns:
      an immutable Collection containing all the different permutations of the original iterable.
      Throws:
      NullPointerException - If the specified iterable is null, has any null elements, or if the specified comparator is null.
    • cartesianProduct

      @SafeVarargs public static <E> List<List<E>> cartesianProduct(Collection<? extends E>... cs)
      Note: copy from Google Guava under Apache License v2.
      Returns every possible list that can be formed by choosing one element from each of the given lists in order; the "n-ary Cartesian product" of the lists. For example:
         
      
         Lists.cartesianProduct(ImmutableList.of(
             ImmutableList.of(1, 2),
             ImmutableList.of("A", "B", "C")))

      returns a list containing six lists in the following order:

      • ImmutableList.of(1, "A")
      • ImmutableList.of(1, "B")
      • ImmutableList.of(1, "C")
      • ImmutableList.of(2, "A")
      • ImmutableList.of(2, "B")
      • ImmutableList.of(2, "C")

      The result is guaranteed to be in the "traditional", lexicographical order for Cartesian products that you would get from nesting for loops:

         
      
         for (B b0 : lists.get(0)) {
           for (B b1 : lists.get(1)) {
             ...
             ImmutableList<B> tuple = ImmutableList.of(b0, b1, ...);
             // operate on tuple
           }
         }

      Note that if any input list is empty, the Cartesian product will also be empty. If no lists at all are provided (an empty list), the resulting Cartesian product has one element, an empty list (counter-intuitive, but mathematically consistent).

      Performance notes: while the cartesian product of lists of size m, n, p is a list of size m x n x p, its actual memory consumption is much smaller. When the cartesian product is constructed, the input lists are merely copied. Only as the resulting list is iterated are the individual lists created, and these are not retained after iteration.

      Type Parameters:
      E - any common base class shared by all axes (often just Object)
      Parameters:
      cs - the lists to choose elements from, in the order that the elements chosen from those lists should appear in the resulting lists
      Returns:
      lists
      Throws:
      IllegalArgumentException - if the size of the cartesian product would be greater than Integer.MAX_VALUE
      NullPointerException - if lists, any one of the lists, or any element of a provided list is null
    • cartesianProduct

      public static <E> List<List<E>> cartesianProduct(Collection<? extends Collection<? extends E>> cs)
      Note: copy from Google Guava under Apache License v2.
      Returns every possible list that can be formed by choosing one element from each of the given lists in order; the "n-ary Cartesian product" of the lists. For example:
         
      
         Lists.cartesianProduct(ImmutableList.of(
             ImmutableList.of(1, 2),
             ImmutableList.of("A", "B", "C")))

      returns a list containing six lists in the following order:

      • ImmutableList.of(1, "A")
      • ImmutableList.of(1, "B")
      • ImmutableList.of(1, "C")
      • ImmutableList.of(2, "A")
      • ImmutableList.of(2, "B")
      • ImmutableList.of(2, "C")

      The result is guaranteed to be in the "traditional", lexicographical order for Cartesian products that you would get from nesting for loops:

         
      
         for (B b0 : lists.get(0)) {
           for (B b1 : lists.get(1)) {
             ...
             ImmutableList<B> tuple = ImmutableList.of(b0, b1, ...);
             // operate on tuple
           }
         }

      Note that if any input list is empty, the Cartesian product will also be empty. If no lists at all are provided (an empty list), the resulting Cartesian product has one element, an empty list (counter-intuitive, but mathematically consistent).

      Performance notes: while the cartesian product of lists of size m, n, p is a list of size m x n x p, its actual memory consumption is much smaller. When the cartesian product is constructed, the input lists are merely copied. Only as the resulting list is iterated are the individual lists created, and these are not retained after iteration.

      Type Parameters:
      E - any common base class shared by all axes (often just Object)
      Parameters:
      cs - the lists to choose elements from, in the order that the elements chosen from those lists should appear in the resulting lists
      Returns:
      lists
      Throws:
      IllegalArgumentException - if the size of the cartesian product would be greater than Integer.MAX_VALUE
      NullPointerException - if lists, any one of the lists, or any element of a provided list is null