@Benchmark public Position minByMarketValue_serial_lazy_collect_methodref_jdk() { return this.positions.getJdkPositions().stream().collect( Collectors.minBy(MARKET_VALUE_COMPARATOR_METHODREF)).get(); }
@Benchmark public Position minByMarketValue_serial_lazy_collect_lambda_jdk() { return this.positions.getJdkPositions().stream().collect( Collectors.minBy(MARKET_VALUE_COMPARATOR_LAMBDA)).get(); }
@Benchmark public Position minByQuantity_serial_lazy_collect_methodref_jdk() { return this.positions.getJdkPositions().stream().collect( Collectors.minBy(QUANTITY_COMPARATOR_METHODREF)).get(); }
@Benchmark public Position minByQuantity_serial_lazy_collect_lambda_jdk() { return this.positions.getJdkPositions().stream().collect( Collectors.minBy(QUANTITY_COMPARATOR_LAMBDA)).get(); }
@Benchmark public Position minByQuantity_parallel_lazy_collect_methodref_jdk() { return this.positions.getJdkPositions().parallelStream().collect( Collectors.minBy(QUANTITY_COMPARATOR_METHODREF)).get(); }
@Benchmark public Position minByMarketValue_parallel_lazy_collect_methodref_jdk() { return this.positions.getJdkPositions().parallelStream().collect( Collectors.minBy(MARKET_VALUE_COMPARATOR_METHODREF)).get(); }
@Benchmark public Position minByMarketValue_parallel_lazy_collect_lambda_jdk() { return this.positions.getJdkPositions().parallelStream().collect( Collectors.minBy(MARKET_VALUE_COMPARATOR_LAMBDA)).get(); }
@Benchmark public Position minByQuantity_parallel_lazy_collect_lambda_jdk() { return this.positions.getJdkPositions().parallelStream().collect( Collectors.minBy(QUANTITY_COMPARATOR_LAMBDA)).get(); }
public final static <T> Optional<T> min(final Stream<T> stream, final Comparator<? super T> comparator) { return stream.collect(java.util.stream.Collectors.minBy(comparator)); }
public final static<T> Optional<T> min(Stream<T> stream,Comparator<? super T> comparator){ return stream.collect(Collectors.minBy(comparator)); } public final static <T,C extends Comparable<? super C>> Optional<T> maxBy(Stream<T> stream,Function<? super T,? extends C> f){
public final static<T> Optional<T> min(Stream<T> stream,Comparator<? super T> comparator){ return stream.collect(Collectors.minBy(comparator)); } public final static <T,C extends Comparable<? super C>> Optional<T> maxBy(Stream<T> stream,Function<T,C> f){
public final static <T> Optional<T> min(final Stream<T> stream, final Comparator<? super T> comparator) { return stream.collect(java.util.stream.Collectors.minBy(comparator)); }
public final static <T> Optional<T> min(final Stream<T> stream, final Comparator<? super T> comparator) { return stream.collect(java.util.stream.Collectors.minBy(comparator)); }
/** * Find the item for which the supplied projection returns the minimum value (variant for non-naturally-comparable * projected values). * @param projection The projection to apply to each item. * @param comparator The comparator to use to compare the projected values. * @param <T> The type of each item. * @param <Y> The type of the projected value to compare on. * @return The collector. */ public static <T, Y> Collector<T, ?, Optional<T>> minBy(Function<T, Y> projection, Comparator<Y> comparator) { return Collectors.minBy((a, b) -> { Y element1 = projection.apply(a); Y element2 = projection.apply(b); return comparator.compare(element1, element2); }); }
/** * Find the item for which the supplied projection returns the minimum value (variant for non-naturally-comparable * projected values). * @param projection The projection to apply to each item. * @param comparator The comparator to use to compare the projected values. * @param <T> The type of each item. * @param <Y> The type of the projected value to compare on. * @return The collector. */ public static <T, Y> Collector<T, ?, Optional<T>> minBy(Function<T, Y> projection, Comparator<Y> comparator) { return Collectors.minBy((a, b) -> { Y element1 = projection.apply(a); Y element2 = projection.apply(b); return comparator.compare(element1, element2); }); }
import java.util.Optional; import java.util.List; import java.util.Map.Entry; import java.util.stream.Collectors; public static List<Label> getDistinctMinLabels(List<Label> labels) { return labels .stream() .collect( // group by label id // and of each group get the label with the minimum amount Collectors.groupingBy( label -> label.id, Collectors.minBy((l1, l2) -> Double.compare(l1.amount, l2.amount)) ) ) .entrySet() .stream() .map(Entry::getValue) .map(optional -> optional.get()) // Collectors.minBy returns an Optional<Label> .collect(Collectors.toList()); }
Collection<StopTime> filterStopTimes(Set<String> tripIds) throws IOException { return getGtfsCrud().getStopTimes().filter(i -> tripIds.contains(i.getTripId())) .collect(Collectors.groupingBy(StopTime::getTripId)).values().stream().flatMap(i -> { StopTime max = i.stream().collect(Collectors.maxBy(Comparator.comparing(StopTime::getStopSequence))) .get(); StopTime min = i.stream().collect(Collectors.minBy(Comparator.comparing(StopTime::getStopSequence))) .get(); return Stream.of(max, min); }).collect(Collectors.toList()); }
public static void main(String[] args) { List<String> strings = Arrays.asList("this", "is", "a", "long", "list", "of", "strings", "to", "use", "as", "a", "demo"); Map<Integer, List<String>> lengthMap = strings.stream() .collect(groupingBy(String::length)); printMap(lengthMap); Map<Integer, Long> counting = strings.stream() .collect(groupingBy(String::length, counting())); printMap(counting); Map<Integer, Optional<String>> minBy = strings.stream() .collect(groupingBy(String::length, Collectors.minBy(comparingInt(String::length)))); printMap(minBy); Map<Integer, Optional<String>> maxBy = strings.stream() .collect(groupingBy(String::length, Collectors.maxBy(comparingInt(String::length)))); printMap(maxBy); } }
/** * C< – return the minimum-valued output */ public static Object minCollect( EvaluationContext ctx, DecisionTable dt, List<? extends Indexed> matches, List<Object> results) { Object result = generalizedCollect( ctx, dt, results, x -> x.map( y -> (Comparable) y ).collect( minBy( Comparator.naturalOrder() ) ).orElse( null ) ); ctx.notifyEvt( () -> { List<Integer> indexes = Collections.singletonList( matches.get( results.indexOf( result ) ).getIndex() + 1 ); return new DecisionTableRulesSelectedEvent( FEELEvent.Severity.INFO, "Rules fired for decision table '" + dt.getName() + "': " + indexes, dt.getName(), dt.getName(), indexes ); } ); return result; }
private void thenFilesAreWrittenToS3(AmazonS3 s3, String sinkTopic, List<Long> offsets) { waitForPassing(Duration.ofSeconds(10), () -> { List<String> keys = s3.listObjects(S3_BUCKET, S3_PREFIX).getObjectSummaries().stream() .map(S3ObjectSummary::getKey).collect(toList()); Set<Map.Entry<Integer, Long>> partAndOffset = keys.stream() .filter(key -> key.endsWith(".gz") && key.contains(sinkTopic)) .map(key -> immutableEntry(Integer.parseInt(key.replaceAll(".*?-(\\d{5})-\\d{12}\\.gz", "$1")), Long.parseLong(key.replaceAll(".*?-\\d{5}-(\\d{12})\\.gz", "$1")))) .collect(toSet()); Map<Integer, Long> startOffsets = transformValues(partAndOffset.stream() .collect(groupingBy(Map.Entry::getKey, minBy(Map.Entry.comparingByValue()))), (optEntry) -> optEntry.map(Map.Entry::getValue).orElse(0L)); assertTrue(startOffsets + "[0] !~ " + offsets, ofNullable(startOffsets.get(0)).orElse(-1L) >= offsets.get(0)); assertTrue(startOffsets + "[1] !~ " + offsets, ofNullable(startOffsets.get(1)).orElse(-1L) >= offsets.get(1)); }); }