java.util.stream.Collectors.collectingAndThen()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(9.5k)|赞(0)|评价(0)|浏览(261)

本文整理了Java中java.util.stream.Collectors.collectingAndThen()方法的一些代码示例,展示了Collectors.collectingAndThen()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Collectors.collectingAndThen()方法的具体详情如下:
包路径:java.util.stream.Collectors
类名称:Collectors
方法名:collectingAndThen

Collectors.collectingAndThen介绍

暂无

代码示例

代码示例来源:origin: google/guava

  1. /**
  2. * Returns a {@link Collector} that accumulates elements into an {@code ImmutableMap} whose keys
  3. * and values are the result of applying the provided mapping functions to the input elements.
  4. *
  5. * <p>If the mapped keys contain duplicates (according to {@link Object#equals(Object)}), the
  6. * values are merged using the specified merging function. Entries will appear in the encounter
  7. * order of the first occurrence of the key.
  8. *
  9. * @since 21.0
  10. */
  11. @Beta
  12. public static <T, K, V> Collector<T, ?, ImmutableMap<K, V>> toImmutableMap(
  13. Function<? super T, ? extends K> keyFunction,
  14. Function<? super T, ? extends V> valueFunction,
  15. BinaryOperator<V> mergeFunction) {
  16. checkNotNull(keyFunction);
  17. checkNotNull(valueFunction);
  18. checkNotNull(mergeFunction);
  19. return Collectors.collectingAndThen(
  20. Collectors.toMap(keyFunction, valueFunction, mergeFunction, LinkedHashMap::new),
  21. ImmutableMap::copyOf);
  22. }

代码示例来源:origin: apache/kafka

  1. public static <T> List<T> concatLists(List<T> left, List<T> right, Function<List<T>, List<T>> finisher) {
  2. return Stream.concat(left.stream(), right.stream())
  3. .collect(Collectors.collectingAndThen(Collectors.toList(), finisher));
  4. }

代码示例来源:origin: google/guava

  1. /**
  2. * Returns a {@link Collector} that accumulates elements into an {@code ImmutableSortedMap} whose
  3. * keys and values are the result of applying the provided mapping functions to the input
  4. * elements.
  5. *
  6. * <p>If the mapped keys contain duplicates (according to the comparator), the the values are
  7. * merged using the specified merging function. Entries will appear in the encounter order of the
  8. * first occurrence of the key.
  9. *
  10. * @since 21.0
  11. */
  12. @Beta
  13. public static <T, K, V> Collector<T, ?, ImmutableSortedMap<K, V>> toImmutableSortedMap(
  14. Comparator<? super K> comparator,
  15. Function<? super T, ? extends K> keyFunction,
  16. Function<? super T, ? extends V> valueFunction,
  17. BinaryOperator<V> mergeFunction) {
  18. checkNotNull(comparator);
  19. checkNotNull(keyFunction);
  20. checkNotNull(valueFunction);
  21. checkNotNull(mergeFunction);
  22. return Collectors.collectingAndThen(
  23. Collectors.toMap(
  24. keyFunction, valueFunction, mergeFunction, () -> new TreeMap<K, V>(comparator)),
  25. ImmutableSortedMap::copyOfSorted);
  26. }

代码示例来源:origin: speedment/speedment

  1. public static <T> Collector<T, ?, List<T>> toReversedList() {
  2. return Collectors.collectingAndThen(Collectors.toList(), l -> {
  3. Collections.<T>reverse(l);
  4. return l;
  5. });
  6. }

代码示例来源:origin: knowm/XChange

  1. public static <T> Collector<T, ?, T> singletonCollector() {
  2. return Collectors.collectingAndThen(
  3. Collectors.toList(),
  4. list -> {
  5. if (list.size() > 1) {
  6. throw new IllegalStateException("List contains more than one element: " + list);
  7. }
  8. return list.size() > 0 ? list.get(0) : null;
  9. });
  10. }
  11. }

