use of java8.util.IntSummaryStatistics in project streamsupport by stefan-zobel.
the class CollectAndSummaryStatisticsTest method testIntStatistics.
public void testIntStatistics() {
List<IntSummaryStatistics> instances = new ArrayList<>();
instances.add(StreamSupport.stream(countTo(1000)).collect(Collectors.summarizingInt(i -> i)));
instances.add(StreamSupport.stream(countTo(1000)).mapToInt(i -> i).summaryStatistics());
instances.add(StreamSupport.stream(countTo(1000)).mapToInt(i -> i).collect(IntSummaryStatistics::new, IntSummaryStatistics::accept, IntSummaryStatistics::combine));
instances.add(StreamSupport.stream(countTo(1000)).mapToInt(i -> i).collect(() -> new IntSummaryStatistics(0, -1, 1001, 2), IntSummaryStatistics::accept, IntSummaryStatistics::combine));
instances.add(StreamSupport.parallelStream(countTo(1000)).collect(Collectors.summarizingInt(i -> i)));
instances.add(StreamSupport.parallelStream(countTo(1000)).mapToInt(i -> i).summaryStatistics());
instances.add(StreamSupport.parallelStream(countTo(1000)).mapToInt(i -> i).collect(IntSummaryStatistics::new, IntSummaryStatistics::accept, IntSummaryStatistics::combine));
instances.add(StreamSupport.parallelStream(countTo(1000)).mapToInt(i -> i).collect(() -> new IntSummaryStatistics(0, -1, 1001, 2), IntSummaryStatistics::accept, IntSummaryStatistics::combine));
IntSummaryStatistics original = instances.get(0);
instances.add(new IntSummaryStatistics(original.getCount(), original.getMin(), original.getMax(), original.getSum()));
for (IntSummaryStatistics stats : instances) {
assertEquals(stats.getCount(), 1000);
assertEquals(stats.getSum(), StreamSupport.stream(countTo(1000)).mapToInt(i -> i).sum());
assertEquals(stats.getAverage(), (double) stats.getSum() / stats.getCount());
assertEquals(stats.getMax(), 1000);
assertEquals(stats.getMin(), 1);
}
expectThrows(IllegalArgumentException.class, () -> new IntSummaryStatistics(-1, 0, 0, 0));
expectThrows(IllegalArgumentException.class, () -> new IntSummaryStatistics(1, 3, 2, 0));
}
use of java8.util.IntSummaryStatistics in project streamsupport by stefan-zobel.
the class SummaryStatisticsTest method testIntStatistics.
public void testIntStatistics() {
List<IntSummaryStatistics> instances = new ArrayList<>();
instances.add(StreamSupport.stream(countTo(1000)).collect(Collectors.summarizingInt(i -> i)));
instances.add(StreamSupport.stream(countTo(1000)).mapToInt(i -> i).summaryStatistics());
instances.add(StreamSupport.stream(countTo(1000), 0, true).collect(Collectors.summarizingInt(i -> i)));
instances.add(StreamSupport.stream(countTo(1000), 0, true).mapToInt(i -> i).summaryStatistics());
for (IntSummaryStatistics stats : instances) {
assertEquals(stats.getCount(), 1000);
assertEquals(stats.getSum(), StreamSupport.stream(countTo(1000)).mapToInt(i -> i).sum());
assertEquals(stats.getMax(), 1000);
assertEquals(stats.getMin(), 1);
}
}
Aggregations