當前位置: 首頁>>代碼示例>>Java>>正文


Java Collectors.collectingAndThen方法代碼示例

本文整理匯總了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);
}
 
開發者ID:zugzug90,項目名稱:guava-mock,代碼行數:27,代碼來源:ImmutableSortedMap.java

示例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);
}
 
開發者ID:zugzug90,項目名稱:guava-mock,代碼行數:13,代碼來源:ImmutableMap.java

示例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);
}
 
開發者ID:google,項目名稱:mug,代碼行數:21,代碼來源:BiCollection.java

示例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);
}
 
開發者ID:zugzug90,項目名稱:guava-mock,代碼行數:23,代碼來源:ImmutableMap.java

示例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);
}
 
開發者ID:zugzug90,項目名稱:guava-mock,代碼行數:16,代碼來源:ImmutableSortedMap.java

示例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);
}
 
開發者ID:zugzug90,項目名稱:guava-mock,代碼行數:55,代碼來源:ImmutableSetMultimap.java

示例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;
    });
}
 
開發者ID:hashsdn,項目名稱:hashsdn-controller,代碼行數:7,代碼來源:LocalSnapshotStore.java

示例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);
}
 
開發者ID:zugzug90,項目名稱:guava-mock,代碼行數:46,代碼來源:ImmutableListMultimap.java


注:本文中的java.util.stream.Collectors.collectingAndThen方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。