代码示例来源:origin: SonarSource/sonarqube

  1. /**
  2. * For stream of one expected element, return the element
  3. *
  4. * @throws IllegalArgumentException if stream has no element or more than 1 element
  5. */
  6. public static <T> Collector<T, ?, T> toOneElement() {
  7. return java.util.stream.Collectors.collectingAndThen(
  8. java.util.stream.Collectors.toList(),
  9. list -> {
  10. if (list.size() != 1) {
  11. throw new IllegalStateException("Stream should have only one element");
  12. }
  13. return list.get(0);
  14. });
  15. }

代码示例来源:origin: apache/incubator-druid

  1. private List<String> prepareDimensionsOrMetrics(@Nullable List<String> list, Interner<List<String>> interner)
  2. {
  3. if (list == null) {
  4. return ImmutableList.of();
  5. } else {
  6. List<String> result = list
  7. .stream()
  8. .filter(s -> !Strings.isNullOrEmpty(s))
  9. // dimensions & metrics are stored as canonical string values to decrease memory required for storing
  10. // large numbers of segments.
  11. .map(STRING_INTERNER::intern)
  12. // TODO replace with ImmutableList.toImmutableList() when updated to Guava 21+
  13. .collect(Collectors.collectingAndThen(Collectors.toList(), ImmutableList::copyOf));
  14. return interner.intern(result);
  15. }
  16. }

代码示例来源:origin: speedment/speedment

  1. /**
  2. * Creates and returns a new unmodifiable list of immutable Stage object
  3. *
  4. * @return a new unmodifiable list of immutable Stage object
  5. */
  6. List<Stage<?>> stages() {
  7. resolveStages();
  8. return stageBeans.stream()
  9. .map(StageBean::asStage)
  10. .collect(collectingAndThen(toList(), Collections::unmodifiableList));
  11. }

代码示例来源:origin: graphhopper/graphhopper

  1. static List<SanitizedFareRule> sanitizeFareRules(List<FareRule> gtfsFareRules) {
  2. // Make proper fare rule objects from the CSV-like FareRule
  3. ArrayList<SanitizedFareRule> result = new ArrayList<>();
  4. result.addAll(gtfsFareRules.stream().filter(rule -> rule.route_id != null).map(rule -> new RouteRule(rule.route_id)).collect(toList()));
  5. result.addAll(gtfsFareRules.stream().filter(rule -> rule.origin_id != null && rule.destination_id != null).map(rule -> new OriginDestinationRule(rule.origin_id, rule.destination_id)).collect(toList()));
  6. result.add(gtfsFareRules.stream().filter(rule -> rule.contains_id != null).map(rule -> rule.contains_id).collect(Collectors.collectingAndThen(toList(), ZoneRule::new)));
  7. return result;
  8. }

代码示例来源:origin: pmd/pmd

  1. AstPackageExplorer(Language language) {
  2. availableNodeNames =
  3. getClassesInPackage("net.sourceforge.pmd.lang." + language.getTerseName() + ".ast")
  4. .filter(clazz -> clazz.getSimpleName().startsWith("AST"))
  5. .filter(clazz -> !clazz.isInterface() && !Modifier.isAbstract(clazz.getModifiers()))
  6. .map(m -> m.getSimpleName().substring("AST".length()))
  7. .collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
  8. }

代码示例来源:origin: org.assertj/assertj-core

  1. private <T> List<T> simplePropertyValues(String propertyName, Class<T> clazz, Iterable<?> target) {
  2. return stream(target).map(e -> e == null ? null : propertyValue(propertyName, clazz, e))
  3. .collect(collectingAndThen(toList(), Collections::unmodifiableList));
  4. }

代码示例来源:origin: hs-web/hsweb-framework

  1. @Override
  2. public TaskEventListener getTaskListener(String eventType) {
  3. if (CollectionUtils.isEmpty(configEntity.getListeners())) {
  4. return null;
  5. }
  6. return configEntity
  7. .getListeners()
  8. .stream()
  9. .filter(config -> eventType.equals(config.getEventType()))
  10. .map(ProcessConfigurationServiceImpl.this::<TaskEvent>createTaskEventListener)
  11. .collect(Collectors.collectingAndThen(Collectors.toList(),
  12. list -> event -> list.forEach(listener -> listener.accept(event))));
  13. }
  14. };

