本文整理汇总了Java中java.util.stream.Collector.Characteristics类的典型用法代码示例。如果您正苦于以下问题:Java Characteristics类的具体用法?Java Characteristics怎么用?Java Characteristics使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Characteristics类属于java.util.stream.Collector包,在下文中一共展示了Characteristics类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: collapse
import java.util.stream.Collector.Characteristics; //导入依赖的package包/类
/**
* Perform a partial mutable reduction using the supplied {@link Collector}
* on a series of adjacent elements.
*
* <p>
* This is a <a href="package-summary.html#StreamOps">quasi-intermediate</a>
* partial reduction operation.
*
* @param <R> the type of the elements in the resulting stream
* @param <A> the intermediate accumulation type of the {@code Collector}
* @param collapsible a non-interfering, stateless predicate to apply to the
* pair of adjacent elements of the input stream which returns true
* for elements which should be collected together.
* @param collector a {@code Collector} which is used to combine the
* adjacent elements.
* @return the new stream
* @since 0.3.6
*/
public <R, A> StreamEx<R> collapse(BiPredicate<? super T, ? super T> collapsible,
Collector<? super T, A, R> collector) {
Supplier<A> supplier = collector.supplier();
BiConsumer<A, ? super T> accumulator = collector.accumulator();
StreamEx<A> stream = collapseInternal(collapsible, t -> {
A acc = supplier.get();
accumulator.accept(acc, t);
return acc;
}, (acc, t) -> {
accumulator.accept(acc, t);
return acc;
}, collector.combiner());
if (collector.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)) {
@SuppressWarnings("unchecked")
StreamEx<R> result = (StreamEx<R>) stream;
return result;
}
return stream.map(collector.finisher());
}
示例2: grouping
import java.util.stream.Collector.Characteristics; //导入依赖的package包/类
@SuppressWarnings("unchecked")
static <K, D, A, M extends Map<K, D>> PartialCollector<Map<K, A>, M> grouping(Supplier<M> mapFactory,
Collector<?, A, D> downstream) {
BinaryOperator<A> downstreamMerger = downstream.combiner();
BiConsumer<Map<K, A>, Map<K, A>> merger = (map1, map2) -> {
for (Map.Entry<K, A> e : map2.entrySet())
map1.merge(e.getKey(), e.getValue(), downstreamMerger);
};
if (downstream.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)) {
return (PartialCollector<Map<K, A>, M>) new PartialCollector<>((Supplier<Map<K, A>>) mapFactory,
merger, Function.identity(), ID_CHARACTERISTICS);
}
Function<A, D> downstreamFinisher = downstream.finisher();
return new PartialCollector<>((Supplier<Map<K, A>>) mapFactory, merger, map -> {
map.replaceAll((k, v) -> ((Function<A, A>) downstreamFinisher).apply(v));
return (M) map;
}, NO_CHARACTERISTICS);
}
示例3: CollectorImpl
import java.util.stream.Collector.Characteristics; //导入依赖的package包/类
CollectorImpl(Supplier<A> supplier, BiConsumer<A, T> accumulator, BinaryOperator<A> combiner, Function<A, R> finisher,
Set<Characteristics> characteristics) {
this.supplier = supplier;
this.accumulator = accumulator;
this.combiner = combiner;
this.finisher = finisher;
this.characteristics = characteristics;
}
示例4: collectingAndThen
import java.util.stream.Collector.Characteristics; //导入依赖的package包/类
/**
* Adapts a {@code Collector} to perform an additional finishing
* transformation. For example, one could adapt the {@link #toList()}
* collector to always produce an immutable list with:
* <pre>{@code
* List<String> people
* = people.stream().collect(collectingAndThen(toList(), Collections::unmodifiableList));
* }</pre>
*
* @param <T> the type of the input elements
* @param <A> intermediate accumulation type of the downstream collector
* @param <R> result type of the downstream collector
* @param <RR> result type of the resulting collector
* @param downstream a collector
* @param finisher a function to be applied to the final result of the downstream collector
* @return a collector which performs the action of the downstream collector,
* followed by an additional finishing step
*/
public static <T, A, R, RR> Collector<T, A, RR> collectingAndThen(final Collector<T, A, R> downstream, final Function<R, RR> finisher) {
Objects.requireNonNull(finisher);
final Function<A, R> downstreamFinisher = downstream.finisher();
final Function<A, RR> thenFinisher = new Function<A, RR>() {
@Override
public RR apply(A t) {
return finisher.apply(downstreamFinisher.apply(t));
}
};
Set<Collector.Characteristics> characteristics = downstream.characteristics();
if (characteristics.contains(Collector.Characteristics.IDENTITY_FINISH)) {
if (characteristics.size() == 1)
characteristics = Collectors.CH_NOID;
else {
characteristics = EnumSet.copyOf(characteristics);
characteristics.remove(Collector.Characteristics.IDENTITY_FINISH);
characteristics = Collections.unmodifiableSet(characteristics);
}
}
return new CollectorImpl<>(downstream.supplier(), downstream.accumulator(), downstream.combiner(), thenFinisher, characteristics);
}
示例5: collect
import java.util.stream.Collector.Characteristics; //导入依赖的package包/类
/**
* {@inheritDoc}
*
* <p>
* If special <a
* href="package-summary.html#ShortCircuitReduction">short-circuiting
* collector</a> is passed, this operation becomes short-circuiting as well.
*/
@Override
public <R, A> R collect(Collector<? super T, A, R> collector) {
Predicate<A> finished = finished(collector);
if (finished != null) {
BiConsumer<A, ? super T> acc = collector.accumulator();
BinaryOperator<A> combiner = collector.combiner();
Spliterator<T> spliterator = spliterator();
if (!isParallel()) {
A a = collector.supplier().get();
if (!finished.test(a)) {
try {
// forEachRemaining can be much faster
// and take much less memory than tryAdvance for certain
// spliterators
spliterator.forEachRemaining(e -> {
acc.accept(a, e);
if (finished.test(a))
throw new CancelException();
});
} catch (CancelException ex) {
// ignore
}
}
return collector.finisher().apply(a);
}
Spliterator<A> spltr;
if (!spliterator.hasCharacteristics(Spliterator.ORDERED)
|| collector.characteristics().contains(Characteristics.UNORDERED)) {
spltr = new UnorderedCancellableSpliterator<>(spliterator, collector.supplier(), acc, combiner,
finished);
} else {
spltr = new OrderedCancellableSpliterator<>(spliterator, collector.supplier(), acc, combiner, finished);
}
return collector.finisher().apply(
new StreamEx<>(StreamSupport.stream(spltr, true), context).findFirst().get());
}
return rawCollect(collector);
}
示例6: flatMapping
import java.util.stream.Collector.Characteristics; //导入依赖的package包/类
/**
* Adapts a {@code Collector} accepting elements of type {@code U} to one
* accepting elements of type {@code T} by applying a flat mapping function
* to each input element before accumulation. The flat mapping function maps
* an input element to a {@link Stream stream} covering zero or more output
* elements that are then accumulated downstream. Each mapped stream is
* {@link java.util.stream.BaseStream#close() closed} after its contents
* have been placed downstream. (If a mapped stream is {@code null} an empty
* stream is used, instead.)
*
* <p>
* This method is similar to {@code Collectors.flatMapping} method which
* appears in JDK 9. However when downstream collector is
* <a href="package-summary.html#ShortCircuitReduction">short-circuiting</a>
* , this method will also return a short-circuiting collector.
*
* @param <T> the type of the input elements
* @param <U> type of elements accepted by downstream collector
* @param <A> intermediate accumulation type of the downstream collector
* @param <R> result type of collector
* @param mapper a function to be applied to the input elements, which
* returns a stream of results
* @param downstream a collector which will receive the elements of the
* stream returned by mapper
* @return a collector which applies the mapping function to the input
* elements and provides the flat mapped results to the downstream
* collector
* @since 0.4.1
*/
public static <T, U, A, R> Collector<T, ?, R> flatMapping(Function<? super T, ? extends Stream<? extends U>> mapper,
Collector<? super U, A, R> downstream) {
BiConsumer<A, ? super U> downstreamAccumulator = downstream.accumulator();
Predicate<A> finished = finished(downstream);
if (finished != null) {
return new CancellableCollectorImpl<>(downstream.supplier(), (acc, t) -> {
if (finished.test(acc))
return;
try (Stream<? extends U> stream = mapper.apply(t)) {
if (stream != null) {
stream.spliterator().forEachRemaining(u -> {
downstreamAccumulator.accept(acc, u);
if (finished.test(acc))
throw new CancelException();
});
}
} catch (CancelException ex) {
// ignore
}
}, downstream.combiner(), downstream.finisher(), finished, downstream.characteristics());
}
return Collector.of(downstream.supplier(), (acc, t) -> {
try (Stream<? extends U> stream = mapper.apply(t)) {
if (stream != null) {
stream.spliterator().forEachRemaining(u -> downstreamAccumulator.accept(acc, u));
}
}
}, downstream.combiner(), downstream.finisher(), downstream.characteristics().toArray(new Characteristics[0]));
}
示例7: BaseCollector
import java.util.stream.Collector.Characteristics; //导入依赖的package包/类
BaseCollector(Supplier<A> supplier, BiConsumer<A, A> merger, Function<A, R> finisher,
Set<Characteristics> characteristics) {
this.supplier = supplier;
this.merger = merger;
this.finisher = finisher;
this.characteristics = characteristics;
}
示例8: CancellableCollectorImpl
import java.util.stream.Collector.Characteristics; //导入依赖的package包/类
CancellableCollectorImpl(Supplier<A> supplier, BiConsumer<A, T> accumulator, BinaryOperator<A> combiner,
Function<A, R> finisher, Predicate<A> finished,
Set<java.util.stream.Collector.Characteristics> characteristics) {
this.supplier = supplier;
this.accumulator = accumulator;
this.combiner = combiner;
this.finisher = finisher;
this.finished = finished;
this.characteristics = characteristics;
}
示例9: toSet
import java.util.stream.Collector.Characteristics; //导入依赖的package包/类
/**
* Returns a {@link Collector} that accumulates the input elements into an {@link ImmutableSet}.
*
* @return The {@link Collector}. Will never be {@code null}.
*/
public static <T> Collector<T, ImmutableSet.Builder<T>, ImmutableSet<T>> toSet() {
BinaryOperator<ImmutableSet.Builder<T>> combiner = (first, second) -> first.addAll(second.build());
return Collector.of(ImmutableSet::builder, ImmutableSet.Builder::add, combiner, ImmutableSet.Builder::build,
Characteristics.UNORDERED);
}
示例10: of
import java.util.stream.Collector.Characteristics; //导入依赖的package包/类
public static <T, A, R, X extends Throwable> ThrowingCollector<T, A, R, X>
of(Collector<T, A, R> collector) {
Objects.requireNonNull(collector);
return new ThrowingCollector<T, A, R, X>() {
@Override
public ThrowingSupplier<A, X> supplier() {
return collector.supplier()::get;
}
@Override
public ThrowingBiConsumer<A, T, X> accumulator() {
return collector.accumulator()::accept;
}
@Override
public ThrowingBinaryOperator<A, X> combiner() {
return collector.combiner()::apply;
}
@Override
public ThrowingFunction<A, R, X> finisher() {
return collector.finisher()::apply;
}
@Override
public Set<Characteristics> characteristics() {
return collector.characteristics();
}
};
}
示例11: toMultiSet
import java.util.stream.Collector.Characteristics; //导入依赖的package包/类
public static <T> Collector<T, ?, Multiset<T>> toMultiSet( final Supplier<Multiset<T>> supplier ){
return Collector.of(
supplier,
( set, t ) -> set.add( t ),
( l, r ) -> { l.addAll( r ); return l; },
l -> l,
Characteristics.IDENTITY_FINISH );
}
示例12: toImmutableSortedSetReversed
import java.util.stream.Collector.Characteristics; //导入依赖的package包/类
public static <T extends Comparable<?>> Collector<T, ImmutableSortedSet.Builder<T>, ImmutableSortedSet<T>> toImmutableSortedSetReversed() {
return Collector.of(
ImmutableSortedSet::<T> reverseOrder,
ImmutableSortedSet.Builder<T>::add,
(l, r) -> l.addAll(r.build()),
ImmutableSortedSet.Builder<T>::build,
Characteristics.UNORDERED);
}
示例13: toImmutableSortedSet
import java.util.stream.Collector.Characteristics; //导入依赖的package包/类
public static <T> Collector<T, ?, ImmutableSortedSet<T>> toImmutableSortedSet( final Supplier<Builder<T>> supplier ){
return Collector.of(
supplier,
ImmutableSortedSet.Builder<T>::add,
(l, r) -> l.addAll(r.build()),
ImmutableSortedSet.Builder<T>::build,
Characteristics.UNORDERED);
}
示例14: toBiMap
import java.util.stream.Collector.Characteristics; //导入依赖的package包/类
public static <T, K, V> Collector<T,?,BiMap<K,V>> toBiMap(
final Supplier<BiMap<K,V>> supplier,
final Function<T,K> keyFunction,
final Function<T,V> valueFunction ){
return Collector.of(
supplier,
(map, value) -> map.put( keyFunction.apply( value ), valueFunction.apply( value )),
(l, r) -> { l.putAll( r ); return l;},
map -> map,
Characteristics.IDENTITY_FINISH );
}
示例15: toTable
import java.util.stream.Collector.Characteristics; //导入依赖的package包/类
public static <T, R, C, V> Collector<T,?,Table<R,C,V>> toTable(
final Function<T,R> rowFunction,
final Function<T,C> columnFunction,
final Function<T,V> valueFunction ){
return Collector.of(
HashBasedTable::<R,C,V> create,
(table, value ) -> table.put( rowFunction.apply( value ), columnFunction.apply( value ), valueFunction.apply( value )),
(l, r) -> { l.putAll( r ); return l; },
table -> table,
Characteristics.IDENTITY_FINISH);
}