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

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

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

IntStream.average介绍

[英]Returns an OptionalDouble describing the arithmetic mean of elements of this stream, or an empty optional if this stream is empty. This is a special case of a reduction.

This is a terminal operation.
[中]返回描述此流元素算术平均值的OptionalDouble,如果此流为空,则返回空可选值。这是reduction的一个特例。
这是一个terminal operation

代码示例

代码示例来源:origin: biezhi/30-seconds-of-java8

public static double average(int[] arr) {
  return IntStream.of(arr)
      .average()
      .orElseThrow(() -> new IllegalArgumentException("Array is empty"));
}

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

default OptionalDouble average(IntPipeline pipeline) {
  requireNonNull(pipeline);
  return optimize(pipeline).getAsIntStream().average();
}

代码示例来源:origin: Vedenin/useful-java-links

mapToInt(People::getAge).average().getAsDouble();
System.out.println("manAverageAge = " + manAverageAge); // print  manAverageAge = 36.0

代码示例来源:origin: shekhargulati/30-seconds-of-java

public static double average(int[] arr) {
  return IntStream.of(arr)
      .average()
      .orElseThrow(() -> new IllegalArgumentException("Array is empty"));
}

代码示例来源:origin: matyb/java-koans

@Koan
public void mapReduce() {
  OptionalDouble averageLengthOptional = places.stream()
      .mapToInt(String::length)
      .average();
  double averageLength = Math.round(averageLengthOptional.getAsDouble());
  assertEquals(averageLength, __);
}

代码示例来源:origin: mulesoft/mule

private void assertRetry() {
 assertThat(executedRetries.get(), is(RETRIES + 1));
 assertThat(executionMomentDeltas.stream().mapToInt(Long::intValue).average().getAsDouble(),
       anyOf(greaterThanOrEqualTo(FREQUENCY.doubleValue()), lessThanOrEqualTo(FREQUENCY.doubleValue() * 1.3)));
}

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

@Test
public void foldInt(){
  Double res= ReactiveSeq.range(1, 1000).mapToInt(i->i).map(i->i*2).filter(i->i<500).average().getAsDouble();
  assertThat(res,equalTo(250d));
}
@Test

代码示例来源:origin: se.ugli.ugli-commons/ugli-commons

@Override
public OptionalDouble average() {
  // This is a terminal operation
  return evalAndclose(() -> stream.average());
}

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

String commaSeparatedString = "1,2,3,4,5";
String[] numbers = commaSeparatedString.split(",");
Stream<String> stringStream = Arrays.stream(numbers);
IntStream intStream = stringStream.mapToInt(value -> Integer.parseInt(value));
OptionalDouble average = intStream.average();
System.out.println(average.isPresent() ? 
           "average = " + average.getAsDouble() : 
           "stream is empty!")

代码示例来源:origin: dunwu/javacore

private static void test3() {
  IntStream
    .range(0, 10)
    .average()
    .ifPresent(System.out::println);
}

代码示例来源:origin: dunwu/javacore

private static void test1() {
    int[] ints = {1, 3, 5, 7, 11};
    Arrays
      .stream(ints)
      .average()
      .ifPresent(System.out::println);
  }
}

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

public int average(int[] grades)
{
  OptionalDouble od =  Arrays.stream(grades).average();
  if(od.isPresent())
  {
    return (int)od.getAsDouble();
  }
  return 0; //error condition to be checked by calling method.
}

代码示例来源:origin: org.restcomm.imscf/imscf-common

private int updateAndAverageCpuPercent() {
    int currentCpu = getCpuPercentage();
    cpuUsageHistory.set(cpuUsageHistoryIndex++ % parameters.getCpuMeasurementWindow(), currentCpu);
    return (int) Math.round(cpuUsageHistory.stream().mapToInt(i -> i).average().getAsDouble());
  }
}

代码示例来源:origin: chungkwong/MathOCR

@Override
  public int getFontSize(){
    return (int)matrix.stream().flatMap((line)->line.stream()).
        mapToInt((cell)->cell.getFontSize()).average().orElse(Symbol.DEFAULT_SIZE);
  }
}

代码示例来源:origin: trekawek/radioblock

@Override
  protected Short computeNext() {
    for (int i = 0; i < localBuffer.length; i++) {
      if (it.hasNext()) {
        localBuffer[i] = it.next();
      } else {
        return endOfData();
      }
    }
    return (short) IntStream.of(localBuffer).average().orElse(0);
  }
}

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

final int numberOfRandom = 10;
final int min = 0;
final int max = 50;
final Random random = new Random();
System.out.println("The ave is: "+random.ints(min, max).limit(numberOfRandom).average());

代码示例来源:origin: BruceEckel/OnJava8-Examples

public static void main(String[] args) {
  System.out.println(rands().average().getAsDouble());
  System.out.println(rands().max().getAsInt());
  System.out.println(rands().min().getAsInt());
  System.out.println(rands().sum());
  System.out.println(rands().summaryStatistics());
 }
}

代码示例来源:origin: addthis/hydra

/**
 * Calculate the availability-domain-score of a task moving from one host to another.
 * A lower score is a better move to make, because it more distant.
 */
private double calculateTaskScore(HostLocation fromHostLocation, HostLocation toHostLocation, JobTask task) {
  // get average distance score for all tasks host locations (except any on the fromHostLocation)
  return task.getAllTaskHosts()
        .stream()
        .map(h -> hostManager.getHostState(h).getHostLocation())
        .filter(hl -> !hl.equals(fromHostLocation))
        .mapToInt(toHostLocation::assignScoreByHostLocation)
        .average().orElse(0D);
}

代码示例来源:origin: one.util/streamex

@Override
public OptionalDouble average() {
  if (context.fjp != null)
    return context.terminate(stream()::average);
  return stream().average();
}

代码示例来源:origin: org.mule.runtime/mule-core-tests

private void assertRetry() {
 assertThat(executedRetries.get(), is(RETRIES + 1));
 assertThat(executionMomentDeltas.stream().mapToInt(Long::intValue).average().getAsDouble(),
       anyOf(greaterThanOrEqualTo(FREQUENCY.doubleValue()), lessThanOrEqualTo(FREQUENCY.doubleValue() * 1.3)));
}

相关文章

微信公众号

最新文章

更多