com.github.phantomthief.util.MoreCollectors Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of more-lambdas Show documentation
Show all versions of more-lambdas Show documentation
Some useful lambda implements for Java 8.
package com.github.phantomthief.util;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Stream.of;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.ToIntFunction;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.carrotsearch.hppc.IntArrayList;
import com.carrotsearch.hppc.IntHashSet;
import com.carrotsearch.hppc.IntObjectHashMap;
import com.carrotsearch.hppc.LongArrayList;
import com.carrotsearch.hppc.LongHashSet;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multimap;
/**
* @author w.vela
*/
public final class MoreCollectors {
public static final Set CH_ID = Collections
.unmodifiableSet(EnumSet.of(Collector.Characteristics.IDENTITY_FINISH));
public static final Set CH_NOID = Collections.emptySet();
private MoreCollectors() {
throw new UnsupportedOperationException();
}
public static Collector toIntList() {
return new CollectorImpl<>(IntArrayList::new, IntArrayList::add, (left, right) -> {
left.addAll(right);
return left;
}, CH_ID);
}
public static Collector toLongList() {
return new CollectorImpl<>(LongArrayList::new, LongArrayList::add, (left, right) -> {
left.addAll(right);
return left;
}, CH_ID);
}
public static Collector toIntSet() {
return new CollectorImpl<>(IntHashSet::new, IntHashSet::add, (left, right) -> {
left.addAll(right);
return left;
}, CH_ID);
}
public static Collector toLongSet() {
return new CollectorImpl<>(LongHashSet::new, LongHashSet::add, (left, right) -> {
left.addAll(right);
return left;
}, CH_ID);
}
public static Collector, IntObjectHashMap> toIntMap(
ToIntFunction super T> keyMapper, Function super T, ? extends U> valueMapper) {
BiConsumer, T> accumulator = (map, element) -> map
.put(keyMapper.applyAsInt(element), valueMapper.apply(element));
return new CollectorImpl<>(IntObjectHashMap::new, accumulator, (m1, m2) -> {
m1.putAll(m2);
return m1;
}, CH_ID);
}
public static Collector, ?, Map> toMap() {
return Collectors.toMap(Entry::getKey, Entry::getValue);
}
@Deprecated
public static Collector> toMultiset(
Function super T, ? extends K> elementMapper, ToIntFunction super T> countMapper) {
BiConsumer, T> accumulator = (set, element) -> set
.add(elementMapper.apply(element), countMapper.applyAsInt(element));
BinaryOperator> finisher = (m1, m2) -> {
m1.addAll(m2);
return m1;
};
return new CollectorImpl<>(HashMultiset::create, accumulator, finisher, CH_ID);
}
/**
* use {@link com.google.common.collect.Multimaps#toMultimap(Function, Function, Supplier)} if using guava 21.0+
*/
@Deprecated
public static > Collector toMultimap(
Function super T, ? extends K> keyMapper,
Function super T, ? extends U> valueMapper, Supplier supplier) {
BiConsumer accumulator = (multimap, element) -> multimap.put(keyMapper.apply(element),
valueMapper.apply(element));
BinaryOperator finisher = (m1, m2) -> {
m1.putAll(m2);
return m1;
};
return new CollectorImpl<>(supplier, accumulator, finisher, CH_ID);
}
public static > Collector toMap(
Function super T, ? extends K> keyMapper,
Function super T, ? extends U> valueMapper, Supplier supplier) {
return toMap(keyMapper, valueMapper, throwingMerger(), supplier);
}
private static > Collector toMap(
Function super T, ? extends K> keyMapper,
Function super T, ? extends U> valueMapper, BinaryOperator mergeFunction,
Supplier mapSupplier) {
BiConsumer accumulator = (map, element) -> map.merge(keyMapper.apply(element),
valueMapper.apply(element), mergeFunction);
return new CollectorImpl<>(mapSupplier, accumulator, mapMerger(mergeFunction), CH_ID);
}
public static Collector>> groupingByAllowNullKey(
Function super T, ? extends K> classifier) {
return groupingByAllowNullKey(classifier, toList());
}
public static Collector> groupingByAllowNullKey(
Function super T, ? extends K> classifier,
Collector super T, A, D> downstream) {
return groupingByAllowNullKey(classifier, HashMap::new, downstream);
}
/**
* allow null key for mapFactory, nothing else changes
*/
public static > Collector groupingByAllowNullKey(
Function super T, ? extends K> classifier,
Supplier mapFactory,
Collector super T, A, D> downstream) {
Supplier downstreamSupplier = downstream.supplier();
BiConsumer downstreamAccumulator = downstream.accumulator();
BiConsumer
© 2015 - 2024 Weber Informatics LLC | Privacy Policy