好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

JAVA集合框架工具类自定义Collections集合方法

项目中有需要多次统计 某些集合中 的某个属性值,所以考虑封装一个方法,让其其定义实现计算方式。 话不多说,看代码:

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 等,最好自己做下非空处理。相信高质量的封装代码能为你自己加分的!

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接

查看更多关于JAVA集合框架工具类自定义Collections集合方法的详细内容...

  阅读:7次