Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.landawn.abacus.util.stream.Collectors Maven / Gradle / Ivy
/*
* Copyright (C) 2016, 2017, 2018, 2019 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.stream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.ConcurrentModificationException;
import java.util.Deque;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collector.Characteristics;
import com.landawn.abacus.DataSet;
import com.landawn.abacus.exception.DuplicatedResultException;
import com.landawn.abacus.util.Array;
import com.landawn.abacus.util.BiMap;
import com.landawn.abacus.util.BooleanList;
import com.landawn.abacus.util.ByteList;
import com.landawn.abacus.util.ByteSummaryStatistics;
import com.landawn.abacus.util.CharList;
import com.landawn.abacus.util.CharSummaryStatistics;
import com.landawn.abacus.util.ContinuableFuture;
import com.landawn.abacus.util.DoubleList;
import com.landawn.abacus.util.DoubleSummaryStatistics;
import com.landawn.abacus.util.FloatList;
import com.landawn.abacus.util.FloatSummaryStatistics;
import com.landawn.abacus.util.Fn;
import com.landawn.abacus.util.Fn.BiConsumers;
import com.landawn.abacus.util.Fn.BinaryOperators;
import com.landawn.abacus.util.Fn.Suppliers;
import com.landawn.abacus.util.ImmutableList;
import com.landawn.abacus.util.ImmutableMap;
import com.landawn.abacus.util.ImmutableSet;
import com.landawn.abacus.util.IntList;
import com.landawn.abacus.util.IntSummaryStatistics;
import com.landawn.abacus.util.Joiner;
import com.landawn.abacus.util.KahanSummation;
import com.landawn.abacus.util.ListMultimap;
import com.landawn.abacus.util.LongList;
import com.landawn.abacus.util.LongMultiset;
import com.landawn.abacus.util.LongSummaryStatistics;
import com.landawn.abacus.util.Multimap;
import com.landawn.abacus.util.Multiset;
import com.landawn.abacus.util.MutableBoolean;
import com.landawn.abacus.util.MutableLong;
import com.landawn.abacus.util.N;
import com.landawn.abacus.util.ObjIterator;
import com.landawn.abacus.util.Pair;
import com.landawn.abacus.util.ShortList;
import com.landawn.abacus.util.ShortSummaryStatistics;
import com.landawn.abacus.util.Tuple;
import com.landawn.abacus.util.Tuple.Tuple2;
import com.landawn.abacus.util.Tuple.Tuple3;
import com.landawn.abacus.util.Tuple.Tuple4;
import com.landawn.abacus.util.Tuple.Tuple5;
import com.landawn.abacus.util.u.Holder;
import com.landawn.abacus.util.u.Optional;
import com.landawn.abacus.util.u.OptionalDouble;
import com.landawn.abacus.util.u.OptionalInt;
import com.landawn.abacus.util.u.OptionalLong;
import com.landawn.abacus.util.function.BiConsumer;
import com.landawn.abacus.util.function.BiFunction;
import com.landawn.abacus.util.function.BinaryOperator;
import com.landawn.abacus.util.function.BooleanSupplier;
import com.landawn.abacus.util.function.Consumer;
import com.landawn.abacus.util.function.Function;
import com.landawn.abacus.util.function.IntFunction;
import com.landawn.abacus.util.function.Predicate;
import com.landawn.abacus.util.function.Supplier;
import com.landawn.abacus.util.function.ToByteFunction;
import com.landawn.abacus.util.function.ToCharFunction;
import com.landawn.abacus.util.function.ToDoubleFunction;
import com.landawn.abacus.util.function.ToFloatFunction;
import com.landawn.abacus.util.function.ToIntFunction;
import com.landawn.abacus.util.function.ToLongFunction;
import com.landawn.abacus.util.function.ToShortFunction;
import com.landawn.abacus.util.function.TriFunction;
/**
*
* @see {@code java.util.stream.Collectors}
*
*/
public abstract class Collectors {
static final Object NONE = new Object();
@Deprecated
static final Set CH_CONCURRENT_ID = Collections
.unmodifiableSet(EnumSet.of(Characteristics.CONCURRENT, Characteristics.UNORDERED, Characteristics.IDENTITY_FINISH));
@Deprecated
static final Set CH_CONCURRENT_NOID = Collections.unmodifiableSet(EnumSet.of(Characteristics.CONCURRENT, Characteristics.UNORDERED));
static final Set CH_UNORDERED_ID = Collections.unmodifiableSet(EnumSet.of(Characteristics.UNORDERED, Characteristics.IDENTITY_FINISH));
static final Set CH_UNORDERED_NOID = Collections.unmodifiableSet(EnumSet.of(Characteristics.UNORDERED));
static final Set CH_ID = Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH));
static final Set CH_NOID = Collections.emptySet();
// ============================================================================================================
static final Function, ImmutableList> ImmutableList_Finisher = new Function, ImmutableList>() {
@Override
public ImmutableList apply(List t) {
return ImmutableList.of(t);
}
};
static final Function, ImmutableSet> ImmutableSet_Finisher = new Function, ImmutableSet>() {
@Override
public ImmutableSet apply(Set t) {
return ImmutableSet.of(t);
}
};
static final Function, ImmutableMap> ImmutableMap_Finisher = new Function, ImmutableMap>() {
@Override
public ImmutableMap apply(Map t) {
return ImmutableMap.of(t);
}
};
static final BiConsumer, Object> Multiset_Accumulator = new BiConsumer, Object>() {
@Override
public void accept(Multiset c, Object t) {
c.add(t);
}
};
static final BinaryOperator> Multiset_Combiner = new BinaryOperator>() {
@Override
public Multiset apply(Multiset a, Multiset b) {
a.addAll(b);
return a;
}
};
static final BiConsumer, Object> LongMultiset_Accumulator = new BiConsumer, Object>() {
@Override
public void accept(LongMultiset c, Object t) {
c.add(t);
}
};
static final BinaryOperator> LongMultiset_Combiner = new BinaryOperator>() {
@Override
public LongMultiset apply(LongMultiset a, LongMultiset b) {
a.addAll(b);
return a;
}
};
static final BiConsumer BooleanList_Accumulator = new BiConsumer() {
@Override
public void accept(BooleanList c, Boolean t) {
c.add(t.booleanValue());
}
};
static final BinaryOperator BooleanList_Combiner = new BinaryOperator() {
@Override
public BooleanList apply(BooleanList a, BooleanList b) {
a.addAll(b);
return a;
}
};
static final Function BooleanArray_Finisher = new Function() {
@Override
public boolean[] apply(BooleanList t) {
return t.trimToSize().array();
}
};
static final BiConsumer CharList_Accumulator = new BiConsumer() {
@Override
public void accept(CharList c, Character t) {
c.add(t.charValue());
}
};
static final BinaryOperator CharList_Combiner = new BinaryOperator() {
@Override
public CharList apply(CharList a, CharList b) {
a.addAll(b);
return a;
}
};
static final Function CharArray_Finisher = new Function() {
@Override
public char[] apply(CharList t) {
return t.trimToSize().array();
}
};
static final BiConsumer ByteList_Accumulator = new BiConsumer() {
@Override
public void accept(ByteList c, Byte t) {
c.add(t.byteValue());
}
};
static final BinaryOperator ByteList_Combiner = new BinaryOperator() {
@Override
public ByteList apply(ByteList a, ByteList b) {
if (a.size() >= b.size()) {
a.addAll(b);
return a;
} else {
b.addAll(a);
return b;
}
}
};
static final Function ByteArray_Finisher = new Function() {
@Override
public byte[] apply(ByteList t) {
return t.trimToSize().array();
}
};
static final BiConsumer ShortList_Accumulator = new BiConsumer() {
@Override
public void accept(ShortList c, Short t) {
c.add(t.shortValue());
}
};
static final BinaryOperator ShortList_Combiner = new BinaryOperator() {
@Override
public ShortList apply(ShortList a, ShortList b) {
if (a.size() >= b.size()) {
a.addAll(b);
return a;
} else {
b.addAll(a);
return b;
}
}
};
static final Function ShortArray_Finisher = new Function() {
@Override
public short[] apply(ShortList t) {
return t.trimToSize().array();
}
};
static final BiConsumer IntList_Accumulator = new BiConsumer() {
@Override
public void accept(IntList c, Integer t) {
c.add(t.intValue());
}
};
static final BinaryOperator IntList_Combiner = new BinaryOperator() {
@Override
public IntList apply(IntList a, IntList b) {
if (a.size() >= b.size()) {
a.addAll(b);
return a;
} else {
b.addAll(a);
return b;
}
}
};
static final Function IntArray_Finisher = new Function() {
@Override
public int[] apply(IntList t) {
return t.trimToSize().array();
}
};
static final BiConsumer LongList_Accumulator = new BiConsumer() {
@Override
public void accept(LongList c, Long t) {
c.add(t.longValue());
}
};
static final BinaryOperator LongList_Combiner = new BinaryOperator() {
@Override
public LongList apply(LongList a, LongList b) {
if (a.size() >= b.size()) {
a.addAll(b);
return a;
} else {
b.addAll(a);
return b;
}
}
};
static final Function LongArray_Finisher = new Function() {
@Override
public long[] apply(LongList t) {
return t.trimToSize().array();
}
};
static final BiConsumer FloatList_Accumulator = new BiConsumer() {
@Override
public void accept(FloatList c, Float t) {
c.add(t.floatValue());
}
};
static final BinaryOperator FloatList_Combiner = new BinaryOperator() {
@Override
public FloatList apply(FloatList a, FloatList b) {
if (a.size() >= b.size()) {
a.addAll(b);
return a;
} else {
b.addAll(a);
return b;
}
}
};
static final Function FloatArray_Finisher = new Function() {
@Override
public float[] apply(FloatList t) {
return t.trimToSize().array();
}
};
static final BiConsumer DoubleList_Accumulator = new BiConsumer() {
@Override
public void accept(DoubleList c, Double t) {
c.add(t.doubleValue());
}
};
static final BinaryOperator DoubleList_Combiner = new BinaryOperator() {
@Override
public DoubleList apply(DoubleList a, DoubleList b) {
if (a.size() >= b.size()) {
a.addAll(b);
return a;
} else {
b.addAll(a);
return b;
}
}
};
static final Function DoubleArray_Finisher = new Function() {
@Override
public double[] apply(DoubleList t) {
return t.trimToSize().array();
}
};
static final BiConsumer Joiner_Accumulator = new BiConsumer() {
@Override
public void accept(Joiner a, CharSequence t) {
a.append(t);
}
};
static final BinaryOperator Joiner_Combiner = new BinaryOperator() {
@Override
public Joiner apply(Joiner a, Joiner b) {
if (a.length() > b.length()) {
a.merge(b);
b.close();
return a;
} else {
b.merge(a);
a.close();
return b;
}
}
};
static final Function Joiner_Finisher = new Function() {
@Override
public String apply(Joiner a) {
return a.toString();
}
};
static final Function Counting_Accumulator = new Function() {
@Override
public Long apply(Object t) {
return 1L;
}
};
static final BinaryOperator Counting_Combiner = new BinaryOperator() {
@Override
public Long apply(Long a, Long b) {
return a.longValue() + b.longValue();
}
};
static final Function CountingInt_Accumulator = new Function() {
@Override
public Integer apply(Object t) {
return 1;
}
};
static final BinaryOperator CountingInt_Combiner = new BinaryOperator() {
@Override
public Integer apply(Integer a, Integer b) {
return a.intValue() + b.intValue();
}
};
static final Supplier SummingInt_Supplier = new Supplier() {
@Override
public long[] get() {
return new long[1];
}
};
static final BinaryOperator SummingInt_Combiner = new BinaryOperator() {
@Override
public long[] apply(long[] a, long[] b) {
a[0] += b[0];
return a;
}
};
static final Function SummingInt_Finisher = new Function() {
@Override
public Integer apply(long[] a) {
return N.toIntExact(a[0]);
}
};
static final Supplier SummingInt_Supplier_2 = new Supplier() {
@Override
public long[] get() {
return new long[2];
}
};
static final BinaryOperator SummingInt_Combiner_2 = new BinaryOperator() {
@Override
public long[] apply(long[] a, long[] b) {
a[0] += b[0];
a[1] += b[1];
return a;
}
};
static final Function SummingInt_Finisher_2 = new Function() {
@Override
public OptionalInt apply(long[] a) {
return a[1] == 0 ? OptionalInt.empty() : OptionalInt.of(N.toIntExact(a[0]));
}
};
static final Supplier SummingLong_Supplier = new Supplier() {
@Override
public long[] get() {
return new long[1];
}
};
static final BinaryOperator SummingLong_Combiner = new BinaryOperator() {
@Override
public long[] apply(long[] a, long[] b) {
a[0] += b[0];
return a;
}
};
static final Function SummingLong_Finisher = new Function() {
@Override
public Long apply(long[] a) {
return a[0];
}
};
static final Supplier SummingLong_Supplier_2 = new Supplier() {
@Override
public long[] get() {
return new long[2];
}
};
static final BinaryOperator SummingLong_Combiner_2 = new BinaryOperator() {
@Override
public long[] apply(long[] a, long[] b) {
a[0] += b[0];
a[1] += b[1];
return a;
}
};
static final Function SummingLong_Finisher_2 = new Function() {
@Override
public OptionalLong apply(long[] a) {
return a[1] == 0 ? OptionalLong.empty() : OptionalLong.of(a[0]);
}
};
static final Supplier SummingDouble_Supplier = new Supplier() {
@Override
public KahanSummation get() {
return new KahanSummation();
}
};
static final BinaryOperator SummingDouble_Combiner = new BinaryOperator() {
@Override
public KahanSummation apply(KahanSummation a, KahanSummation b) {
a.combine(b);
return a;
}
};
static final Function SummingDouble_Finisher = new Function() {
@Override
public Double apply(KahanSummation a) {
return a.sum();
}
};
static final Supplier SummingDouble_Supplier_2 = new Supplier() {
@Override
public KahanSummation get() {
return new KahanSummation();
}
};
static final BinaryOperator SummingDouble_Combiner_2 = new BinaryOperator() {
@Override
public KahanSummation apply(KahanSummation a, KahanSummation b) {
a.combine(b);
return a;
}
};
static final Function SummingDouble_Finisher_2 = new Function() {
@Override
public OptionalDouble apply(KahanSummation a) {
return a.count() == 0 ? OptionalDouble.empty() : OptionalDouble.of(a.sum());
}
};
static final Supplier SummingBigInteger_Supplier = new Supplier() {
@Override
public BigInteger[] get() {
return new BigInteger[] { BigInteger.ZERO };
}
};
static final BinaryOperator SummingBigInteger_Combiner = new BinaryOperator() {
@Override
public BigInteger[] apply(BigInteger[] a, BigInteger[] b) {
a[0] = a[0].add(b[0]);
return a;
}
};
static final Function SummingBigInteger_Finisher = new Function() {
@Override
public BigInteger apply(BigInteger[] a) {
return a[0];
}
};
static final Supplier SummingBigDecimal_Supplier = new Supplier() {
@Override
public BigDecimal[] get() {
return new BigDecimal[] { BigDecimal.ZERO };
}
};
static final BinaryOperator SummingBigDecimal_Combiner = new BinaryOperator() {
@Override
public BigDecimal[] apply(BigDecimal[] a, BigDecimal[] b) {
a[0] = a[0].add(b[0]);
return a;
}
};
static final Function SummingBigDecimal_Finisher = new Function() {
@Override
public BigDecimal apply(BigDecimal[] a) {
return a[0];
}
};
static final Supplier AveragingInt_Supplier = new Supplier() {
@Override
public long[] get() {
return new long[2];
}
};
static final BinaryOperator AveragingInt_Combiner = new BinaryOperator() {
@Override
public long[] apply(long[] a, long[] b) {
a[0] += b[0];
a[1] += b[1];
return a;
}
};
static final Function AveragingInt_Finisher = new Function() {
@Override
public Double apply(long[] a) {
return a[1] == 0 ? 0d : ((double) a[0]) / a[1];
}
};
static final Function AveragingInt_Finisher_2 = new Function() {
@Override
public OptionalDouble apply(long[] a) {
if (a[1] == 0) {
return OptionalDouble.empty();
} else {
return OptionalDouble.of(((double) a[0]) / a[1]);
}
}
};
static final Supplier AveragingLong_Supplier = new Supplier() {
@Override
public long[] get() {
return new long[2];
}
};
static final BinaryOperator AveragingLong_Combiner = new BinaryOperator() {
@Override
public long[] apply(long[] a, long[] b) {
a[0] += b[0];
a[1] += b[1];
return a;
}
};
static final Function AveragingLong_Finisher = new Function() {
@Override
public Double apply(long[] a) {
return a[1] == 0 ? 0d : ((double) a[0]) / a[1];
}
};
static final Function AveragingLong_Finisher_2 = new Function() {
@Override
public OptionalDouble apply(long[] a) {
if (a[1] == 0) {
return OptionalDouble.empty();
} else {
return OptionalDouble.of(((double) a[0]) / a[1]);
}
}
};
static final Supplier AveragingDouble_Supplier = new Supplier() {
@Override
public KahanSummation get() {
return new KahanSummation();
}
};
static final BinaryOperator AveragingDouble_Combiner = new BinaryOperator() {
@Override
public KahanSummation apply(KahanSummation a, KahanSummation b) {
a.combine(b);
return a;
}
};
static final Function AveragingDouble_Finisher = new Function() {
@Override
public Double apply(KahanSummation a) {
return a.average().orElse(0);
}
};
static final Function AveragingDouble_Finisher_2 = new Function() {
@Override
public OptionalDouble apply(KahanSummation a) {
return a.average();
}
};
static final Supplier> AveragingBigInteger_Supplier = new Supplier>() {
@Override
public Pair get() {
return Pair.of(BigInteger.ZERO, MutableLong.of(0));
}
};
static final BinaryOperator> AveragingBigInteger_Combiner = new BinaryOperator>() {
@Override
public Pair apply(Pair a, Pair b) {
a.setLeft(a.left.add(b.left));
a.right.add(b.right.value());
return a;
}
};
static final Function, BigDecimal> AveragingBigInteger_Finisher = new Function, BigDecimal>() {
@Override
public BigDecimal apply(Pair a) {
return a.right.value() == 0 ? BigDecimal.ZERO : new BigDecimal(a.left).divide(new BigDecimal(a.right.value()));
}
};
static final Supplier> AveragingBigDecimal_Supplier = new Supplier>() {
@Override
public Pair get() {
return Pair.of(BigDecimal.ZERO, MutableLong.of(0));
}
};
static final BinaryOperator> AveragingBigDecimal_Combiner = new BinaryOperator>() {
@Override
public Pair apply(Pair a, Pair b) {
a.setLeft(a.left.add(b.left));
a.right.add(b.right.value());
return a;
}
};
static final Function, BigDecimal> AveragingBigDecimal_Finisher = new Function, BigDecimal>() {
@Override
public BigDecimal apply(Pair a) {
return a.right.value() == 0 ? BigDecimal.ZERO : a.left.divide(new BigDecimal(a.right.value()));
}
};
static final Supplier SummarizingChar_Supplier = new Supplier() {
@Override
public CharSummaryStatistics get() {
return new CharSummaryStatistics();
}
};
static final BinaryOperator SummarizingChar_Combiner = new BinaryOperator() {
@Override
public CharSummaryStatistics apply(CharSummaryStatistics a, CharSummaryStatistics b) {
a.combine(b);
return a;
}
};
static final Supplier SummarizingByte_Supplier = new Supplier() {
@Override
public ByteSummaryStatistics get() {
return new ByteSummaryStatistics();
}
};
static final BinaryOperator SummarizingByte_Combiner = new BinaryOperator() {
@Override
public ByteSummaryStatistics apply(ByteSummaryStatistics a, ByteSummaryStatistics b) {
a.combine(b);
return a;
}
};
static final Supplier SummarizingShort_Supplier = new Supplier() {
@Override
public ShortSummaryStatistics get() {
return new ShortSummaryStatistics();
}
};
static final BinaryOperator SummarizingShort_Combiner = new BinaryOperator() {
@Override
public ShortSummaryStatistics apply(ShortSummaryStatistics a, ShortSummaryStatistics b) {
a.combine(b);
return a;
}
};
static final Supplier SummarizingInt_Supplier = new Supplier() {
@Override
public IntSummaryStatistics get() {
return new IntSummaryStatistics();
}
};
static final BinaryOperator SummarizingInt_Combiner = new BinaryOperator() {
@Override
public IntSummaryStatistics apply(IntSummaryStatistics a, IntSummaryStatistics b) {
a.combine(b);
return a;
}
};
static final Supplier SummarizingLong_Supplier = new Supplier() {
@Override
public LongSummaryStatistics get() {
return new LongSummaryStatistics();
}
};
static final BinaryOperator SummarizingLong_Combiner = new BinaryOperator() {
@Override
public LongSummaryStatistics apply(LongSummaryStatistics a, LongSummaryStatistics b) {
a.combine(b);
return a;
}
};
static final Supplier SummarizingFloat_Supplier = new Supplier() {
@Override
public FloatSummaryStatistics get() {
return new FloatSummaryStatistics();
}
};
static final BinaryOperator SummarizingFloat_Combiner = new BinaryOperator() {
@Override
public FloatSummaryStatistics apply(FloatSummaryStatistics a, FloatSummaryStatistics b) {
a.combine(b);
return a;
}
};
static final Supplier SummarizingDouble_Supplier = new Supplier() {
@Override
public DoubleSummaryStatistics get() {
return new DoubleSummaryStatistics();
}
};
static final BinaryOperator SummarizingDouble_Combiner = new BinaryOperator() {
@Override
public DoubleSummaryStatistics apply(DoubleSummaryStatistics a, DoubleSummaryStatistics b) {
a.combine(b);
return a;
}
};
static final Function, Object> Reducing_Finisher_0 = new Function, Object>() {
@Override
public Object apply(Holder a) {
return a.value();
}
};
static final BiConsumer, Object> Reducing_Accumulator = new BiConsumer, Object>() {
@Override
public void accept(OptHolder a, Object t) {
a.accept(t);
}
};
static final BinaryOperator> Reducing_Combiner = new BinaryOperator>() {
@Override
public OptHolder apply(OptHolder a, OptHolder b) {
if (b.present) {
a.accept(b.value);
}
return a;
}
};
static final Function, Optional> Reducing_Finisher = new Function, Optional>() {
@Override
public Optional apply(OptHolder a) {
return a.present ? Optional.of(a.value) : (Optional) Optional.empty();
}
};
static final BiConsumer, Object> Reducing_Accumulator_2 = new BiConsumer, Object>() {
@Override
public void accept(MappingOptHolder a, Object t) {
a.accept(t);
}
};
static final BinaryOperator> Reducing_Combiner_2 = new BinaryOperator>() {
@Override
public MappingOptHolder apply(MappingOptHolder a, MappingOptHolder b) {
if (b.present) {
if (a.present) {
a.value = a.op.apply(a.value, b.value);
} else {
a.value = b.value;
a.present = true;
}
}
return a;
}
};
static final Function, Optional