
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-se Show documentation
Show all versions of abacus-common-se Show documentation
A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.
The newest version!
/*
* 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.util.AbstractSet;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.RandomAccess;
import java.util.Set;
import com.landawn.abacus.util.u.Nullable;
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;
import com.landawn.abacus.util.function.Function;
/**
*
* 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.StringUtil
*/
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 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 a
* @param cmp
* @return
*/
public static Nullable min(final T[] a, final Comparator super T> 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 c
* @return
*/
public static > Nullable min(final Collection extends T> c) {
return N.isNullOrEmpty(c) ? Nullable. empty() : Nullable.of(N.min(c));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param c
* @param cmp
* @return
*/
public static Nullable min(final Collection extends T> c, final Comparator super T> cmp) {
return N.isNullOrEmpty(c) ? Nullable. empty() : Nullable.of(N.min(c, cmp));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param iter
* @return
*/
public static > Nullable min(final Iterator extends T> iter) {
return min(iter, N.NULL_MAX_COMPARATOR);
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param iter
* @param cmp
* @return
*/
public static Nullable min(final Iterator extends T> iter, Comparator super T> cmp) {
cmp = cmp == null ? N.NULL_MAX_COMPARATOR : cmp;
if (iter == null || iter.hasNext() == false) {
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 a
* @param cmp
* @return
*/
@SuppressWarnings("rawtypes")
public static Nullable minBy(final T[] a, final Function super T, ? extends Comparable> keyMapper) {
return min(a, Fn.comparingBy(keyMapper));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param c
* @param cmp
* @return
*/
@SuppressWarnings("rawtypes")
public static Nullable minBy(final Collection extends T> c, final Function super T, ? extends Comparable> keyMapper) {
return min(c, Fn.comparingBy(keyMapper));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param iter
* @param cmp
* @return
*/
@SuppressWarnings("rawtypes")
public static Nullable minBy(final Iterator extends T> iter, final Function super T, ? extends Comparable> keyMapper) {
return min(iter, Fn.comparingBy(keyMapper));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @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 a
* @param cmp
* @return
*/
public static Nullable max(final T[] a, final Comparator super T> 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 c
* @return
*/
public static > Nullable max(final Collection extends T> c) {
return N.isNullOrEmpty(c) ? Nullable. empty() : Nullable.of(N.max(c));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param c
* @return
*/
public static Nullable max(final Collection extends T> c, final Comparator super T> cmp) {
return N.isNullOrEmpty(c) ? Nullable. empty() : Nullable.of(N.max(c, cmp));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param iter
* @return
*/
public static > Nullable max(final Iterator extends T> iter) {
return max(iter, N.NULL_MIN_COMPARATOR);
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param iter
* @param cmp
* @return
*/
public static Nullable max(final Iterator extends T> iter, Comparator super T> cmp) {
cmp = cmp == null ? N.NULL_MIN_COMPARATOR : cmp;
if (iter == null || iter.hasNext() == false) {
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 a
* @param cmp
* @return
*/
@SuppressWarnings("rawtypes")
public static Nullable maxBy(final T[] a, final Function super T, ? extends Comparable> keyMapper) {
return max(a, Fn.comparingBy(keyMapper));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param c
* @param cmp
* @return
*/
@SuppressWarnings("rawtypes")
public static Nullable maxBy(final Collection extends T> c, final Function super T, ? extends Comparable> keyMapper) {
return max(c, Fn.comparingBy(keyMapper));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param iter
* @param cmp
* @return
*/
@SuppressWarnings("rawtypes")
public static Nullable maxBy(final Iterator extends T> iter, final Function super T, ? extends Comparable> keyMapper) {
return max(iter, Fn.comparingBy(keyMapper));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @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 c
* @return
*/
public static > Nullable median(final Collection extends T> 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 a
* @param cmp
* @return
*/
public static Nullable median(final T[] a, final Comparator super T> 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 c
* @param cmp
* @return
*/
public static Nullable median(final Collection extends T> c, final Comparator super T> 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 a
* @param keyMapper
* @return
*/
@SuppressWarnings("rawtypes")
public static Nullable medianBy(final T[] a, final Function super T, ? extends Comparable> keyMapper) {
return median(a, Fn.comparingBy(keyMapper));
}
/**
* Returns {@code Nullable.empty()} if the specified {@code Array/Collection} is {@code null} or empty.
*
* @param c
* @param keyMapper
* @return
*/
@SuppressWarnings("rawtypes")
public static Nullable medianBy(final Collection extends T> c, final Function super T, ? extends Comparable> 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 c
* @param k
* @return
*/
public static > Nullable kthLargest(final Collection extends T> 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 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 c
* @param k
* @param cmp
* @return
*/
public static Nullable kthLargest(final Collection extends T> c, final int k, final Comparator super T> 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 a
* @param k
* @param cmp
* @return
*/
public static Nullable kthLargest(final T[] a, final int k, final Comparator super T> cmp) {
return N.isNullOrEmpty(a) || a.length < k ? Nullable. empty() : Nullable.of(N.kthLargest(a, k, cmp));
}
/**
* 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 super T, E> 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 super T, E> 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 Collection extends T> 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 c
* @param fromIndex
* @param toIndex
* @return
*/
public static OptionalDouble averageInt(final Collection extends T> 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 func
* @return
* @throws E the e
*/
public static OptionalDouble averageInt(final Collection extends T> c, final Throwables.ToIntFunction super T, E> func)
throws E {
if (N.isNullOrEmpty(c)) {
return OptionalDouble.empty();
}
return OptionalDouble.of(((double) N.sumInt(c, func)) / c.size());
}
/**
* 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 extends T> c, final int fromIndex, final int toIndex,
final Throwables.ToIntFunction super T, E> 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 super T, E> 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 super T, E> 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 Collection extends T> 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 c
* @param fromIndex
* @param toIndex
* @return
*/
public static OptionalDouble averageLong(final Collection extends T> 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 func
* @return
* @throws E the e
*/
public static OptionalDouble averageLong(final Collection extends T> c, final Throwables.ToLongFunction super T, E> func)
throws E {
if (N.isNullOrEmpty(c)) {
return OptionalDouble.empty();
}
return OptionalDouble.of(((double) N.sumLong(c, func)) / c.size());
}
/**
* 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 extends T> c, final int fromIndex, final int toIndex,
final Throwables.ToLongFunction super T, E> 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 super T, E> 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 super T, E> 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 Collection extends T> 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 c
* @param fromIndex
* @param toIndex
* @return
*/
public static OptionalDouble averageDouble(final Collection extends T> 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 func
* @return
* @throws E the e
*/
public static OptionalDouble averageDouble(final Collection extends T> c, final Throwables.ToDoubleFunction super T, E> func)
throws E {
if (N.isNullOrEmpty(c)) {
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
* @param c
* @param fromIndex
* @param toIndex
* @param func
* @return
* @throws E the e
*/
public static OptionalDouble averageDouble(final Collection extends T> c, final int fromIndex, final int toIndex,
final Throwables.ToDoubleFunction super T, E> 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();
}
public static OptionalInt indexOf(final Object[] a, final Object objToFind) {
return Index.of(a, objToFind);
}
public static OptionalInt indexOf(final Collection> c, final Object objToFind) {
return Index.of(c, objToFind);
}
public static OptionalInt lastIndexOf(final Object[] a, final Object objToFind) {
return Index.last(a, objToFind);
}
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 super T, E> predicateForFirst, final Throwables.Predicate super T, E2> 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);
}
public static OptionalInt findFirstOrLastIndex(final Collection extends T> c,
final Throwables.Predicate super T, E> predicateForFirst, final Throwables.Predicate super T, E2> 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 super T, E> 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 super T, E> predicateForFirst, final Throwables.Predicate super T, E2> 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 extends T> c,
final Throwables.Predicate super T, E> 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 extends T> c,
final Throwables.Predicate super T, E> predicateForFirst, final Throwables.Predicate super T, E2> 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 findFirstOrLast(final T[] a,
final Throwables.Predicate super T, E> predicateForFirst, final Throwables.Predicate super T, E2> predicateForLast) throws E, E2 {
if (N.isNullOrEmpty(a)) {
return Nullable. empty();
}
final Nullable res = N.findFirst(a, predicateForFirst);
return res.isPresent() ? res : N.findLast(a, predicateForLast);
}
/**
* Find first or last.
*
* @param
* @param
* @param
* @param c
* @param predicateForFirst
* @param predicateForLast
* @return the nullable
* @throws E the e
* @throws E2 the e2
*/
public static Nullable findFirstOrLast(final Collection extends T> c,
final Throwables.Predicate super T, E> predicateForFirst, final Throwables.Predicate super T, E2> predicateForLast) throws E, E2 {
if (N.isNullOrEmpty(c)) {
return Nullable. empty();
}
final Nullable res = N.findFirst(c, predicateForFirst);
return res.isPresent() ? res : N.findLast(c, predicateForLast);
}
/**
* Find first and last.
*
* @param
* @param
* @param a
* @param predicate
* @return the pair
* @throws E the e
*/
public static Pair, Nullable> findFirstAndLast(final T[] a, final Throwables.Predicate super T, E> predicate)
throws E {
return findFirstAndLast(a, predicate, predicate);
}
/**
* Find first and last.
*
* @param
* @param
* @param
* @param a
* @param predicateForFirst
* @param predicateForLast
* @return the pair
* @throws E the e
* @throws E2 the e2
*/
public static Pair, Nullable> findFirstAndLast(final T[] a,
final Throwables.Predicate super T, E> predicateForFirst, final Throwables.Predicate super T, E2> predicateForLast) throws E, E2 {
if (N.isNullOrEmpty(a)) {
return Pair.of(Nullable. empty(), Nullable. empty());
}
return Pair.of(N.findFirst(a, predicateForFirst), N.findLast(a, predicateForLast));
}
/**
* Find first and last.
*
* @param
* @param
* @param c
* @param predicate
* @return the pair
* @throws E the e
*/
public static Pair, Nullable> findFirstAndLast(final Collection extends T> c,
final Throwables.Predicate super T, E> predicate) throws E {
return findFirstAndLast(c, predicate, predicate);
}
/**
* Find first and last.
*
* @param
* @param
* @param
* @param c
* @param predicateForFirst
* @param predicateForLast
* @return the pair
* @throws E the e
* @throws E2 the e2
*/
public static Pair, Nullable> findFirstAndLast(final Collection extends T> c,
final Throwables.Predicate super T, E> predicateForFirst, final Throwables.Predicate super T, E2> predicateForLast) throws E, E2 {
if (N.isNullOrEmpty(c)) {
return Pair.of(Nullable. empty(), Nullable. empty());
}
return Pair.of(N.findFirst(c, predicateForFirst), N.findLast(c, predicateForLast));
}
// /**
// *
// * @param
// * @param
// * @param a
// * @param b
// * @return
// * @see N#crossJoin(Collection, Collection)
// * @deprecated replaced by {@code N.crossJoin(Collection, Collection)}
// */
// @Deprecated
// public static List> crossJoin(final Collection a, final Collection b) {
// return crossJoin(a, b, Fn. pair());
// }
//
// /**
// *
// * @param
// * @param
// * @param
// * @param
// * @param a
// * @param b
// * @param func
// * @return
// * @throws E
// * @see N#crossJoin(Collection, Collection, com.landawn.abacus.util.Throwables.BiFunction)
// * @deprecated replaced by {@code N.crossJoin(Collection, Collection, com.landawn.abacus.util.Try.BiFunction)}
// */
// @Deprecated
// public static List crossJoin(final Collection a, final Collection b,
// final Throwables.BiFunction super T, ? super U, R, E> func) throws E {
// N.checkArgNotNull(func, "func");
//
// final List result = new ArrayList<>(N.size(a) * N.size(b));
//
// if (N.isNullOrEmpty(a) || N.isNullOrEmpty(b)) {
// return result;
// }
//
// for (T ae : a) {
// for (U be : b) {
// result.add(func.apply(ae, be));
// }
// }
//
// return result;
// }
//
// /**
// * The time complexity is O(n + m) : n is the size of this Seq
and m is the size of specified collection b
.
// *
// * @param the generic type
// * @param the generic type
// * @param the element type
// * @param the generic type
// * @param a the a
// * @param b the b
// * @param leftKeyMapper the left key mapper
// * @param rightKeyMapper the right key mapper
// * @return the list
// * @throws E the e
// * @throws E2 the e2
// * @see N#innerJoin(Collection, Collection, com.landawn.abacus.util.Throwables.Function, com.landawn.abacus.util.Throwables.Function)
// * @see sql join
// * @deprecated replaced by {@code N.innerJoin(Collection, Collection, com.landawn.abacus.util.Try.Function, com.landawn.abacus.util.Try.Function)}
// */
// @Deprecated
// public static List> innerJoin(final Collection a, final Collection b,
// final Throwables.Function super T, ? extends K, E> leftKeyMapper, final Throwables.Function super U, ? extends K, E2> rightKeyMapper)
// throws E, E2 {
// final List> result = new ArrayList<>(N.min(9, N.size(a), N.size(b)));
//
// if (N.isNullOrEmpty(a) || N.isNullOrEmpty(b)) {
// return result;
// }
//
// final ListMultimap rightKeyMap = ListMultimap.from(b, rightKeyMapper);
//
// for (T left : a) {
// final List rights = rightKeyMap.get(leftKeyMapper.apply(left));
//
// if (N.notNullOrEmpty(rights)) {
// for (U right : rights) {
// result.add(Pair.of(left, right));
// }
// }
// }
//
// return result;
// }
//
// /**
// * The time complexity is O(n * m) : n is the size of this Seq
and m is the size of specified collection b
.
// *
// * @param the generic type
// * @param the generic type
// * @param the element type
// * @param a the a
// * @param b the b
// * @param predicate the predicate
// * @return the list
// * @throws E the e
// * @see N#innerJoin(Collection, Collection, com.landawn.abacus.util.Throwables.BiPredicate)
// * @see sql join
// * @deprecated replaced by {@code N.innerJoin(Collection, Collection, com.landawn.abacus.util.Try.BiPredicate)}
// */
// @Deprecated
// public static List> innerJoin(final Collection a, final Collection b,
// final Throwables.BiPredicate super T, ? super U, E> predicate) throws E {
// final List> result = new ArrayList<>(N.min(9, N.size(a), N.size(b)));
//
// if (N.isNullOrEmpty(a) || N.isNullOrEmpty(b)) {
// return result;
// }
//
// for (T left : a) {
// for (U right : b) {
// if (predicate.test(left, right)) {
// result.add(Pair.of(left, right));
// }
// }
// }
//
// return result;
// }
//
// /**
// * The time complexity is O(n + m) : n is the size of this Seq
and m is the size of specified collection b
.
// *
// * @param the generic type
// * @param the generic type
// * @param the element type
// * @param the generic type
// * @param a the a
// * @param b the b
// * @param leftKeyMapper the left key mapper
// * @param rightKeyMapper the right key mapper
// * @return the list
// * @throws E the e
// * @throws E2 the e2
// * @see N#fullJoin(Collection, Collection, com.landawn.abacus.util.Throwables.Function, com.landawn.abacus.util.Throwables.Function)
// * @see sql join
// * @deprecated replaced by {@code N.fullJoin(Collection, Collection, com.landawn.abacus.util.Try.Function, com.landawn.abacus.util.Try.Function)}
// */
// @Deprecated
// public static List> fullJoin(final Collection a, final Collection b,
// final Throwables.Function super T, ? extends K, E> leftKeyMapper, final Throwables.Function super U, ? extends K, E2> rightKeyMapper)
// throws E, E2 {
// final List> result = new ArrayList<>(N.max(9, N.size(a), N.size(b)));
//
// if (N.isNullOrEmpty(a)) {
// for (T left : a) {
// result.add(Pair.of(left, (U) null));
// }
// } else if (N.isNullOrEmpty(b)) {
// for (U right : b) {
// result.add(Pair.of((T) null, right));
// }
// } else {
// final ListMultimap rightKeyMap = ListMultimap.from(b, rightKeyMapper);
// final Map joinedRights = new IdentityHashMap<>();
//
// for (T left : a) {
// final List rights = rightKeyMap.get(leftKeyMapper.apply(left));
//
// if (N.notNullOrEmpty(rights)) {
// for (U right : rights) {
// result.add(Pair.of(left, right));
// joinedRights.put(right, right);
// }
// } else {
// result.add(Pair.of(left, (U) null));
// }
// }
//
// for (U right : b) {
// if (joinedRights.containsKey(right) == false) {
// result.add(Pair.of((T) null, right));
// }
// }
// }
//
// return result;
// }
//
// /**
// * The time complexity is O(n * m) : n is the size of this Seq
and m is the size of specified collection b
.
// *
// * @param the generic type
// * @param the generic type
// * @param the element type
// * @param a the a
// * @param b the b
// * @param predicate the predicate
// * @return the list
// * @throws E the e
// * @see N#fullJoin(Collection, Collection, com.landawn.abacus.util.Throwables.BiPredicate)
// * @see sql join
// * @deprecated replaced by {@code N.fullJoin(Collection, Collection, com.landawn.abacus.util.Try.BiPredicate)}
// */
// @Deprecated
// public static List