类java.util.IntSummaryStatistics源码实例Demo

下面列出了怎么用java.util.IntSummaryStatistics的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: streamex   文件: IntCollectorTest.java
@Test
public void testSummarizing() {
    withRandom(r -> {
        int[] data = IntStreamEx.of(r, 1000, 1, Integer.MAX_VALUE).toArray();
        IntSummaryStatistics expected = IntStream.of(data).summaryStatistics();
        IntSummaryStatistics statistics = IntStreamEx.of(data).collect(IntCollector.summarizing());
        assertEquals(expected.getCount(), statistics.getCount());
        assertEquals(expected.getSum(), statistics.getSum());
        assertEquals(expected.getMax(), statistics.getMax());
        assertEquals(expected.getMin(), statistics.getMin());
        statistics = IntStreamEx.of(data).parallel().collect(IntCollector.summarizing());
        assertEquals(expected.getCount(), statistics.getCount());
        assertEquals(expected.getSum(), statistics.getSum());
        assertEquals(expected.getMax(), statistics.getMax());
        assertEquals(expected.getMin(), statistics.getMin());
    });
}
 
public void testIntStatistics() {
    List<IntSummaryStatistics> instances = new ArrayList<>();
    instances.add(countTo(1000).stream().collect(Collectors.summarizingInt(i -> i)));
    instances.add(countTo(1000).stream().mapToInt(i -> i).summaryStatistics());
    instances.add(countTo(1000).stream().mapToInt(i -> i).collect(IntSummaryStatistics::new,
                                                                  IntSummaryStatistics::accept,
                                                                  IntSummaryStatistics::combine));
    instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingInt(i -> i)));
    instances.add(countTo(1000).parallelStream().mapToInt(i -> i).summaryStatistics());
    instances.add(countTo(1000).parallelStream().mapToInt(i -> i).collect(IntSummaryStatistics::new,
                                                                          IntSummaryStatistics::accept,
                                                                          IntSummaryStatistics::combine));

    for (IntSummaryStatistics stats : instances) {
        assertEquals(stats.getCount(), 1000);
        assertEquals(stats.getSum(), countTo(1000).stream().mapToInt(i -> i).sum());
        assertEquals(stats.getAverage(), (double) stats.getSum() / stats.getCount());
        assertEquals(stats.getMax(), 1000);
        assertEquals(stats.getMin(), 1);
    }
}
 
源代码3 项目: james-project   文件: ConcurrentTestRunnerTest.java
@Test
void runRandomlyDistributedReactorOperationsShouldRunAllOperationsEvenly() throws ExecutionException, InterruptedException {
    AtomicInteger firstOperationRuns = new AtomicInteger(0);
    AtomicInteger secondOperationRuns = new AtomicInteger(0);
    AtomicInteger thirdOperationRuns = new AtomicInteger(0);
    int threadCount = 10;
    int operationCount = 1000;
    ConcurrentTestRunner.builder()
        .randomlyDistributedReactorOperations(
            (threadNumber, step) -> Mono.fromRunnable(firstOperationRuns::incrementAndGet),
            (threadNumber, step) -> Mono.fromRunnable(secondOperationRuns::incrementAndGet),
            (threadNumber, step) -> Mono.fromRunnable(thirdOperationRuns::incrementAndGet))
        .threadCount(threadCount)
        .operationCount(operationCount)
        .runSuccessfullyWithin(Duration.ofMinutes(1));

    IntSummaryStatistics statistics = IntStream.of(firstOperationRuns.get(), secondOperationRuns.get(), thirdOperationRuns.get()).summaryStatistics();
    int min = statistics.getMin();
    int max = statistics.getMax();

    assertThat(max - min).isLessThan((threadCount * operationCount) * 5 / 100);
}
 
