JAVA集合框架工具類自定義Collections集合方法
項目中有需要多次統(tǒng)計 某些集合中 的某個屬性值,所以考慮封裝一個方法,讓其其定義實現(xiàn)計算方式。 話不多說,看代碼:
1、封裝的自定義集合工具類:CollectionsCustom
package com.test.util;
import java.util.Collection;
import org.apache.commons.collections.CollectionUtils;
/**
* 自定義集合處理類
*/
public class CollectionsCustom {
/**
* 將傳入的collection內(nèi)對象進行計算后得出結果
* @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
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) {
//統(tǒng)計集合內(nèi)分數(shù)之和
testTotalScore();
//統(tǒng)計集合內(nèi)年齡之和
testTotalAge();
}
private static void testTotalScore(){
//統(tǒng)計集合內(nèi)分數(shù)之和
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("總共分數(shù):" + totalScore);
}
private static void testTotalAge(){
//統(tǒng)計集合內(nèi)年齡之和
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;//分數(shù)
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、測試輸出結果:
總共分數(shù):187.15
總共年齡:73
這里如果傳入的是封裝類型Integer等,最好自己做下非空處理。相信高質量的封裝代碼能為你自己加分的!
總結
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關內(nèi)容請查看下面相關鏈接
相關文章
java+SQL server2008學生信息管理系統(tǒng)源碼
這篇文章主要為大家詳細介紹了java+SQL server2008學生信息管理系統(tǒng)源碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01
springboot yml配置文件使用@project.xxxx@啟動報錯Do not
這篇文章主要介紹了springboot yml配置文件使用@project.xxxx@啟動報錯Do not use @ for indentation問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
基于Java的度分秒坐標轉純經(jīng)緯度坐標的漂亮國基地信息管理的方法
本文以java語言為例,詳細介紹如何管理漂亮國的基地信息,為下一步全球的空間可視化打下堅實的基礎,首先介紹如何對數(shù)據(jù)進行去重處理,然后介紹在java當中如何進行度分秒位置的轉換,最后結合實現(xiàn)原型進行詳細的說明,感興趣的朋友跟隨小編一起看看吧2024-06-06
SpringBoot Filter修改返回內(nèi)容,解決請求卡死200的錯誤
這篇文章主要介紹了SpringBoot Filter修改返回內(nèi)容,解決請求卡死200的錯誤問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07

