本文整理匯總了Java中java.util.stream.Collector.of方法的典型用法代碼示例。如果您正苦於以下問題:Java Collector.of方法的具體用法?Java Collector.of怎麽用?Java Collector.of使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.stream.Collector
的用法示例。
在下文中一共展示了Collector.of方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: toImmutableEnumMap
import java.util.stream.Collector; //導入方法依賴的package包/類
/**
* Returns a {@link Collector} that accumulates elements into an {@code ImmutableMap} whose keys
* and values are the result of applying the provided mapping functions to the input elements. The
* resulting implementation is specialized for enum key types. The returned map and its views will
* iterate over keys in their enum definition order, not encounter order.
*
* <p>If the mapped keys contain duplicates, an {@code IllegalArgumentException} is thrown when
* the collection operation is performed. (This differs from the {@code Collector} returned by
* {@link java.util.stream.Collectors#toMap(java.util.function.Function,
* java.util.function.Function) Collectors.toMap(Function, Function)}, which throws an
* {@code IllegalStateException}.)
*
* @since 21.0
*/
@Beta
public static <T, K extends Enum<K>, V> Collector<T, ?, ImmutableMap<K, V>> toImmutableEnumMap(
java.util.function.Function<? super T, ? extends K> keyFunction,
java.util.function.Function<? super T, ? extends V> valueFunction) {
checkNotNull(keyFunction);
checkNotNull(valueFunction);
return Collector.of(
() ->
new Accumulator<K, V>(
(v1, v2) -> {
throw new IllegalArgumentException("Multiple values for key: " + v1 + ", " + v2);
}),
(accum, t) -> {
K key = checkNotNull(keyFunction.apply(t), "Null key for input %s", t);
V newValue = checkNotNull(valueFunction.apply(t), "Null value for input %s", t);
accum.put(key, newValue);
},
Accumulator::combine,
Accumulator::toImmutableMap,
Collector.Characteristics.UNORDERED);
}
示例2: toImmutableSortedMap
import java.util.stream.Collector; //導入方法依賴的package包/類
static <T, K, V> Collector<T, ?, ImmutableSortedMap<K, V>> toImmutableSortedMap(
Comparator<? super K> comparator,
Function<? super T, ? extends K> keyFunction,
Function<? super T, ? extends V> valueFunction) {
checkNotNull(comparator);
checkNotNull(keyFunction);
checkNotNull(valueFunction);
/*
* We will always fail if there are duplicate keys, and the keys are always sorted by
* the Comparator, so the entries can come in in arbitrary order -- so we report UNORDERED.
*/
return Collector.of(
() -> new ImmutableSortedMap.Builder<K, V>(comparator),
(builder, input) -> builder.put(keyFunction.apply(input), valueFunction.apply(input)),
ImmutableSortedMap.Builder::combine,
ImmutableSortedMap.Builder::build,
Collector.Characteristics.UNORDERED);
}
示例3: toImmutableList
import java.util.stream.Collector; //導入方法依賴的package包/類
public static <T> Collector<T, ?, ImmutableList<T>> toImmutableList() {
Supplier<ImmutableList.Builder<T>> supplier = ImmutableList.Builder::new;
BiConsumer<ImmutableList.Builder<T>, T> accumulator = ImmutableList.Builder::add;
BinaryOperator<ImmutableList.Builder<T>> combiner = (l, r) -> l.addAll(r.build());
Function<ImmutableList.Builder<T>, ImmutableList<T>> finisher = ImmutableList.Builder::build;
return Collector.of(supplier, accumulator, combiner, finisher);
}
示例4: toImmutableMap
import java.util.stream.Collector; //導入方法依賴的package包/類
static <T, K, V> Collector<T, ?, ImmutableMap<K, V>> toImmutableMap(
Function<? super T, ? extends K> keyFunction,
Function<? super T, ? extends V> valueFunction) {
checkNotNull(keyFunction);
checkNotNull(valueFunction);
return Collector.of(
ImmutableMap.Builder<K, V>::new,
(builder, input) -> builder.put(keyFunction.apply(input), valueFunction.apply(input)),
ImmutableMap.Builder::combine,
ImmutableMap.Builder::build);
}
示例5: toImmutableSet
import java.util.stream.Collector; //導入方法依賴的package包/類
static <E> Collector<E, ?, ImmutableSet<E>> toImmutableSet() {
return Collector.of(
ImmutableSet::<E>builder,
ImmutableSet.Builder::add,
ImmutableSet.Builder::combine,
ImmutableSet.Builder::build);
}
示例6: mapToStream
import java.util.stream.Collector; //導入方法依賴的package包/類
/**
* <p>Builds a collector that accumulates all the elements in a stream. The objects
* of the input stream of {@code T} are added to the returned stream. This collector is useful
* only as a downstream collector. </p>
*
* @param <T> the type of the elements of the provided stream
* @return a collector that builds a stream of the elements
*/
public static <T> Collector<T, ?, Stream<T>> mapToStream() {
return Collector.<T, Stream.Builder<T>, Stream<T>>of(
Stream::builder,
Stream.Builder::accept,
(builder1, builder2) -> {
builder2.build().forEach(builder1);
return builder1;
},
Stream.Builder::build
);
}
示例7: of
import java.util.stream.Collector; //導入方法依賴的package包/類
/**
* Returns a collector that collects items in a Morpheus array
* @param type the array type
* @param expectedLength an estimate of the expected length, does not have to be exact
* @param <T> the array element type
* @return the newly created collector
*/
public static <T> Collector<T,ArrayBuilder<T>,Array<T>> of(Class<T> type, int expectedLength) {
final Supplier<ArrayBuilder<T>> supplier = () -> ArrayBuilder.of(expectedLength, type);
final BinaryOperator<ArrayBuilder<T>> combiner = ArrayBuilder::addAll;
final BiConsumer<ArrayBuilder<T>,T> accumulator = ArrayBuilder::add;
final Function<ArrayBuilder<T>,Array<T>> finisher = ArrayBuilder::toArray;
return Collector.of(supplier, accumulator, combiner, finisher);
}
示例8: toImmutableSet
import java.util.stream.Collector; //導入方法依賴的package包/類
public static <T> Collector<T, ?, ImmutableSet<T>> toImmutableSet() {
Supplier<ImmutableSet.Builder<T>> supplier = ImmutableSet.Builder::new;
BiConsumer<ImmutableSet.Builder<T>, T> accumulator = ImmutableSet.Builder::add;
BinaryOperator<ImmutableSet.Builder<T>> combiner = (l, r) -> l.addAll(r.build());
Function<ImmutableSet.Builder<T>, ImmutableSet<T>> finisher = ImmutableSet.Builder::build;
return Collector.of(supplier, accumulator, combiner, finisher);
}
示例9: toImmutableTable
import java.util.stream.Collector; //導入方法依賴的package包/類
/**
* Returns a {@code Collector} that accumulates elements into an {@code ImmutableTable}. Each
* input element is mapped to one cell in the returned table, with the rows, columns, and values
* generated by applying the specified functions. If multiple inputs are mapped to the same row
* and column pair, they will be combined with the specified merging function in encounter order.
*
* <p>The returned {@code Collector} will throw a {@code NullPointerException} at collection time
* if the row, column, value, or merging functions return null on any input.
*
* @since 21.0
*/
public static <T, R, C, V> Collector<T, ?, ImmutableTable<R, C, V>> toImmutableTable(
Function<? super T, ? extends R> rowFunction,
Function<? super T, ? extends C> columnFunction,
Function<? super T, ? extends V> valueFunction,
BinaryOperator<V> mergeFunction) {
checkNotNull(rowFunction);
checkNotNull(columnFunction);
checkNotNull(valueFunction);
checkNotNull(mergeFunction);
/*
* No mutable Table exactly matches the insertion order behavior of ImmutableTable.Builder, but
* the Builder can't efficiently support merging of duplicate values. Getting around this
* requires some work.
*/
return Collector.of(
() -> new CollectorState<R, C, V>()
/* GWT isn't currently playing nicely with constructor references? */,
(state, input) ->
state.put(
rowFunction.apply(input),
columnFunction.apply(input),
valueFunction.apply(input),
mergeFunction),
(s1, s2) -> s1.combine(s2, mergeFunction),
state -> state.toTable());
}
示例10: collector
import java.util.stream.Collector; //導入方法依賴的package包/類
public static Collector<Transaction, XirrDetails, XirrDetails> collector() {
return Collector.of(
XirrDetails::new,
XirrDetails::accumulate,
XirrDetails::combine,
Collector.Characteristics.IDENTITY_FINISH,
Collector.Characteristics.UNORDERED);
}
示例11: toSortedMap
import java.util.stream.Collector; //導入方法依賴的package包/類
public static <T, K, V> Collector<T, ?, ImmutableSortedMap<K, V>> toSortedMap(Comparator<? super K> comparator, Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) {
return Collector.of(
() -> new ImmutableSortedMap.Builder<K, V>(comparator),
(builder, input) -> builder.put(keyMapper.apply(input), valueMapper.apply(input)),
(l, r) -> l.putAll(r.build()),
ImmutableSortedMap.Builder::build,
Collector.Characteristics.UNORDERED
);
}
示例12: toTable
import java.util.stream.Collector; //導入方法依賴的package包/類
/**
* Returns a {@link Collector} that accumulates elements into a {@code Table} created using the
* specified supplier, whose cells are generated by applying the provided mapping functions to the
* input elements. Cells are inserted into the generated {@code Table} in encounter order.
*
* <p>If multiple input elements map to the same row and column, the specified merging function is
* used to combine the values. Like {@link
* java.util.stream.Collectors#toMap(java.util.function.Function, java.util.function.Function,
* BinaryOperator, java.util.function.Supplier)}, this Collector throws a {@code
* NullPointerException} on null values returned from {@code valueFunction}, and treats nulls
* returned from {@code mergeFunction} as removals of that row/column pair.
*
* @since 21.0
*/
public static <T, R, C, V, I extends Table<R, C, V>> Collector<T, ?, I> toTable(
java.util.function.Function<? super T, ? extends R> rowFunction,
java.util.function.Function<? super T, ? extends C> columnFunction,
java.util.function.Function<? super T, ? extends V> valueFunction,
BinaryOperator<V> mergeFunction,
java.util.function.Supplier<I> tableSupplier) {
checkNotNull(rowFunction);
checkNotNull(columnFunction);
checkNotNull(valueFunction);
checkNotNull(mergeFunction);
checkNotNull(tableSupplier);
return Collector.of(
tableSupplier,
(table, input) ->
merge(
table,
rowFunction.apply(input),
columnFunction.apply(input),
valueFunction.apply(input),
mergeFunction),
(table1, table2) -> {
for (Table.Cell<R, C, V> cell2 : table2.cellSet()) {
merge(table1, cell2.getRowKey(), cell2.getColumnKey(), cell2.getValue(), mergeFunction);
}
return table1;
});
}
示例13: closureCollector
import java.util.stream.Collector; //導入方法依賴的package包/類
/**
* Collect types into a new closure (using a @code{ClosureHolder})
*/
public Collector<Type, ClosureHolder, List<Type>> closureCollector(boolean minClosure, BiPredicate<Type, Type> shouldSkip) {
return Collector.of(() -> new ClosureHolder(minClosure, shouldSkip),
ClosureHolder::add,
ClosureHolder::merge,
ClosureHolder::closure);
}
示例14: toImmutableSortedSet
import java.util.stream.Collector; //導入方法依賴的package包/類
static <E> Collector<E, ?, ImmutableSortedSet<E>> toImmutableSortedSet(
Comparator<? super E> comparator) {
checkNotNull(comparator);
return Collector.of(
() -> new ImmutableSortedSet.Builder<E>(comparator),
ImmutableSortedSet.Builder::add,
ImmutableSortedSet.Builder::combine,
ImmutableSortedSet.Builder::build);
}
示例15: toArrayNode
import java.util.stream.Collector; //導入方法依賴的package包/類
private Collector<JsonNode, ArrayNode, ArrayNode> toArrayNode() {
return Collector.of(() -> mapper().createArrayNode(),
ArrayNode::add,
ArrayNode::addAll);
}