源代码4 项目: james-project   文件: ConcurrentTestRunnerTest.java
@Test
void runRandomlyDistributedOperationsShouldRunAllOperationsEvenly() throws ExecutionException, InterruptedException {
    AtomicInteger firstOperationRuns = new AtomicInteger(0);
    AtomicInteger secondOperationRuns = new AtomicInteger(0);
    AtomicInteger thirdOperationRuns = new AtomicInteger(0);
    int threadCount = 10;
    int operationCount = 1000;
    ConcurrentTestRunner.builder()
        .randomlyDistributedOperations(
            (threadNumber, step) -> firstOperationRuns.incrementAndGet(),
            (threadNumber, step) -> secondOperationRuns.incrementAndGet(),
            (threadNumber, step) -> thirdOperationRuns.incrementAndGet())
        .threadCount(threadCount)
        .operationCount(operationCount)
        .runSuccessfullyWithin(Duration.ofMinutes(1));

    IntSummaryStatistics statistics = IntStream.of(firstOperationRuns.get(), secondOperationRuns.get(), thirdOperationRuns.get()).summaryStatistics();
    int min = statistics.getMin();
    int max = statistics.getMax();

    assertThat(max - min).isLessThan((threadCount * operationCount) * 5 / 100);
}
 
public void testIntStatistics() {
    List<IntSummaryStatistics> instances = new ArrayList<>();
    instances.add(countTo(1000).stream().collect(Collectors.summarizingInt(i -> i)));
    instances.add(countTo(1000).stream().mapToInt(i -> i).summaryStatistics());
    instances.add(countTo(1000).stream().mapToInt(i -> i).collect(IntSummaryStatistics::new,
                                                                  IntSummaryStatistics::accept,
                                                                  IntSummaryStatistics::combine));
    instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingInt(i -> i)));
    instances.add(countTo(1000).parallelStream().mapToInt(i -> i).summaryStatistics());
    instances.add(countTo(1000).parallelStream().mapToInt(i -> i).collect(IntSummaryStatistics::new,
                                                                          IntSummaryStatistics::accept,
                                                                          IntSummaryStatistics::combine));

    for (IntSummaryStatistics stats : instances) {
        assertEquals(stats.getCount(), 1000);
        assertEquals(stats.getSum(), countTo(1000).stream().mapToInt(i -> i).sum());
        assertEquals(stats.getAverage(), (double) stats.getSum() / stats.getCount());
        assertEquals(stats.getMax(), 1000);
        assertEquals(stats.getMin(), 1);
    }
}
 
public void testIntStatistics() {
    List<IntSummaryStatistics> instances = new ArrayList<>();
    instances.add(countTo(1000).stream().collect(Collectors.summarizingInt(i -> i)));
    instances.add(countTo(1000).stream().mapToInt(i -> i).summaryStatistics());
    instances.add(countTo(1000).stream().mapToInt(i -> i).collect(IntSummaryStatistics::new,
                                                                  IntSummaryStatistics::accept,
                                                                  IntSummaryStatistics::combine));
    instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingInt(i -> i)));
    instances.add(countTo(1000).parallelStream().mapToInt(i -> i).summaryStatistics());
    instances.add(countTo(1000).parallelStream().mapToInt(i -> i).collect(IntSummaryStatistics::new,
                                                                          IntSummaryStatistics::accept,
                                                                          IntSummaryStatistics::combine));

    for (IntSummaryStatistics stats : instances) {
        assertEquals(stats.getCount(), 1000);
        assertEquals(stats.getSum(), countTo(1000).stream().mapToInt(i -> i).sum());
        assertEquals(stats.getAverage(), (double) stats.getSum() / stats.getCount());
        assertEquals(stats.getMax(), 1000);
        assertEquals(stats.getMin(), 1);
    }
}
 
源代码7 项目: ffwd   文件: HighFrequencyDetector.java
private int computeTimeDelta(List<Metric> list) {
  int size = list.size();
  IntSummaryStatistics stats = IntStream.range(1, size)
      .map(
          x ->
              (int)
                  (list.get(size - x).getTime().getTime()
                      - list.get(size - x - 1).getTime().getTime()))
      .filter(d -> (d >= 0 && d < minFrequencyMillisAllowed))
      .summaryStatistics();

  int result = -1;

  /**
   * In order to be marked as high frequency metric the number of points
   * should be above the BURST_THRESHOLD.
   * It ignores any small bursts of high frequency metrics.
   */
  if (stats.getCount() > BURST_THRESHOLD) {
    // uses minimal delta time from all consecutive data points
    result = stats.getMin();
    log.info("stats: " + stats);
  }
  return result;
}
 
