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

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

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

Collectors.minBy介绍

暂无

代码示例

代码示例来源:origin: goldmansachs/gs-collections

@Benchmark
public Position minByMarketValue_serial_lazy_collect_methodref_jdk()
{
  return this.positions.getJdkPositions().stream().collect(
      Collectors.minBy(MARKET_VALUE_COMPARATOR_METHODREF)).get();
}

代码示例来源:origin: goldmansachs/gs-collections

@Benchmark
public Position minByMarketValue_serial_lazy_collect_lambda_jdk()
{
  return this.positions.getJdkPositions().stream().collect(
      Collectors.minBy(MARKET_VALUE_COMPARATOR_LAMBDA)).get();
}

代码示例来源:origin: goldmansachs/gs-collections

@Benchmark
public Position minByQuantity_serial_lazy_collect_methodref_jdk()
{
  return this.positions.getJdkPositions().stream().collect(
      Collectors.minBy(QUANTITY_COMPARATOR_METHODREF)).get();
}

代码示例来源:origin: goldmansachs/gs-collections

@Benchmark
public Position minByQuantity_serial_lazy_collect_lambda_jdk()
{
  return this.positions.getJdkPositions().stream().collect(
      Collectors.minBy(QUANTITY_COMPARATOR_LAMBDA)).get();
}

代码示例来源:origin: goldmansachs/gs-collections

@Benchmark
public Position minByQuantity_parallel_lazy_collect_methodref_jdk()
{
  return this.positions.getJdkPositions().parallelStream().collect(
      Collectors.minBy(QUANTITY_COMPARATOR_METHODREF)).get();
}

代码示例来源:origin: goldmansachs/gs-collections

@Benchmark
public Position minByMarketValue_parallel_lazy_collect_methodref_jdk()
{
  return this.positions.getJdkPositions().parallelStream().collect(
      Collectors.minBy(MARKET_VALUE_COMPARATOR_METHODREF)).get();
}

代码示例来源:origin: goldmansachs/gs-collections

@Benchmark
public Position minByMarketValue_parallel_lazy_collect_lambda_jdk()
{
  return this.positions.getJdkPositions().parallelStream().collect(
      Collectors.minBy(MARKET_VALUE_COMPARATOR_LAMBDA)).get();
}

代码示例来源:origin: goldmansachs/gs-collections

@Benchmark
public Position minByQuantity_parallel_lazy_collect_lambda_jdk()
{
  return this.positions.getJdkPositions().parallelStream().collect(
      Collectors.minBy(QUANTITY_COMPARATOR_LAMBDA)).get();
}

代码示例来源:origin: aol/cyclops

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));
}

代码示例来源:origin: com.aol.cyclops/cyclops-streams

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){

代码示例来源:origin: com.aol.cyclops/cyclops-base

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){

代码示例来源:origin: com.aol.simplereact/cyclops-react

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));
}

代码示例来源:origin: com.oath.cyclops/cyclops

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));
}

代码示例来源:origin: poetix/protonpack

/**
 * 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);
  });
}

代码示例来源:origin: com.codepoetics/protonpack

/**
 * 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);
  });
}

代码示例来源:origin: stackoverflow.com

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());   
}

代码示例来源:origin: hasadna/open-bus

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());
}

代码示例来源:origin: kousen/java_8_recipes

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);
  }
}

代码示例来源:origin: org.kie/kie-dmn-feel

/**
 * 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;
}

代码示例来源:origin: spredfast/kafka-connect-s3

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));
  });
}

相关文章