/** * 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); }
public static <T> List<T> concatLists(List<T> left, List<T> right, Function<List<T>, List<T>> finisher) { return Stream.concat(left.stream(), right.stream()) .collect(Collectors.collectingAndThen(Collectors.toList(), finisher)); }
/** * 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); }
public static <T> Collector<T, ?, List<T>> toReversedList() { return Collectors.collectingAndThen(Collectors.toList(), l -> { Collections.<T>reverse(l); return l; }); }
public static <T> Collector<T, ?, T> singletonCollector() { return Collectors.collectingAndThen( Collectors.toList(), list -> { if (list.size() > 1) { throw new IllegalStateException("List contains more than one element: " + list); } return list.size() > 0 ? list.get(0) : null; }); } }
/** * For stream of one expected element, return the element * * @throws IllegalArgumentException if stream has no element or more than 1 element */ public static <T> Collector<T, ?, T> toOneElement() { return java.util.stream.Collectors.collectingAndThen( java.util.stream.Collectors.toList(), list -> { if (list.size() != 1) { throw new IllegalStateException("Stream should have only one element"); } return list.get(0); }); }
private List<String> prepareDimensionsOrMetrics(@Nullable List<String> list, Interner<List<String>> interner) { if (list == null) { return ImmutableList.of(); } else { List<String> result = list .stream() .filter(s -> !Strings.isNullOrEmpty(s)) // dimensions & metrics are stored as canonical string values to decrease memory required for storing // large numbers of segments. .map(STRING_INTERNER::intern) // TODO replace with ImmutableList.toImmutableList() when updated to Guava 21+ .collect(Collectors.collectingAndThen(Collectors.toList(), ImmutableList::copyOf)); return interner.intern(result); } }
/** * Creates and returns a new unmodifiable list of immutable Stage object * * @return a new unmodifiable list of immutable Stage object */ List<Stage<?>> stages() { resolveStages(); return stageBeans.stream() .map(StageBean::asStage) .collect(collectingAndThen(toList(), Collections::unmodifiableList)); }
static List<SanitizedFareRule> sanitizeFareRules(List<FareRule> gtfsFareRules) { // Make proper fare rule objects from the CSV-like FareRule ArrayList<SanitizedFareRule> result = new ArrayList<>(); result.addAll(gtfsFareRules.stream().filter(rule -> rule.route_id != null).map(rule -> new RouteRule(rule.route_id)).collect(toList())); 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())); result.add(gtfsFareRules.stream().filter(rule -> rule.contains_id != null).map(rule -> rule.contains_id).collect(Collectors.collectingAndThen(toList(), ZoneRule::new))); return result; }
AstPackageExplorer(Language language) { availableNodeNames = getClassesInPackage("net.sourceforge.pmd.lang." + language.getTerseName() + ".ast") .filter(clazz -> clazz.getSimpleName().startsWith("AST")) .filter(clazz -> !clazz.isInterface() && !Modifier.isAbstract(clazz.getModifiers())) .map(m -> m.getSimpleName().substring("AST".length())) .collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList)); }
private <T> List<T> simplePropertyValues(String propertyName, Class<T> clazz, Iterable<?> target) { return stream(target).map(e -> e == null ? null : propertyValue(propertyName, clazz, e)) .collect(collectingAndThen(toList(), Collections::unmodifiableList)); }
@Override public TaskEventListener getTaskListener(String eventType) { if (CollectionUtils.isEmpty(configEntity.getListeners())) { return null; } return configEntity .getListeners() .stream() .filter(config -> eventType.equals(config.getEventType())) .map(ProcessConfigurationServiceImpl.this::<TaskEvent>createTaskEventListener) .collect(Collectors.collectingAndThen(Collectors.toList(), list -> event -> list.forEach(listener -> listener.accept(event)))); } };
public Expression toPredicate(TupleDomain<Symbol> tupleDomain) { if (tupleDomain.isNone()) { return FALSE_LITERAL; } Map<Symbol, Domain> domains = tupleDomain.getDomains().get(); return domains.entrySet().stream() .sorted(comparing(entry -> entry.getKey().getName())) .map(entry -> toPredicate(entry.getValue(), entry.getKey().toSymbolReference())) .collect(collectingAndThen(toImmutableList(), ExpressionUtils::combineConjuncts)); }
private <T> List<T> simpleFieldValues(String fieldName, Class<T> clazz, Iterable<?> target) { return stream(target).map(e -> e == null ? null : fieldValue(fieldName, clazz, e)) .collect(collectingAndThen(toList(), Collections::unmodifiableList)); }
@Override public ProcessEventListener getProcessListener(String eventType) { if (CollectionUtils.isEmpty(entity.getListeners())) { return null; } return entity .getListeners() .stream() .filter(config -> eventType.equals(config.getEventType())) .map(ProcessConfigurationServiceImpl.this::<ProcessEvent>createTaskEventListener) .collect(Collectors.collectingAndThen(Collectors.toList(), list -> event -> list.forEach(listener -> listener.accept(event)))); } };
checkNotNull(keyFunction); checkNotNull(valuesFunction); return Collectors.collectingAndThen( Multimaps.flatteningToMultimap( input -> checkNotNull(keyFunction.apply(input)),
checkNotNull(keyFunction); checkNotNull(valuesFunction); return Collectors.collectingAndThen( Multimaps.flatteningToMultimap( input -> checkNotNull(keyFunction.apply(input)),
private static Collector<JCVariableDecl, ?, ImmutableMultimap<Integer, JCVariableDecl>> collectByEditDistanceTo(String baseName) { return Collectors.collectingAndThen( Multimaps.toMultimap( (JCVariableDecl varDecl) -> LevenshteinEditDistance.getEditDistance(baseName, varDecl.name.toString()), varDecl -> varDecl, LinkedHashMultimap::create), ImmutableMultimap::copyOf); }
public CompletableFuture<List<Project>> getProjectsForCompanies(List<UUID> companyIds) { return CompletableFuture.supplyAsync(() -> projects.values().stream() .filter(project -> companyIds.contains(project.getCompanyId())) .collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList))); }
/** * 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); }