@Test
public void int_summary_stats_with_stream() {

	IntSummaryStatistics stats = orderEntries.stream()
			.mapToInt((x) -> x.getAmount()).summaryStatistics();

	// average
	assertEquals(13.5, stats.getAverage(), 0);

	// count
	assertEquals(4, stats.getCount(), 0);

	// max
	assertEquals(18, stats.getMax(), 0);

	// min
	assertEquals(10, stats.getMin(), 0);

	// sum
	assertEquals(54, stats.getSum(), 0);
}
 
源代码9 项目: megamek   文件: FormationType.java
private static IntSummaryStatistics damageAtRangeStats(MechSummary ms, int range) {
    List<Integer> retVal = new ArrayList<>();
    for (int i = 0; i < ms.getEquipmentNames().size(); i++) {
        if (EquipmentType.get(ms.getEquipmentNames().get(i)) instanceof WeaponType) {
            final WeaponType weapon = (WeaponType)EquipmentType.get(ms.getEquipmentNames().get(i));
            if (weapon.getLongRange() < range) {
                continue;
            }
            int damage = 0;
            if (weapon.getAmmoType() != AmmoType.T_NA) {
                Optional<EquipmentType> ammo = ms.getEquipmentNames().stream()
                    .map(name -> EquipmentType.get(name))
                    .filter(eq -> eq instanceof AmmoType
                            && ((AmmoType)eq).getAmmoType() == weapon.getAmmoType()
                            && ((AmmoType)eq).getRackSize() == weapon.getRackSize())
                    .findFirst();
                if (ammo.isPresent()) {
                    damage = ((AmmoType)ammo.get()).getDamagePerShot()
                            * Math.max(1, ((AmmoType)ammo.get()).getRackSize());
                }
            } else {
                damage = weapon.getDamage(range);
            }
            if (damage > 0) {
                for (int j = 0; j < ms.getEquipmentQuantities().get(i); j++) {
                    retVal.add(damage);
                }
            }
        }
    }
    return retVal.stream().mapToInt(Integer::intValue).summaryStatistics();
}
 
源代码10 项目: pravega   文件: ReaderCheckpointTest.java
private void verifyEvents(final List<EventRead<Integer>> events, int startInclusive, int endExclusive) {

        Supplier<java.util.stream.Stream<Integer>> streamSupplier = () -> events.stream().map(EventRead::getEvent).sorted();
        IntSummaryStatistics stats = streamSupplier.get().collect(Collectors.summarizingInt(value -> value));

        assertTrue(String.format("Check for first event: %d, %d", stats.getMin(), startInclusive),
                stats.getMin() == startInclusive);
        assertTrue(String.format("Check for last event: %d, %d", stats.getMax(), endExclusive),
                stats.getMax() == endExclusive - 1);
        //Check for missing events
        assertEquals(String.format("Check for number of events: %d, %d, %d", endExclusive, startInclusive, stats.getCount()),
                endExclusive - startInclusive, stats.getCount());
        assertEquals(String.format("Check for duplicate events: %d, %d, %d", endExclusive, startInclusive, streamSupplier.get().distinct().count()),
                endExclusive - startInclusive, streamSupplier.get().distinct().count());
    }
 
