在 Java 8 中查找列表的最大值、最小值、总和和平均值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25988707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find maximum, minimum, sum and average of a list in Java 8
提问by DrJava
How to find the maximum, minimum, sum and average of the numbers in the following list in Java 8?
如何在Java 8中找到以下列表中数字的最大值,最小值,总和和平均值?
List<Integer> primes = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29);
采纳答案by Kick Buttowski
There is a class name, IntSummaryStatistics
有一个类名, IntSummaryStatistics
For example:
例如:
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(stats);
Output:
输出:
IntSummaryStatistics{count=10, sum=129, min=2, average=12.900000, max=29}
Hope it helps
希望能帮助到你
回答by KeyMaker00
I think it's always good to know more than one solution to a problem to pick the right that suits the problem the most. Here are some other solutions:
我认为知道一个问题的多个解决方案来选择最适合问题的解决方案总是好的。以下是一些其他解决方案:
final List<Integer> primes = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29);
Find the maximum
找到最大值
// MAX -- Solution 1
primes.stream() //
.max(Comparator.comparing(i -> i)) //
.ifPresent(max -> System.out.println("Maximum found is " + max));
// MAX -- Solution 2
primes.stream() //
.max((i1, i2) -> Integer.compare(i1, i2)) //
.ifPresent(max -> System.out.println("Maximum found is " + max));
// MAX -- Solution 3
int max = Integer.MIN_VALUE;
for (int i : primes) {
max = (i > max) ? i : max;
}
if (max == Integer.MIN_VALUE) {
System.out.println("No result found");
} else {
System.out.println("Maximum found is " + max);
}
// MAX -- Solution 4
max = Collections.max(primes);
System.out.println("Maximum found is " + max);
Find the minimum
找到最小值
// MIN -- Solution 1
primes.stream() //
.min(Comparator.comparing(i -> i)) //
.ifPresent(min -> System.out.println("Minimum found is " + min));
// MIN -- Solution 2
primes.stream() //
.max(Comparator.comparing(i -> -i)) //
.ifPresent(min -> System.out.println("Minimum found is " + min));
// MIN -- Solution 3
int min = Integer.MAX_VALUE;
for (int i : primes) {
min = (i < min) ? i : min;
}
if (min == Integer.MAX_VALUE) {
System.out.println("No result found");
} else {
System.out.println("Minimum found is " + min);
}
// MIN -- Solution 4
min = Collections.min(primes);
System.out.println("Minimum found is " + min);
Find the average
求平均值
// AVERAGE -- Solution 1
primes.stream() //
.mapToInt(i -> i) //
.average() //
.ifPresent(avg -> System.out.println("Average found is " + avg));
// AVERAGE -- Solution 2
int sum = 0;
for (int i : primes) {
sum+=i;
}
if(primes.isEmpty()){
System.out.println("List is empty");
} else {
System.out.println("Average found is " + sum/(float)primes.size());
}
Find the sum
求和
// SUM -- Solution 1
int sum1 = primes.stream() //
.mapToInt(i -> i) //
.sum(); //
System.out.println("Sum found is " + sum1);
// SUM -- Solution 2
int sum2 = 0;
for (int i : primes) {
sum2+=i;
}
System.out.println("Sum found is " + sum2);
But be as consice as possible, so my favourites are:
但要尽可能简洁,所以我最喜欢的是:
// Find a maximum with java.Collections
Collections.max(primes);
// Find a minimum with java.Collections
Collections.min(primes);
By the way, Oracle Tutorial is a golden mine: https://docs.oracle.com/javase/tutorial/collections/streams/reduction.html
顺便说一句,Oracle 教程是一个金矿:https: //docs.oracle.com/javase/tutorial/collections/streams/reduction.html