項目中有需要多次統計 某些集合中 的某個屬性值,所以考慮封裝一個方法,讓其其定義實現計算方式。 話不多說,看代碼:
1、封裝的自定義集合工具類:collectionscustom
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package com.test.util; import java.util.collection; import org.apache.commons.collections.collectionutils; /** * 自定義集合處理類 */ public class collectionscustom { /** * 將傳入的collection內對象進行計算后得出結果 * @param original 計算前collection * @param reducefunction 計算方式 * @param initvalue 計算結果初始值 * @param <input> collection對象類型 * @param <output> 結果類型 * @return */ public static <input, output> output reduce(collection<input> original, output initvalue, reducefunction<input, output> reducefunction) { output result = initvalue; if (collectionutils.isempty(original)) { return result; } if (reducefunction == null ) { return result; } for (input input : original) { result = reducefunction.apply(input, result); } return result; } /** * 自定義計算接口 * @param <input> * @param <result> */ public interface reducefunction<input, result> { result apply(input input, result lastresult); } } |
2、測試類testcollections
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
package com.test; import java.math.bigdecimal; import java.util.arrays; import java.util.list; import com.test.util.collectionscustom; public class testcollection { private static list<user> list = arrays.aslist( new user( "張三" , bigdecimal.valueof( 35.6 ), 18 ), new user( "李四" , bigdecimal.valueof( 85 ), 30 ), new user( "趙六" , bigdecimal.valueof( 66.55 ), 25 )); public static void main(string[] args) { //統計集合內分數之和 testtotalscore(); //統計集合內年齡之和 testtotalage(); } private static void testtotalscore(){ //統計集合內分數之和 bigdecimal totalscore = collectionscustom.reduce(list, bigdecimal.zero, new collectionscustom.reducefunction<user, bigdecimal>() { @override public bigdecimal apply(user input, bigdecimal lastresult) { // todo auto-generated method stub return lastresult.add(input.getscore()); } }); system.out.println( "總共分數:" + totalscore); } private static void testtotalage(){ //統計集合內年齡之和 integer totalage = collectionscustom.reduce(list, 0 , new collectionscustom.reducefunction<user, integer>() { @override public integer apply(user input, integer lastresult) { // todo auto-generated method stub return lastresult += input.getage(); } }); system.out.println( "總共年齡:" + totalage); } static class user{ private string username; //姓名 private bigdecimal score; //分數 private integer age; public string getusername() { return username; } public void setusername(string username) { this .username = username; } public bigdecimal getscore() { return score; } public void setscore(bigdecimal score) { this .score = score; } public integer getage() { return age; } public void setage(integer age) { this .age = age; } public user(string username, bigdecimal score, integer age) { super (); this .username = username; this .score = score; this .age = age; } public user() { // todo auto-generated constructor stub } } } |
3、測試輸出結果:
總共分數:187.15
總共年齡:73
這里如果傳入的是封裝類型integer
等,最好自己做下非空處理。相信高質量的封裝代碼能為你自己加分的!
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接