JAVA集合框架工具類自定義Collections集合方法
項(xiàng)目中有需要多次統(tǒng)計(jì) 某些集合中 的某個(gè)屬性值,所以考慮封裝一個(gè)方法,讓其其定義實(shí)現(xiàn)計(jì)算方式。 話不多說,看代碼:
1、封裝的自定義集合工具類:CollectionsCustom
package com.test.util; import java.util.Collection; import org.apache.commons.collections.CollectionUtils; /** * 自定義集合處理類 */ public class CollectionsCustom { /** * 將傳入的collection內(nèi)對(duì)象進(jìn)行計(jì)算后得出結(jié)果 * @param original 計(jì)算前collection * @param reduceFunction 計(jì)算方式 * @param initValue 計(jì)算結(jié)果初始值 * @param <Input> collection對(duì)象類型 * @param <Output> 結(jié)果類型 * @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; } /** * 自定義計(jì)算接口 * @param <Input> * @param <Result> */ public interface ReduceFunction<Input, Result> { Result apply(Input input, Result lastResult); } }
2、測(cè)試類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)計(jì)集合內(nèi)分?jǐn)?shù)之和 testTotalScore(); //統(tǒng)計(jì)集合內(nèi)年齡之和 testTotalAge(); } private static void testTotalScore(){ //統(tǒng)計(jì)集合內(nèi)分?jǐn)?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("總共分?jǐn)?shù):" + totalScore); } private static void testTotalAge(){ //統(tǒng)計(jì)集合內(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;//分?jǐn)?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、測(cè)試輸出結(jié)果:
總共分?jǐn)?shù):187.15
總共年齡:73
這里如果傳入的是封裝類型Integer
等,最好自己做下非空處理。相信高質(zhì)量的封裝代碼能為你自己加分的!
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
SpringMVC中的HandlerMapping和HandlerAdapter詳解
這篇文章主要介紹了SpringMVC中的HandlerMapping和HandlerAdapter詳解,在Spring MVC中,HandlerMapping(處理器映射器)用于確定請(qǐng)求處理器對(duì)象,請(qǐng)求處理器可以是任何對(duì)象,只要它們使用了@Controller注解或注解@RequestMapping,需要的朋友可以參考下2023-08-08解決SpringBoot項(xiàng)目使用多線程處理任務(wù)時(shí)無法通過@Autowired注入bean問題
這篇文章主要介紹了SpringBoot項(xiàng)目使用多線程處理任務(wù)時(shí)無法通過@Autowired注入bean問題的解決方法,需要的朋友可以參考下2018-09-09Java處理圖片實(shí)現(xiàn)base64編碼轉(zhuǎn)換
這篇文章主要介紹了Java處理圖片實(shí)現(xiàn)base64編碼轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02SpringBoot項(xiàng)目如何打可執(zhí)行war包
最近小編做了一個(gè)springboot項(xiàng)目,最后需要打成war包在容器中部署,下面小編給大家分享下SpringBoot項(xiàng)目如何打可執(zhí)行war包,感興趣的朋友一起看看吧2020-04-04SpringBoot使用JdbcTemplate操作數(shù)據(jù)庫
這篇文章主要介紹了SpringBoot使用JdbcTemplate操作數(shù)據(jù)庫,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07教你用Java Swing實(shí)現(xiàn)自助取款機(jī)系統(tǒng)
今天給大家?guī)淼氖顷P(guān)于JAVA的相關(guān)知識(shí),文章圍繞著如何用Java Swing實(shí)現(xiàn)自助取款機(jī)系統(tǒng)展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06