com.landawn.abacus.util.Iterables Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-common Show documentation
Show all versions of abacus-common Show documentation
A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.
/*
* Copyright (c) 2018, Haiyang Li.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.landawn.abacus.util;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.AbstractCollection;
import java.util.AbstractList;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.NavigableSet;
import java.util.NoSuchElementException;
import java.util.RandomAccess;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Function;
import com.landawn.abacus.util.Range.BoundType;
import com.landawn.abacus.util.u.Nullable;
import com.landawn.abacus.util.u.Optional;
import com.landawn.abacus.util.u.OptionalByte;
import com.landawn.abacus.util.u.OptionalChar;
import com.landawn.abacus.util.u.OptionalDouble;
import com.landawn.abacus.util.u.OptionalFloat;
import com.landawn.abacus.util.u.OptionalInt;
import com.landawn.abacus.util.u.OptionalLong;
import com.landawn.abacus.util.u.OptionalShort;
/**
*
* 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 {@code Collection/Array/Iterator}.
*
*
*
* The methods in this class should only read the input {@code Collection/Array/Iterator} parameters, not modify them.
*
*
* @see com.landawn.abacus.util.N
* @see com.landawn.abacus.util.Iterators
* @see com.landawn.abacus.util.Maps
* @see com.landawn.abacus.util.Strings
*/
public final class Iterables {
private Iterables() {
// Utility class.
}
/**
* Returns {@code OptionalChar.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param a
* @return
*/
@SafeVarargs
public static OptionalChar min(final char... a) {
return a == null || a.length == 0 ? OptionalChar.empty() : OptionalChar.of(N.min(a));
}
/**
* Returns {@code OptionalByte.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param a
* @return
*/
@SafeVarargs
public static OptionalByte min(final byte... a) {
return a == null || a.length == 0 ? OptionalByte.empty() : OptionalByte.of(N.min(a));
}
/**
* Returns {@code OptionalShort.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param a
* @return
*/
@SafeVarargs
public static OptionalShort min(final short... a) {
return a == null || a.length == 0 ? OptionalShort.empty() : OptionalShort.of(N.min(a));
}
/**
* Returns {@code OptionalInt.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param a
* @return
*/
@SafeVarargs
public static OptionalInt min(final int... a) {
return a == null || a.length == 0 ? OptionalInt.empty() : OptionalInt.of(N.min(a));
}
/**
* Returns {@code OptionalLong.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param a
* @return
*/
@SafeVarargs
public static OptionalLong min(final long... a) {
return a == null || a.length == 0 ? OptionalLong.empty() : OptionalLong.of(N.min(a));
}
/**
* Returns {@code OptionalFloat.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param a
* @return
*/
@SafeVarargs
public static OptionalFloat min(final float... a) {
return a == null || a.length == 0 ? OptionalFloat.empty() : OptionalFloat.of(N.min(a));
}
/**
* Returns {@code OptionalDouble.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param a
* @return
*/
@SafeVarargs
public static OptionalDouble min(final double... a) {
return a == null || a.length == 0 ? OptionalDouble.empty() : OptionalDouble.of(N.min(a));
}
/**
* Returns {@code OptionalChar.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param a
* @return
*/
@SafeVarargs
public static OptionalChar max(final char... a) {
return a == null || a.length == 0 ? OptionalChar.empty() : OptionalChar.of(N.max(a));
}
/**
* Returns {@code OptionalByte.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param a
* @return
*/
@SafeVarargs
public static OptionalByte max(final byte... a) {
return a == null || a.length == 0 ? OptionalByte.empty() : OptionalByte.of(N.max(a));
}
/**
* Returns {@code OptionalShort.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param a
* @return
*/
@SafeVarargs
public static OptionalShort max(final short... a) {
return a == null || a.length == 0 ? OptionalShort.empty() : OptionalShort.of(N.max(a));
}
/**
* Returns {@code OptionalInt.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param a
* @return
*/
@SafeVarargs
public static OptionalInt max(final int... a) {
return a == null || a.length == 0 ? OptionalInt.empty() : OptionalInt.of(N.max(a));
}
/**
* Returns {@code OptionalLong.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param a
* @return
*/
@SafeVarargs
public static OptionalLong max(final long... a) {
return a == null || a.length == 0 ? OptionalLong.empty() : OptionalLong.of(N.max(a));
}
/**
* Returns {@code OptionalFloat.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param a
* @return
*/
@SafeVarargs
public static OptionalFloat max(final float... a) {
return a == null || a.length == 0 ? OptionalFloat.empty() : OptionalFloat.of(N.max(a));
}
/**
* Returns {@code OptionalDouble.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param a
* @return
*/
@SafeVarargs
public static OptionalDouble max(final double... a) {
return a == null || a.length == 0 ? OptionalDouble.empty() : OptionalDouble.of(N.max(a));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param
* @param a
* @return
*/
public static > Nullable min(final T[] a) {
return N.isNullOrEmpty(a) ? Nullable. empty() : Nullable.of(N.min(a));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param
* @param a
* @param cmp
* @return
*/
public static Nullable min(final T[] a, final Comparator cmp) {
return N.isNullOrEmpty(a) ? Nullable. empty() : Nullable.of(N.min(a, cmp));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param
* @param c
* @return
*/
public static > Nullable min(final Iterable c) {
return min(c, N.NULL_MAX_COMPARATOR);
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param
* @param c
* @param cmp
* @return
*/
public static Nullable min(final Iterable c, final Comparator cmp) {
return c == null ? Nullable. empty() : min(c.iterator(), cmp);
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param
* @param iter
* @return
*/
public static > Nullable min(final Iterator iter) {
return min(iter, N.NULL_MAX_COMPARATOR);
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param
* @param iter
* @param cmp
* @return
*/
public static Nullable min(final Iterator iter, Comparator cmp) {
cmp = cmp == null ? N.NULL_MAX_COMPARATOR : cmp;
if (iter == null || !iter.hasNext()) {
return Nullable. empty();
}
T candidate = null;
T next = null;
do {
next = iter.next();
if (next == null && cmp == N.NULL_MIN_COMPARATOR) {
return Nullable.of(next);
} else if (cmp.compare(next, candidate) < 0) {
candidate = next;
}
} while (iter.hasNext());
return Nullable.of(candidate);
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param
* @param a
* @param keyMapper
* @return
*/
@SuppressWarnings("rawtypes")
public static Nullable minBy(final T[] a, final Function keyMapper) {
return min(a, Fn.comparingBy(keyMapper));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param
* @param c
* @param keyMapper
* @return
*/
@SuppressWarnings("rawtypes")
public static Nullable minBy(final Iterable c, final Function keyMapper) {
return min(c, Fn.comparingBy(keyMapper));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param
* @param iter
* @param keyMapper
* @return
*/
@SuppressWarnings("rawtypes")
public static Nullable minBy(final Iterator iter, final Function keyMapper) {
return min(iter, Fn.comparingBy(keyMapper));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param
* @param a
* @return
*/
public static > Nullable max(final T[] a) {
return N.isNullOrEmpty(a) ? Nullable. empty() : Nullable.of(N.max(a));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param
* @param a
* @param cmp
* @return
*/
public static Nullable max(final T[] a, final Comparator cmp) {
return N.isNullOrEmpty(a) ? Nullable. empty() : Nullable.of(N.max(a, cmp));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param
* @param c
* @return
*/
public static > Nullable max(final Iterable c) {
return max(c, N.NULL_MIN_COMPARATOR);
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param
* @param c
* @param cmp
* @return
*/
public static Nullable max(final Iterable c, final Comparator cmp) {
return c == null ? Nullable. empty() : max(c.iterator(), cmp);
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param
* @param iter
* @return
*/
public static > Nullable max(final Iterator iter) {
return max(iter, N.NULL_MIN_COMPARATOR);
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param
* @param iter
* @param cmp
* @return
*/
public static Nullable max(final Iterator iter, Comparator cmp) {
cmp = cmp == null ? N.NULL_MIN_COMPARATOR : cmp;
if (iter == null || !iter.hasNext()) {
return Nullable. empty();
}
T candidate = null;
T next = null;
do {
next = iter.next();
if (next == null && cmp == N.NULL_MAX_COMPARATOR) {
return Nullable.of(next);
} else if (cmp.compare(next, candidate) > 0) {
candidate = next;
}
} while (iter.hasNext());
return Nullable.of(candidate);
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param
* @param a
* @param keyMapper
* @return
*/
@SuppressWarnings("rawtypes")
public static Nullable maxBy(final T[] a, final Function keyMapper) {
return max(a, Fn.comparingBy(keyMapper));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param
* @param c
* @param keyMapper
* @return
*/
@SuppressWarnings("rawtypes")
public static Nullable maxBy(final Iterable c, final Function keyMapper) {
return max(c, Fn.comparingBy(keyMapper));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param
* @param iter
* @param keyMapper
* @return
*/
@SuppressWarnings("rawtypes")
public static Nullable maxBy(final Iterator iter, final Function keyMapper) {
return max(iter, Fn.comparingBy(keyMapper));
}
/**
*
* @param
* @param a
* @return
*/
public static > Optional> minMax(final T[] a) {
return N.isNullOrEmpty(a) ? Optional.> empty() : Optional.of(N.minMax(a));
}
/**
*
* @param
* @param a
* @param cmp
* @return
*/
public static Optional> minMax(final T[] a, Comparator cmp) {
return N.isNullOrEmpty(a) ? Optional.> empty() : Optional.of(N.minMax(a, cmp));
}
/**
*
* @param
* @param c
* @return
*/
public static > Optional> minMax(final Iterable c) {
final Iterator iter = c == null ? null : c.iterator();
return iter == null || !iter.hasNext() ? Optional.> empty() : Optional.of(N.minMax(iter));
}
/**
*
* @param
* @param c
* @param cmp
* @return
*/
public static Optional> minMax(final Iterable c, Comparator cmp) {
final Iterator iter = c == null ? null : c.iterator();
return iter == null || !iter.hasNext() ? Optional.> empty() : Optional.of(N.minMax(iter, cmp));
}
/**
*
* @param
* @param iter
* @return
* @see Iterables#minMax(Iterator)
*/
public static > Optional> minMax(final Iterator iter) {
return iter == null || !iter.hasNext() ? Optional.> empty() : Optional.of(N.minMax(iter));
}
/**
*
* @param
* @param iter
* @param cmp
* @return
* @see Iterables#minMax(Iterator, Comparator)
*/
public static Optional> minMax(final Iterator iter, Comparator cmp) {
return iter == null || !iter.hasNext() ? Optional.> empty() : Optional.of(N.minMax(iter, cmp));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param
* @param a
* @return
*/
public static > Nullable median(final T[] a) {
return N.isNullOrEmpty(a) ? Nullable. empty() : Nullable.of(N.median(a));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param
* @param c
* @return
*/
public static > Nullable median(final Collection c) {
return N.isNullOrEmpty(c) ? Nullable. empty() : Nullable.of(N.median(c));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param
* @param a
* @param cmp
* @return
*/
public static Nullable median(final T[] a, final Comparator cmp) {
return N.isNullOrEmpty(a) ? Nullable. empty() : Nullable.of(N.median(a, cmp));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param
* @param c
* @param cmp
* @return
*/
public static Nullable median(final Collection c, final Comparator cmp) {
return N.isNullOrEmpty(c) ? Nullable. empty() : Nullable.of(N.median(c, cmp));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param
* @param a
* @param keyMapper
* @return
*/
@SuppressWarnings("rawtypes")
public static Nullable medianBy(final T[] a, final Function keyMapper) {
return median(a, Fn.comparingBy(keyMapper));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param
* @param c
* @param keyMapper
* @return
*/
@SuppressWarnings("rawtypes")
public static Nullable medianBy(final Collection c, final Function keyMapper) {
return median(c, Fn.comparingBy(keyMapper));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or its length/size is less than {@code k}.
*
* @param
* @param c
* @param k
* @return
*/
public static > Nullable kthLargest(final Collection c, final int k) {
return N.isNullOrEmpty(c) || c.size() < k ? Nullable. empty() : Nullable.of(N.kthLargest(c, k));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or its length/size is less than {@code k}.
*
* @param
* @param a
* @param k
* @return
*/
public static > Nullable kthLargest(final T[] a, final int k) {
return N.isNullOrEmpty(a) || a.length < k ? Nullable. empty() : Nullable.of(N.kthLargest(a, k));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or its length/size is less than {@code k}.
*
* @param
* @param c
* @param k
* @param cmp
* @return
*/
public static Nullable kthLargest(final Collection c, final int k, final Comparator cmp) {
return N.isNullOrEmpty(c) || c.size() < k ? Nullable. empty() : Nullable.of(N.kthLargest(c, k, cmp));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or its length/size is less than {@code k}.
*
* @param
* @param a
* @param k
* @param cmp
* @return
*/
public static Nullable kthLargest(final T[] a, final int k, final Comparator cmp) {
return N.isNullOrEmpty(a) || a.length < k ? Nullable. empty() : Nullable.of(N.kthLargest(a, k, cmp));
}
/**
* Returns {@code OptionalLong.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or {@code fromIndex == toIndex}.
*
* @param
* @param c
* @return
*/
public static OptionalLong sumInt(final Iterable c) {
return sumInt(c, Fn.numToInt());
}
/**
* Returns {@code OptionalLong.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or {@code fromIndex == toIndex}.
*
* @param
* @param
* @param c
* @param func
* @return
* @throws E the e
*/
public static OptionalLong sumInt(final Iterable c, final Throwables.ToIntFunction func) throws E {
if (c == null) {
return OptionalLong.empty();
}
final Iterator iter = c.iterator();
if (iter.hasNext() == false) {
return OptionalLong.empty();
}
long result = 0;
do {
result += func.applyAsInt(iter.next());
} while (iter.hasNext());
return OptionalLong.of(result);
}
/**
* Returns {@code OptionalLong.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or {@code fromIndex == toIndex}.
*
* @param
* @param c
* @return
*/
public static OptionalLong sumLong(final Iterable c) {
return sumLong(c, Fn.numToLong());
}
/**
* Returns {@code OptionalLong.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or {@code fromIndex == toIndex}.
*
* @param
* @param
* @param c
* @param func
* @return
* @throws E the e
*/
public static OptionalLong sumLong(final Iterable c, final Throwables.ToLongFunction func) throws E {
if (c == null) {
return OptionalLong.empty();
}
final Iterator iter = c.iterator();
if (iter.hasNext() == false) {
return OptionalLong.empty();
}
long result = 0;
do {
result += func.applyAsLong(iter.next());
} while (iter.hasNext());
return OptionalLong.of(result);
}
/**
* Returns {@code OptionalDouble.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or {@code fromIndex == toIndex}.
*
* @param
* @param c
* @return
*/
public static OptionalDouble sumDouble(final Iterable c) {
return sumDouble(c, Fn.numToDouble());
}
/**
* Returns {@code OptionalDouble.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or {@code fromIndex == toIndex}.
*
* @param
* @param
* @param c
* @param func
* @return
* @throws E the e
*/
public static OptionalDouble sumDouble(final Iterable c, final Throwables.ToDoubleFunction func)
throws E {
if (c == null) {
return OptionalDouble.empty();
}
final Iterator iter = c.iterator();
if (iter.hasNext() == false) {
return OptionalDouble.empty();
}
double result = 0;
do {
result += func.applyAsDouble(iter.next());
} while (iter.hasNext());
return OptionalDouble.of(result);
}
/**
*
* @param c
* @return
*/
public static Optional sumBigInteger(final Iterable c) {
return sumBigInteger(c, Fn.identity());
}
/**
*
* @param
* @param
* @param c
* @param func
* @return
* @throws E the e
*/
public static Optional sumBigInteger(final Iterable c,
final Throwables.Function func) throws E {
if (c == null) {
return Optional.empty();
}
final Iterator iter = c.iterator();
if (iter.hasNext() == false) {
return Optional.empty();
}
BigInteger result = BigInteger.ZERO;
BigInteger next = null;
do {
next = func.apply(iter.next());
if (next != null) {
result = result.add(next);
}
} while (iter.hasNext());
return Optional.of(result);
}
/**
*
* @param c
* @return
*/
public static Optional sumBigDecimal(final Iterable c) {
return sumBigDecimal(c, Fn.identity());
}
/**
*
* @param
* @param
* @param c
* @param func
* @return
* @throws E the e
*/
public static Optional sumBigDecimal(final Iterable c,
final Throwables.Function func) throws E {
if (c == null) {
return Optional.empty();
}
final Iterator iter = c.iterator();
if (iter.hasNext() == false) {
return Optional.empty();
}
BigDecimal result = BigDecimal.ZERO;
BigDecimal next = null;
do {
next = func.apply(iter.next());
if (next != null) {
result = result.add(next);
}
} while (iter.hasNext());
return Optional.of(result);
}
/**
* Returns {@code OptionalDouble.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or {@code fromIndex == toIndex}.
*
* @param
* @param a
* @return
*/
public static OptionalDouble averageInt(final T[] a) {
return averageInt(a, Fn.numToInt());
}
/**
* Returns {@code OptionalDouble.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or {@code fromIndex == toIndex}.
*
* @param
* @param a
* @param fromIndex
* @param toIndex
* @return
*/
public static OptionalDouble averageInt(final T[] a, final int fromIndex, final int toIndex) {
return averageInt(a, fromIndex, toIndex, Fn.numToInt());
}
/**
* Returns {@code OptionalDouble.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or {@code fromIndex == toIndex}.
*
* @param
* @param
* @param a
* @param func
* @return
* @throws E the e
*/
public static OptionalDouble averageInt(final T[] a, final Throwables.ToIntFunction func) throws E {
if (N.isNullOrEmpty(a)) {
return OptionalDouble.empty();
}
return averageInt(a, 0, a.length, func);
}
/**
* Returns {@code OptionalDouble.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or {@code fromIndex == toIndex}.
*
* @param
* @param
* @param a
* @param fromIndex
* @param toIndex
* @param func
* @return
* @throws E the e
*/
public static OptionalDouble averageInt(final T[] a, final int fromIndex, final int toIndex,
final Throwables.ToIntFunction func) throws E {
N.checkFromToIndex(fromIndex, toIndex, N.len(a));
if (fromIndex == toIndex) {
return OptionalDouble.empty();
}
return OptionalDouble.of(((double) N.sumInt(a, fromIndex, toIndex, func)) / (toIndex - fromIndex));
}
/**
* Returns {@code OptionalDouble.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or {@code fromIndex == toIndex}.
*
* @param
* @param c
* @return
*/
public static OptionalDouble averageInt(final Iterable c) {
return averageInt(c, Fn.numToInt());
}
/**
* Returns {@code OptionalDouble.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or {@code fromIndex == toIndex}.
*
* @param
* @param
* @param c
* @param func
* @return
* @throws E the e
*/
public static OptionalDouble averageInt(final Iterable c, final Throwables.ToIntFunction func)
throws E {
if (c == null) {
return OptionalDouble.empty();
}
long sum = 0;
long count = 0;
for (T e : c) {
sum += func.applyAsInt(e);
count++;
}
return count == 0 ? OptionalDouble.empty() : OptionalDouble.of(((double) sum) / count);
}
/**
* Returns {@code OptionalDouble.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or {@code fromIndex == toIndex}.
*
* @param
* @param c
* @param fromIndex
* @param toIndex
* @return
*/
public static OptionalDouble averageInt(final Collection c, final int fromIndex, final int toIndex) {
return averageInt(c, fromIndex, toIndex, Fn.numToInt());
}
/**
* Returns {@code OptionalDouble.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or {@code fromIndex == toIndex}.
*
* @param
* @param
* @param c
* @param fromIndex
* @param toIndex
* @param func
* @return
* @throws E the e
*/
public static OptionalDouble averageInt(final Collection c, final int fromIndex, final int toIndex,
final Throwables.ToIntFunction func) throws E {
N.checkFromToIndex(fromIndex, toIndex, N.size(c));
if (fromIndex == toIndex) {
return OptionalDouble.empty();
}
return OptionalDouble.of(((double) N.sumInt(c, fromIndex, toIndex, func)) / (toIndex - fromIndex));
}
/**
* Returns {@code OptionalDouble.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or {@code fromIndex == toIndex}.
*
* @param
* @param a
* @return
*/
public static OptionalDouble averageLong(final T[] a) {
return averageLong(a, Fn.numToLong());
}
/**
* Returns {@code OptionalDouble.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or {@code fromIndex == toIndex}.
*
* @param
* @param a
* @param fromIndex
* @param toIndex
* @return
*/
public static OptionalDouble averageLong(final T[] a, final int fromIndex, final int toIndex) {
return averageLong(a, fromIndex, toIndex, Fn.numToLong());
}
/**
* Returns {@code OptionalDouble.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or {@code fromIndex == toIndex}.
*
* @param
* @param
* @param a
* @param func
* @return
* @throws E the e
*/
public static OptionalDouble averageLong(final T[] a, final Throwables.ToLongFunction func) throws E {
if (N.isNullOrEmpty(a)) {
return OptionalDouble.empty();
}
return averageLong(a, 0, a.length, func);
}
/**
* Returns {@code OptionalDouble.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or {@code fromIndex == toIndex}.
*
* @param
* @param
* @param a
* @param fromIndex
* @param toIndex
* @param func
* @return
* @throws E the e
*/
public static OptionalDouble averageLong(final T[] a, final int fromIndex, final int toIndex,
final Throwables.ToLongFunction func) throws E {
N.checkFromToIndex(fromIndex, toIndex, N.len(a));
if (fromIndex == toIndex) {
return OptionalDouble.empty();
}
return OptionalDouble.of(((double) N.sumLong(a, fromIndex, toIndex, func)) / (toIndex - fromIndex));
}
/**
* Returns {@code OptionalDouble.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or {@code fromIndex == toIndex}.
*
* @param
* @param c
* @return
*/
public static OptionalDouble averageLong(final Iterable c) {
return averageLong(c, Fn.numToLong());
}
/**
* Returns {@code OptionalDouble.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or {@code fromIndex == toIndex}.
*
* @param
* @param
* @param c
* @param func
* @return
* @throws E the e
*/
public static OptionalDouble averageLong(final Iterable c, final Throwables.ToLongFunction func)
throws E {
if (c == null) {
return OptionalDouble.empty();
}
long sum = 0;
long count = 0;
for (T e : c) {
sum += func.applyAsLong(e);
count++;
}
return count == 0 ? OptionalDouble.empty() : OptionalDouble.of(((double) sum) / count);
}
/**
* Returns {@code OptionalDouble.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or {@code fromIndex == toIndex}.
*
* @param
* @param c
* @param fromIndex
* @param toIndex
* @return
*/
public static OptionalDouble averageLong(final Collection c, final int fromIndex, final int toIndex) {
return averageLong(c, fromIndex, toIndex, Fn.numToLong());
}
/**
* Returns {@code OptionalDouble.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or {@code fromIndex == toIndex}.
*
* @param
* @param
* @param c
* @param fromIndex
* @param toIndex
* @param func
* @return
* @throws E the e
*/
public static OptionalDouble averageLong(final Collection c, final int fromIndex, final int toIndex,
final Throwables.ToLongFunction func) throws E {
N.checkFromToIndex(fromIndex, toIndex, N.size(c));
if (fromIndex == toIndex) {
return OptionalDouble.empty();
}
return OptionalDouble.of(((double) N.sumLong(c, fromIndex, toIndex, func)) / (toIndex - fromIndex));
}
/**
* Returns {@code OptionalDouble.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or {@code fromIndex == toIndex}.
*
* @param
* @param a
* @return
*/
public static OptionalDouble averageDouble(final T[] a) {
return averageDouble(a, Fn.numToDouble());
}
/**
* Returns {@code OptionalDouble.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or {@code fromIndex == toIndex}.
*
* @param
* @param a
* @param fromIndex
* @param toIndex
* @return
*/
public static OptionalDouble averageDouble(final T[] a, final int fromIndex, final int toIndex) {
return averageDouble(a, fromIndex, toIndex, Fn.numToDouble());
}
/**
* Returns {@code OptionalDouble.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or {@code fromIndex == toIndex}.
*
* @param
* @param
* @param a
* @param func
* @return
* @throws E the e
*/
public static OptionalDouble averageDouble(final T[] a, final Throwables.ToDoubleFunction func) throws E {
if (N.isNullOrEmpty(a)) {
return OptionalDouble.empty();
}
return averageDouble(a, 0, a.length, func);
}
/**
* Returns {@code OptionalDouble.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or {@code fromIndex == toIndex}.
*
* @param
* @param
* @param a
* @param fromIndex
* @param toIndex
* @param func
* @return
* @throws E the e
*/
public static OptionalDouble averageDouble(final T[] a, final int fromIndex, final int toIndex,
final Throwables.ToDoubleFunction func) throws E {
N.checkFromToIndex(fromIndex, toIndex, N.len(a));
if (fromIndex == toIndex) {
return OptionalDouble.empty();
}
final KahanSummation summation = new KahanSummation();
for (int i = fromIndex; i < toIndex; i++) {
summation.add(func.applyAsDouble(a[i]));
}
return summation.average();
}
/**
* Returns {@code OptionalDouble.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or {@code fromIndex == toIndex}.
*
* @param
* @param c
* @return
*/
public static OptionalDouble averageDouble(final Iterable c) {
return averageDouble(c, Fn.numToDouble());
}
/**
* Returns {@code OptionalDouble.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or {@code fromIndex == toIndex}.
*
* @param
* @param
* @param c
* @param func
* @return
* @throws E the e
*/
public static OptionalDouble averageDouble(final Iterable c, final Throwables.ToDoubleFunction func)
throws E {
if (c == null) {
return OptionalDouble.empty();
}
final KahanSummation summation = new KahanSummation();
for (T e : c) {
summation.add(func.applyAsDouble(e));
}
return summation.average();
}
/**
* Returns {@code OptionalDouble.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or {@code fromIndex == toIndex}.
*
* @param
* @param c
* @param fromIndex
* @param toIndex
* @return
*/
public static OptionalDouble averageDouble(final Collection c, final int fromIndex, final int toIndex) {
return averageDouble(c, fromIndex, toIndex, Fn.numToDouble());
}
/**
* Returns {@code OptionalDouble.empty()} if the specified {@code Array/Collection} is {@code null} or empty, or {@code fromIndex == toIndex}.
*
* @param
* @param
* @param c
* @param fromIndex
* @param toIndex
* @param func
* @return
* @throws E the e
*/
public static OptionalDouble averageDouble(final Collection c, final int fromIndex, final int toIndex,
final Throwables.ToDoubleFunction func) throws E {
N.checkFromToIndex(fromIndex, toIndex, N.size(c));
if (fromIndex == toIndex) {
return OptionalDouble.empty();
}
final KahanSummation summation = new KahanSummation();
if (c instanceof List && c instanceof RandomAccess) {
final List list = (List) c;
for (int i = fromIndex; i < toIndex; i++) {
summation.add(func.applyAsDouble(list.get(i)));
}
} else {
int idx = 0;
for (T e : c) {
if (idx++ < fromIndex) {
continue;
}
summation.add(func.applyAsDouble(e));
if (idx >= toIndex) {
break;
}
}
}
return summation.average();
}
/**
*
* @param c
* @return
*/
public static Optional averageBigInteger(final Iterable c) {
return averageBigInteger(c, Fn.identity());
}
/**
*
* @param
* @param
* @param c
* @param func
* @return
* @throws E the e
*/
public static Optional averageBigInteger(final Iterable c,
final Throwables.Function func) throws E {
if (c == null) {
return Optional.empty();
}
final Iterator iter = c.iterator();
if (iter.hasNext() == false) {
return Optional.empty();
}
BigInteger sum = BigInteger.ZERO;
long cnt = 0;
BigInteger next = null;
do {
next = func.apply(iter.next());
if (next != null) {
sum = sum.add(next);
cnt++;
}
} while (iter.hasNext());
return cnt == 0 ? Optional.of(BigDecimal.ZERO) : Optional.of(new BigDecimal(sum).divide(BigDecimal.valueOf(cnt)));
}
/**
*
* @param c
* @return
*/
public static Optional averageBigDecimal(final Iterable c) {
return averageBigDecimal(c, Fn.identity());
}
/**
*
* @param
* @param
* @param c
* @param func
* @return
* @throws E the e
*/
public static Optional averageBigDecimal(final Iterable c,
final Throwables.Function func) throws E {
if (c == null) {
return Optional.empty();
}
final Iterator iter = c.iterator();
if (iter.hasNext() == false) {
return Optional.empty();
}
BigDecimal sum = BigDecimal.ZERO;
long cnt = 0;
BigDecimal next = null;
do {
next = func.apply(iter.next());
if (next != null) {
sum = sum.add(next);
cnt++;
}
} while (iter.hasNext());
return cnt == 0 ? Optional.of(BigDecimal.ZERO) : Optional.of(sum.divide(BigDecimal.valueOf(cnt)));
}
/**
*
*
* @param a
* @param objToFind
* @return
*/
public static OptionalInt indexOf(final Object[] a, final Object objToFind) {
return Index.of(a, objToFind);
}
/**
*
*
* @param c
* @param objToFind
* @return
*/
public static OptionalInt indexOf(final Collection c, final Object objToFind) {
return Index.of(c, objToFind);
}
/**
*
*
* @param a
* @param objToFind
* @return
*/
public static OptionalInt lastIndexOf(final Object[] a, final Object objToFind) {
return Index.last(a, objToFind);
}
/**
*
*
* @param c
* @param objToFind
* @return
*/
public static OptionalInt lastIndexOf(final Collection c, final Object objToFind) {
return Index.last(c, objToFind);
}
/**
* Find first or last index.
*
* @param
* @param
* @param
* @param a
* @param predicateForFirst
* @param predicateForLast
* @return the optional int
* @throws E the e
* @throws E2 the e2
*/
public static OptionalInt findFirstOrLastIndex(final T[] a,
final Throwables.Predicate predicateForFirst, final Throwables.Predicate predicateForLast) throws E, E2 {
if (N.isNullOrEmpty(a)) {
return OptionalInt.empty();
}
final OptionalInt res = N.findFirstIndex(a, predicateForFirst);
return res.isPresent() ? res : N.findLastIndex(a, predicateForLast);
}
/**
*
*
* @param
* @param
* @param
* @param c
* @param predicateForFirst
* @param predicateForLast
* @return
* @throws E
* @throws E2
*/
public static OptionalInt findFirstOrLastIndex(final Collection c,
final Throwables.Predicate predicateForFirst, final Throwables.Predicate predicateForLast) throws E, E2 {
if (N.isNullOrEmpty(c)) {
return OptionalInt.empty();
}
final OptionalInt res = N.findFirstIndex(c, predicateForFirst);
return res.isPresent() ? res : N.findLastIndex(c, predicateForLast);
}
/**
* Find first and last index.
*
* @param
* @param
* @param a
* @param predicate
* @return the pair
* @throws E the e
*/
public static Pair findFirstAndLastIndex(final T[] a, final Throwables.Predicate predicate)
throws E {
return findFirstAndLastIndex(a, predicate, predicate);
}
/**
* Find first and last index.
*
* @param
* @param
* @param
* @param a
* @param predicateForFirst
* @param predicateForLast
* @return the pair
* @throws E the e
* @throws E2 the e2
*/
public static Pair findFirstAndLastIndex(final T[] a,
final Throwables.Predicate predicateForFirst, final Throwables.Predicate predicateForLast) throws E, E2 {
if (N.isNullOrEmpty(a)) {
return Pair.of(OptionalInt.empty(), OptionalInt.empty());
}
return Pair.of(N.findFirstIndex(a, predicateForFirst), N.findLastIndex(a, predicateForLast));
}
/**
* Find first and last index.
*
* @param
* @param
* @param c
* @param predicate
* @return the pair
* @throws E the e
*/
public static Pair findFirstAndLastIndex(final Collection c,
final Throwables.Predicate predicate) throws E {
return findFirstAndLastIndex(c, predicate, predicate);
}
/**
* Find first and last index.
*
* @param
* @param
* @param
* @param c
* @param predicateForFirst
* @param predicateForLast
* @return the pair
* @throws E the e
* @throws E2 the e2
*/
public static Pair findFirstAndLastIndex(final Collection c,
final Throwables.Predicate predicateForFirst, final Throwables.Predicate predicateForLast) throws E, E2 {
if (N.isNullOrEmpty(c)) {
return Pair.of(OptionalInt.empty(), OptionalInt.empty());
}
return Pair.of(N.findFirstIndex(c, predicateForFirst), N.findLastIndex(c, predicateForLast));
}
/**
* Find first or last.
*
* @param
* @param
* @param
* @param a
* @param predicateForFirst
* @param predicateForLast
* @return the nullable
* @throws E the e
* @throws E2 the e2
*/
public static Nullable