源代码11 项目: jdk8u-dev-jdk   文件: SummaryStatisticsTest.java
public void testIntStatistics() {
    List<IntSummaryStatistics> instances = new ArrayList<>();
    instances.add(countTo(1000).stream().collect(Collectors.summarizingInt(i -> i)));
    instances.add(countTo(1000).stream().mapToInt(i -> i).summaryStatistics());
    instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingInt(i -> i)));
    instances.add(countTo(1000).parallelStream().mapToInt(i -> i).summaryStatistics());

    for (IntSummaryStatistics stats : instances) {
        assertEquals(stats.getCount(), 1000);
        assertEquals(stats.getSum(), countTo(1000).stream().mapToInt(i -> i).sum());
        assertEquals(stats.getMax(), 1000);
        assertEquals(stats.getMin(), 1);
    }
}
 
源代码12 项目: symja_android_library   文件: Java8TestCase.java
public void testStatistics() {
	IAST ast = (IAST) F.List.of(10, 11, 12, 13, 14, 15, 16, 17, 18, 19);

	// calculating sum using reduce terminal operator
	int value = ast.stream() //
			.mapToInt(IExpr::toIntDefault) //
			.reduce(0, (total, currentValue) -> total + currentValue);
	assertEquals(145, value);

	value = ast.stream() //
			.mapToInt(IExpr::toIntDefault) //
			.sum();
	assertEquals(145, value);

	long longValue = ast.stream() //
			.mapToInt(IExpr::toIntDefault) //
			.count();
	assertEquals(10, longValue);

	IntSummaryStatistics ageStatistics = ast.stream() //
			.mapToInt(IExpr::toIntDefault) //
			.summaryStatistics();

	assertEquals(14.5, ageStatistics.getAverage());
	assertEquals(10, ageStatistics.getCount());
	assertEquals(19, ageStatistics.getMax());
	assertEquals(10, ageStatistics.getMin());
	assertEquals(145, ageStatistics.getSum());
}
 
public void testIntCollectNull() {
    checkNPE(() -> IntStream.of(1).collect(null,
                                           IntSummaryStatistics::accept,
                                           IntSummaryStatistics::combine));
    checkNPE(() -> IntStream.of(1).collect(IntSummaryStatistics::new,
                                           null,
                                           IntSummaryStatistics::combine));
    checkNPE(() -> IntStream.of(1).collect(IntSummaryStatistics::new,
                                           IntSummaryStatistics::accept,
                                           null));
}
 
源代码14 项目: j2objc   文件: IntSummaryStatisticsTest.java
public void test_combine() {
    IntSummaryStatistics iss1 = getIntSummaryStatisticsData2();
    IntSummaryStatistics issCombined = getIntSummaryStatisticsData1();
    issCombined.combine(iss1);

    assertEquals(12, issCombined.getCount());
    assertEquals(118, issCombined.getSum());
    assertEquals(100, issCombined.getMax());
    assertEquals(-5, issCombined.getMin());
    assertEquals(9.833333d, issCombined.getAverage(), 1E-6);
}
 
源代码15 项目: spring-boot-cookbook   文件: GroupbyTest.java
@Test
public void test() {
    List<String> strings = Arrays.asList("a", "bb", "cc", "ddd");
    Map<Integer, IntSummaryStatistics> result = strings.stream()
            .collect(groupingBy(String::length, summarizingInt(String::hashCode)));
    System.out.println(result);
}
 
源代码16 项目: j2objc   文件: IntSummaryStatisticsTest.java
private static IntSummaryStatistics getIntSummaryStatisticsData1() {
    IntSummaryStatistics iss = new IntSummaryStatistics();
    for (int value : data1) {
        iss.accept(value);
    }
    return iss;
}
 
public static void printTrackLengthStatistics(Album album) {
    IntSummaryStatistics trackLengthStats
            = album.getTracks()
                   .mapToInt(track -> track.getLength())
                   .summaryStatistics();

    System.out.printf("Max: %d, Min: %d, Ave: %f, Sum: %d",
                      trackLengthStats.getMax(),
                      trackLengthStats.getMin(),
                      trackLengthStats.getAverage(),
                      trackLengthStats.getSum());
}
 
