2014-09-23 57 views
-4

如何在Java 8中查找以下列表中的最大值,最小值,總和和平均值?在Java 8中查找列表的最大值,最小值,總和和平均值

List<Integer> primes = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29); 
+3

你可以通過編寫一個應用程序來完成。你試過什麼了? – BackSlash 2014-09-23 06:53:14

+0

以下是您所需要的全部內容:http://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html – micha 2014-09-23 06:55:05

+0

以下列表是什麼?你打算髮布一些代碼嗎? – 2014-09-23 06:55:44

回答

34

有一類的名字,IntSummaryStatistics

例如:

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); 

輸出:

IntSummaryStatistics{count=10, sum=129, min=2, average=12.900000, max=29} 

希望它可以幫助

Read about IntSummaryStatistics

+2

+1來解答使用IntSummaryStatistics – sol4me 2014-09-23 07:00:48

+1

不是一個明顯的,但非常好的答案。我喜歡'IntSummaryStatistics'的東西。 – 2014-10-27 18:40:51

+3

我發現它告訴了很多關於java的東西,當你看到一行像'.mapToInt((x) - > x)' – njzk2 2015-05-14 18:31:11

0

我認爲了解問題的多個解決方案總是很好,可以選擇最適合問題的權利。這裏有一些其他的解決方案:

final List<Integer> primes = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29); 

找出最大

// 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); 

找出最小

// 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); 

查找平均

// 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());   
} 

查找總和

// 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); 

但像consice越好,所以我最喜歡的是:

// Find a maximum with java.Collections 
Collections.max(primes); 

// Find a minimum with java.Collections 
Collections.min(primes); 

順便說一句,甲骨文教程是一個金礦:https://docs.oracle.com/javase/tutorial/collections/streams/reduction.html

相關問題