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內對象進行計算后得出結果
* @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)計集合內分數(shù)之和
testTotalScore();
//統(tǒng)計集合內年齡之和
testTotalAge();
}
private static void testTotalScore(){
//統(tǒng)計集合內分數(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)計集合內年齡之和
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等,最好自己做下非空處理。相信高質量的封裝代碼能為你自己加分的!
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
相關文章
SpringMVC中的HandlerMapping和HandlerAdapter詳解
這篇文章主要介紹了SpringMVC中的HandlerMapping和HandlerAdapter詳解,在Spring MVC中,HandlerMapping(處理器映射器)用于確定請求處理器對象,請求處理器可以是任何對象,只要它們使用了@Controller注解或注解@RequestMapping,需要的朋友可以參考下2023-08-08
解決SpringBoot項目使用多線程處理任務時無法通過@Autowired注入bean問題
這篇文章主要介紹了SpringBoot項目使用多線程處理任務時無法通過@Autowired注入bean問題的解決方法,需要的朋友可以參考下2018-09-09
SpringBoot使用JdbcTemplate操作數(shù)據(jù)庫
這篇文章主要介紹了SpringBoot使用JdbcTemplate操作數(shù)據(jù)庫,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07
教你用Java Swing實現(xiàn)自助取款機系統(tǒng)
今天給大家?guī)淼氖顷P于JAVA的相關知識,文章圍繞著如何用Java Swing實現(xiàn)自助取款機系統(tǒng)展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下2021-06-06

