工具類——自定義Collections集合方法
專案中有需要多次統計 某些集合中 的某個屬性值,所以考慮封裝一個方法,讓其其定義實現計算方式。 話不多說,看程式碼:
1、封裝的自定義集合工具類:CollectionsCustom
package com.test.util;
import java.util.Collection;
import org.apache.commons.collections.CollectionUtils;
/**
* 自定義集合處理類
* @author : shijing
* 2017年5月18日下午2:00:10
*/
public class CollectionsCustom {
/**
* 將傳入的collection內物件進行計算後得出結果
* @author : shijing
* 2017年5月18日下午3:09:53
* @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;
}
/**
* 自定義計算介面
* @author : shijing
* 2017年5月18日下午3:09:53
* @param <Input>
* @param <Result>
*/
public interface ReduceFunction<Input, Result> {
Result apply(Input input, Result lastResult);
}
}
2、測試類TestCollections
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 類)Java
- 7、Collections集合工具類
- Java 集合類——Collections(1)Java
- Java 集合類——Collections(3)Java
- Java 集合類——Collections(2)Java
- Collections工具類
- 6、Collections工具類
- Java之Collections工具類Java
- JavaSE基礎:Collections工具類Java
- 集合框架-Collections框架
- Collections工具類,可以使用collections工具類對程式碼中的list進行分組
- 集合框架-Arrays工具類的asList()方法(把陣列轉成集合)框架陣列
- iOS CAAnimation之CATransition (自定義轉場動畫工具類)iOS動畫
- Java 常用工具類 Collections 原始碼分析Java原始碼
- 自定義異常類
- mybatis自定義List集合解析器MyBatis
- 集合框架-去重自定義物件案例框架物件
- IDEA自定義類註釋和方法註釋(自定義groovyScript方法實現多行引數註釋)Idea
- 集合框架-ArrayList集合儲存自定義物件的排序案例框架物件排序
- 為Autodesk Viewer新增自定義工具條的更好方法View
- [Guava] Google Guava 集合工具類GuavaGo
- WebService中使用自定義類的解決方法(5種)Web
- Java集合系列(一):集合的定義及分類Java
- 『政善治』Postman工具 — 7、Postman中儲存請求(Collections集合)Postman
- 【Java學習筆記】Collections集合Java筆記
- 直播軟體開發,工具類的自定義彈窗效果
- 自定義動畫方法animate()動畫
- Java類方法(定義一個工具類,儲存一些常用的方法)Java
- Django(62)自定義認證類Django
- 自定義實現Complex類
- Java的自定義異常類Java
- python 魔術方法 : 讓自定義類更像內建型別Python型別
- 用鬥地主的例項學會使用java Collections工具類Java
- 一個工具類實現自定義Tablayout的下劃線寬度TabLayout
- Collections sort()排序方法排序
- 谷歌開發者工具自定義佈局谷歌
- QingScan 快速整合自定義工具
- 自定義視覺化除錯工具視覺化除錯