本文整理汇总了Java中java.util.stream.Collectors.collectingAndThen方法的典型用法代码示例。如果您正苦于以下问题:Java Collectors.collectingAndThen方法的具体用法?Java Collectors.collectingAndThen怎么用?Java Collectors.collectingAndThen使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.stream.Collectors
的用法示例。
在下文中一共展示了Collectors.collectingAndThen方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: toImmutableSortedMap
import java.util.stream.Collectors; //导入方法依赖的package包/类
/**
* Returns a {@link Collector} that accumulates elements into an {@code ImmutableSortedMap} whose
* keys and values are the result of applying the provided mapping functions to the input
* elements.
*
* <p>If the mapped keys contain duplicates (according to the comparator), the the values are
* merged using the specified merging function. Entries will appear in the encounter order of the
* first occurrence of the key.
*
* @since 21.0
*/
@Beta
public 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,
BinaryOperator<V> mergeFunction) {
checkNotNull(comparator);
checkNotNull(keyFunction);
checkNotNull(valueFunction);
checkNotNull(mergeFunction);
return Collectors.collectingAndThen(
Collectors.toMap(
keyFunction, valueFunction, mergeFunction, () -> new TreeMap<K, V>(comparator)),
ImmutableSortedMap::copyOfSorted);
}
示例2: toImmutableMap
import java.util.stream.Collectors; //导入方法依赖的package包/类
@Beta
public static <T, K, V> Collector<T, ?, ImmutableMap<K, V>> toImmutableMap(
Function<? super T, ? extends K> keyFunction,
Function<? super T, ? extends V> valueFunction,
BinaryOperator<V> mergeFunction) {
checkNotNull(keyFunction);
checkNotNull(valueFunction);
checkNotNull(mergeFunction);
return Collectors.collectingAndThen(
Collectors.toMap(keyFunction, valueFunction, mergeFunction, LinkedHashMap::new),
ImmutableMap::copyOf);
}
示例3: toBiCollection
import java.util.stream.Collectors; //导入方法依赖的package包/类
/**
* Returns a {@code Collector} that extracts the pairs from the input stream,
* and then collects them into a {@code BiCollection}.
*
* @param leftFunction extracts the first element of each pair
* @param rightFunction extracts the second element of each pair
* @param collectorStrategy determines the kind of collection to use. For example:
* {@code Collectors::toList} or {@code ImmutableList::toImmutableList}.
*/
public static <T, L, R> Collector<T, ?, BiCollection<L, R>> toBiCollection(
Function<? super T, ? extends L> leftFunction,
Function<? super T, ? extends R> rightFunction,
CollectorStrategy collectorStrategy) {
requireNonNull(leftFunction);
requireNonNull(rightFunction);
Function<T, Map.Entry<L, R>> toEntry = x -> kv(leftFunction.apply(x), rightFunction.apply(x));
Collector<T, ?, ? extends Collection<? extends Map.Entry<? extends L, ? extends R>>> entryCollector =
Collectors.mapping(toEntry, collectorStrategy.collector());
return Collectors.collectingAndThen(entryCollector, BiCollection::from);
}
示例4: toImmutableMap
import java.util.stream.Collectors; //导入方法依赖的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.
*
* <p>If the mapped keys contain duplicates (according to {@link Object#equals(Object)}), the
* values are merged using the specified merging function. Entries will appear in the encounter
* order of the first occurrence of the key.
*
* @since 21.0
*/
@Beta
public static <T, K, V> Collector<T, ?, ImmutableMap<K, V>> toImmutableMap(
Function<? super T, ? extends K> keyFunction,
Function<? super T, ? extends V> valueFunction,
BinaryOperator<V> mergeFunction) {
checkNotNull(keyFunction);
checkNotNull(valueFunction);
checkNotNull(mergeFunction);
return Collectors.collectingAndThen(
Collectors.toMap(keyFunction, valueFunction, mergeFunction, LinkedHashMap::new),
ImmutableMap::copyOf);
}
示例5: toImmutableSortedMap
import java.util.stream.Collectors; //导入方法依赖的package包/类
@Beta
public 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,
BinaryOperator<V> mergeFunction) {
checkNotNull(comparator);
checkNotNull(keyFunction);
checkNotNull(valueFunction);
checkNotNull(mergeFunction);
return Collectors.collectingAndThen(
Collectors.toMap(
keyFunction, valueFunction, mergeFunction, () -> new TreeMap<K, V>(comparator)),
ImmutableSortedMap::copyOfSorted);
}
示例6: flatteningToImmutableSetMultimap
import java.util.stream.Collectors; //导入方法依赖的package包/类
/**
* Returns a {@code Collector} accumulating entries into an {@code ImmutableSetMultimap}. Each
* input element is mapped to a key and a stream of values, each of which are put into the
* resulting {@code Multimap}, in the encounter order of the stream and the encounter order of the
* streams of values.
*
* <p>Example:
*
* <pre>{@code
* static final ImmutableSetMultimap<Character, Character> FIRST_LETTER_MULTIMAP =
* Stream.of("banana", "apple", "carrot", "asparagus", "cherry")
* .collect(
* flatteningToImmutableSetMultimap(
* str -> str.charAt(0),
* str -> str.substring(1).chars().mapToObj(c -> (char) c));
*
* // is equivalent to
*
* static final ImmutableSetMultimap<Character, Character> FIRST_LETTER_MULTIMAP =
* ImmutableSetMultimap.<Character, Character>builder()
* .putAll('b', Arrays.asList('a', 'n', 'a', 'n', 'a'))
* .putAll('a', Arrays.asList('p', 'p', 'l', 'e'))
* .putAll('c', Arrays.asList('a', 'r', 'r', 'o', 't'))
* .putAll('a', Arrays.asList('s', 'p', 'a', 'r', 'a', 'g', 'u', 's'))
* .putAll('c', Arrays.asList('h', 'e', 'r', 'r', 'y'))
* .build();
*
* // after deduplication, the resulting multimap is equivalent to
*
* static final ImmutableSetMultimap<Character, Character> FIRST_LETTER_MULTIMAP =
* ImmutableSetMultimap.<Character, Character>builder()
* .putAll('b', Arrays.asList('a', 'n'))
* .putAll('a', Arrays.asList('p', 'l', 'e', 's', 'a', 'r', 'g', 'u'))
* .putAll('c', Arrays.asList('a', 'r', 'o', 't', 'h', 'e', 'y'))
* .build();
* }
* }</pre>
*
* @since 21.0
*/
@Beta
public static <T, K, V>
Collector<T, ?, ImmutableSetMultimap<K, V>> flatteningToImmutableSetMultimap(
Function<? super T, ? extends K> keyFunction,
Function<? super T, ? extends Stream<? extends V>> valuesFunction) {
checkNotNull(keyFunction);
checkNotNull(valuesFunction);
return Collectors.collectingAndThen(
Multimaps.flatteningToMultimap(
input -> checkNotNull(keyFunction.apply(input)),
input -> valuesFunction.apply(input).peek(Preconditions::checkNotNull),
MultimapBuilder.linkedHashKeys().linkedHashSetValues()::<K, V>build),
ImmutableSetMultimap::copyOf);
}
示例7: reverse
import java.util.stream.Collectors; //导入方法依赖的package包/类
private static <T> Collector<T, ?, List<T>> reverse() {
return Collectors.collectingAndThen(Collectors.toList(), list -> {
Collections.reverse(list);
return list;
});
}
示例8: flatteningToImmutableListMultimap
import java.util.stream.Collectors; //导入方法依赖的package包/类
/**
* Returns a {@code Collector} accumulating entries into an {@code ImmutableListMultimap}. Each
* input element is mapped to a key and a stream of values, each of which are put into the
* resulting {@code Multimap}, in the encounter order of the stream and the encounter order of the
* streams of values.
*
* <p>Example:
*
* <pre>{@code
* static final ImmutableListMultimap<Character, Character> FIRST_LETTER_MULTIMAP =
* Stream.of("banana", "apple", "carrot", "asparagus", "cherry")
* .collect(
* flatteningToImmutableListMultimap(
* str -> str.charAt(0),
* str -> str.substring(1).chars().mapToObj(c -> (char) c));
*
* // is equivalent to
*
* static final ImmutableListMultimap<Character, Character> FIRST_LETTER_MULTIMAP =
* ImmutableListMultimap.<Character, Character>builder()
* .putAll('b', Arrays.asList('a', 'n', 'a', 'n', 'a'))
* .putAll('a', Arrays.asList('p', 'p', 'l', 'e'))
* .putAll('c', Arrays.asList('a', 'r', 'r', 'o', 't'))
* .putAll('a', Arrays.asList('s', 'p', 'a', 'r', 'a', 'g', 'u', 's'))
* .putAll('c', Arrays.asList('h', 'e', 'r', 'r', 'y'))
* .build();
* }
* }</pre>
*
* @since 21.0
*/
@Beta
public static <T, K, V>
Collector<T, ?, ImmutableListMultimap<K, V>> flatteningToImmutableListMultimap(
Function<? super T, ? extends K> keyFunction,
Function<? super T, ? extends Stream<? extends V>> valuesFunction) {
checkNotNull(keyFunction);
checkNotNull(valuesFunction);
return Collectors.collectingAndThen(
Multimaps.flatteningToMultimap(
input -> checkNotNull(keyFunction.apply(input)),
input -> valuesFunction.apply(input).peek(Preconditions::checkNotNull),
MultimapBuilder.linkedHashKeys().arrayListValues()::<K, V>build),
ImmutableListMultimap::copyOf);
}