代码示例来源:origin: prestodb/presto

  1. public Expression toPredicate(TupleDomain<Symbol> tupleDomain)
  2. {
  3. if (tupleDomain.isNone()) {
  4. return FALSE_LITERAL;
  5. }
  6. Map<Symbol, Domain> domains = tupleDomain.getDomains().get();
  7. return domains.entrySet().stream()
  8. .sorted(comparing(entry -> entry.getKey().getName()))
  9. .map(entry -> toPredicate(entry.getValue(), entry.getKey().toSymbolReference()))
  10. .collect(collectingAndThen(toImmutableList(), ExpressionUtils::combineConjuncts));
  11. }

代码示例来源:origin: org.assertj/assertj-core

  1. private <T> List<T> simpleFieldValues(String fieldName, Class<T> clazz, Iterable<?> target) {
  2. return stream(target).map(e -> e == null ? null : fieldValue(fieldName, clazz, e))
  3. .collect(collectingAndThen(toList(), Collections::unmodifiableList));
  4. }

代码示例来源:origin: hs-web/hsweb-framework

  1. @Override
  2. public ProcessEventListener getProcessListener(String eventType) {
  3. if (CollectionUtils.isEmpty(entity.getListeners())) {
  4. return null;
  5. }
  6. return entity
  7. .getListeners()
  8. .stream()
  9. .filter(config -> eventType.equals(config.getEventType()))
  10. .map(ProcessConfigurationServiceImpl.this::<ProcessEvent>createTaskEventListener)
  11. .collect(Collectors.collectingAndThen(Collectors.toList(),
  12. list -> event -> list.forEach(listener -> listener.accept(event))));
  13. }
  14. };

代码示例来源:origin: google/guava

  1. checkNotNull(keyFunction);
  2. checkNotNull(valuesFunction);
  3. return Collectors.collectingAndThen(
  4. Multimaps.flatteningToMultimap(
  5. input -> checkNotNull(keyFunction.apply(input)),

代码示例来源:origin: google/guava

  1. checkNotNull(keyFunction);
  2. checkNotNull(valuesFunction);
  3. return Collectors.collectingAndThen(
  4. Multimaps.flatteningToMultimap(
  5. input -> checkNotNull(keyFunction.apply(input)),

代码示例来源:origin: google/error-prone

  1. private static Collector<JCVariableDecl, ?, ImmutableMultimap<Integer, JCVariableDecl>>
  2. collectByEditDistanceTo(String baseName) {
  3. return Collectors.collectingAndThen(
  4. Multimaps.toMultimap(
  5. (JCVariableDecl varDecl) ->
  6. LevenshteinEditDistance.getEditDistance(baseName, varDecl.name.toString()),
  7. varDecl -> varDecl,
  8. LinkedHashMultimap::create),
  9. ImmutableMultimap::copyOf);
  10. }

代码示例来源:origin: graphql-java/graphql-java

  1. public CompletableFuture<List<Project>> getProjectsForCompanies(List<UUID> companyIds) {
  2. return CompletableFuture.supplyAsync(() -> projects.values().stream()
  3. .filter(project -> companyIds.contains(project.getCompanyId()))
  4. .collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList)));
  5. }

代码示例来源:origin: prestodb/presto

  1. /**
  2. * Returns a {@link Collector} that accumulates elements into an {@code ImmutableMap} whose keys
  3. * and values are the result of applying the provided mapping functions to the input elements.
  4. *
  5. * <p>If the mapped keys contain duplicates (according to {@link Object#equals(Object)}), the
  6. * values are merged using the specified merging function. Entries will appear in the encounter
  7. * order of the first occurrence of the key.
  8. *
  9. * @since 21.0
  10. */
  11. @Beta
  12. public static <T, K, V> Collector<T, ?, ImmutableMap<K, V>> toImmutableMap(
  13. Function<? super T, ? extends K> keyFunction,
  14. Function<? super T, ? extends V> valueFunction,
  15. BinaryOperator<V> mergeFunction) {
  16. checkNotNull(keyFunction);
  17. checkNotNull(valueFunction);
  18. checkNotNull(mergeFunction);
  19. return Collectors.collectingAndThen(
  20. Collectors.toMap(keyFunction, valueFunction, mergeFunction, LinkedHashMap::new),
  21. ImmutableMap::copyOf);
  22. }

相关文章