public void testIntStatistics() {
    List<IntSummaryStatistics> instances = new ArrayList<>();
    instances.add(countTo(1000).stream().collect(Collectors.summarizingInt(i -> i)));
    instances.add(countTo(1000).stream().mapToInt(i -> i).summaryStatistics());
    instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingInt(i -> i)));
    instances.add(countTo(1000).parallelStream().mapToInt(i -> i).summaryStatistics());

    for (IntSummaryStatistics stats : instances) {
        assertEquals(stats.getCount(), 1000);
        assertEquals(stats.getSum(), countTo(1000).stream().mapToInt(i -> i).sum());
        assertEquals(stats.getMax(), 1000);
        assertEquals(stats.getMin(), 1);
    }
}
 
源代码19 项目: j2objc   文件: IntSummaryStatisticsTest.java
public void test_accept() {
    IntSummaryStatistics iss = new IntSummaryStatistics();
    iss.accept(5);
    assertEquals(1, iss.getCount());
    assertEquals(5, iss.getSum());
    iss.accept(10);
    assertEquals(2, iss.getCount());
    assertEquals(15, iss.getSum());
}
 
源代码20 项目: j2objc   文件: IntSummaryStatisticsTest.java
public void test_empty() {
    IntSummaryStatistics iss = new IntSummaryStatistics();
    assertEquals(0, iss.getCount());
    assertEquals(0, iss.getSum());
    assertEquals(0.0d, iss.getAverage());
    assertEquals(Integer.MAX_VALUE, iss.getMin());
    assertEquals(Integer.MIN_VALUE, iss.getMax());
}
 
源代码21 项目: java-8-lambdas-exercises   文件: Primitives.java
public static void printTrackLengthStatistics(Album album) {
    IntSummaryStatistics trackLengthStats
            = album.getTracks()
                   .mapToInt(track -> track.getLength())
                   .summaryStatistics();

    System.out.printf("Max: %d, Min: %d, Ave: %f, Sum: %d",
                      trackLengthStats.getMax(),
                      trackLengthStats.getMin(),
                      trackLengthStats.getAverage(),
                      trackLengthStats.getSum());
}
 
@Test
public void givenAListOfPosts_whenGroupedByTypeAndSummarizingLikes_thenGetAMapBetweenTypeAndSummary() {
    Map<BlogPostType, IntSummaryStatistics> likeStatisticsPerType = posts.stream()
        .collect(groupingBy(BlogPost::getType, summarizingInt(BlogPost::getLikes)));

    IntSummaryStatistics newsLikeStatistics = likeStatisticsPerType.get(BlogPostType.NEWS);

    assertEquals(2, newsLikeStatistics.getCount());
    assertEquals(50, newsLikeStatistics.getSum());
    assertEquals(25.0, newsLikeStatistics.getAverage(), 0.001);
    assertEquals(35, newsLikeStatistics.getMax());
    assertEquals(15, newsLikeStatistics.getMin());
}
 
源代码23 项目: java8-tutorial   文件: Streams10.java
private static void test4(List<Person> persons) {
    IntSummaryStatistics ageSummary =
        persons
            .stream()
            .collect(Collectors.summarizingInt(p -> p.age));

    System.out.println(ageSummary);
    // IntSummaryStatistics{count=4, sum=76, min=12, average=19,000000, max=23}
}
 
源代码24 项目: dremio-oss   文件: PlanFragmentStats.java
public PlanFragmentStats() {
  sizeByMajorSpecific = new HashMap<>();
  sizeByMinorSpecific = new HashMap<>();
  sizeByMinorAttr = new HashMap<>();
  sizeBySharedAttr = new HashMap<>();
  combinedSize = new IntSummaryStatistics();
}
 
源代码25 项目: hottub   文件: SummaryStatisticsTest.java
public void testIntStatistics() {
    List<IntSummaryStatistics> instances = new ArrayList<>();
    instances.add(countTo(1000).stream().collect(Collectors.summarizingInt(i -> i)));
    instances.add(countTo(1000).stream().mapToInt(i -> i).summaryStatistics());
    instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingInt(i -> i)));
    instances.add(countTo(1000).parallelStream().mapToInt(i -> i).summaryStatistics());

    for (IntSummaryStatistics stats : instances) {
        assertEquals(stats.getCount(), 1000);
        assertEquals(stats.getSum(), countTo(1000).stream().mapToInt(i -> i).sum());
        assertEquals(stats.getMax(), 1000);
        assertEquals(stats.getMin(), 1);
    }
}
 
