下面列出了java.util.function.ToIntBiFunction#java.util.IntSummaryStatistics 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
}
@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);
}
@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);
}
}
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);
}
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();
}
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());
}
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);
}
}
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));
}
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);
}
@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);
}
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);
}
}
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());
}
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());
}
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());
}
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}
}
public PlanFragmentStats() {
sizeByMajorSpecific = new HashMap<>();
sizeByMinorSpecific = new HashMap<>();
sizeByMinorAttr = new HashMap<>();
sizeBySharedAttr = new HashMap<>();
combinedSize = new IntSummaryStatistics();
}
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);
}
}
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);
}
}
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());
}
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);
}
}
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));
}