一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - JAVA集合框架工具類自定義Collections集合方法

JAVA集合框架工具類自定義Collections集合方法

2021-06-20 14:14執筆記憶的空白 Java教程

今天小編就為大家分享一篇關于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等,最好自己做下非空處理。相信高質量的封裝代碼能為你自己加分的!

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美综合国产精品日韩一 | 国产一区二区不卡视频 | 精品国产中文字幕在线视频 | yy8090韩国日本三理论免费 | 亚洲欧美专区精品久久 | 视频免费在线 | 美女和男人免费网站视频 | 午夜欧美福利视频 | 超91精品手机国产在线 | 99久久精品6在线播放 | 国产在线步兵一区二区三区 | 午夜精品久视频在线观看 | 草草在线免费视频 | 精品9e精品视频在线观看 | 亚洲国产AV无码综合在线 | 日韩欧美推理片免费看完整版 | 荡女人人爱 | 精品麻豆| 免费被靠视频动漫 | 视频一区国产精戏刘婷30 | 奇米影视一区 | 欧美贵妇vs高跟办公室 | 亚洲小视频在线 | 亚洲精品九色在线网站 | 色综合图片 | 国产精品视频一区二区三区经 | 天天色综合色 | 色婷婷久久综合中文久久一本 | 亚洲成人网导航 | 2020国产精品亚洲综合网 | 日韩成人免费aa在线看 | 国产精品乱码高清在线观看 | 免费看成人毛片日本久久 | 日本久久免费大片 | 国产精品国产精品国产三级普 | 国产精品igao视频网网址 | juy_661佐佐木明希在线播放 | www免费看| 免费网站看v片在线成人国产系列 | 波多野结衣xxxxx在线播放 | 免费在线看a |