源代码26 项目: openjdk-8-source   文件: SummaryStatisticsTest.java
public void testIntStatistics() {
    List<IntSummaryStatistics> instances = new ArrayList<>();
    instances.add(countTo(1000).stream().collect(Collectors.summarizingInt(i -> i)));
    instances.add(countTo(1000).stream().mapToInt(i -> i).summaryStatistics());
    instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingInt(i -> i)));
    instances.add(countTo(1000).parallelStream().mapToInt(i -> i).summaryStatistics());

    for (IntSummaryStatistics stats : instances) {
        assertEquals(stats.getCount(), 1000);
        assertEquals(stats.getSum(), countTo(1000).stream().mapToInt(i -> i).sum());
        assertEquals(stats.getMax(), 1000);
        assertEquals(stats.getMin(), 1);
    }
}
 
源代码27 项目: javabase   文件: FirstDdemo.java
private static void getMax() {
    List<Integer> primes = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29);
    IntSummaryStatistics stats = primes.stream().mapToInt((x) -> x)
            .summaryStatistics();
    System.out.println("Highest prime number in List : " + stats.getMax());
    System.out.println("Lowest prime number in List : " + stats.getMin());
    System.out.println("Sum of all prime numbers : " + stats.getSum());
    System.out.println("Average of all prime numbers : " + stats.getAverage());

}
 
源代码28 项目: openjdk-8   文件: SummaryStatisticsTest.java
public void testIntStatistics() {
    List<IntSummaryStatistics> instances = new ArrayList<>();
    instances.add(countTo(1000).stream().collect(Collectors.summarizingInt(i -> i)));
    instances.add(countTo(1000).stream().mapToInt(i -> i).summaryStatistics());
    instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingInt(i -> i)));
    instances.add(countTo(1000).parallelStream().mapToInt(i -> i).summaryStatistics());

    for (IntSummaryStatistics stats : instances) {
        assertEquals(stats.getCount(), 1000);
        assertEquals(stats.getSum(), countTo(1000).stream().mapToInt(i -> i).sum());
        assertEquals(stats.getMax(), 1000);
        assertEquals(stats.getMin(), 1);
    }
}
 
源代码29 项目: map-visualiser   文件: HashMapVisualiser.java
public void visualise(final HashMap<?, ?> map)
{
    Object[] table = (Object[]) Type.get(tableField, map);

    console.printf(
        "Size: %d, Resize: %s, Bin Count: %d%n",
        map.size(), Type.get(thresholdField, map), table.length);

    final IntSummaryStatistics collisions = Stream.of(table)
        .mapToInt(row ->
        {
            if (row == null)
            {
                console.println("[]");
                return 0;
            }

            switch (row.getClass().getSimpleName())
            {
                case "TreeNode":
                    return visualiseTree(row);

                case "Node":
                    return visualiseList(row);

                default:
                    throw new IllegalArgumentException("Unknown type of row");
            }
        })
        .summaryStatistics();

    console.printf(
        "Collisions: Max: %d, Ave: %s, Total: %d%n",
        collisions.getMax(),
        collisions.getAverage(),
        collisions.getSum());
}
 
public void testIntCollectNull() {
    checkNPE(() -> IntStream.of(1).collect(null,
                                           IntSummaryStatistics::accept,
                                           IntSummaryStatistics::combine));
    checkNPE(() -> IntStream.of(1).collect(IntSummaryStatistics::new,
                                           null,
                                           IntSummaryStatistics::combine));
    checkNPE(() -> IntStream.of(1).collect(IntSummaryStatistics::new,
                                           IntSummaryStatistics::accept,
                                           null));
}
 
 类所在包
 类方法
 同包方法