您好,登錄后才能下訂單哦!
如何在JAVA中自定義Collections集合?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
1、封裝的自定義集合工具類:CollectionsCustom
package com.test.util; import java.util.Collection; import org.apache.commons.collections.CollectionUtils; /** * 自定義集合處理類 */ public class CollectionsCustom { /** * 將傳入的collection內(nèi)對象進行計算后得出結(jié)果 * @param original 計算前collection * @param reduceFunction 計算方式 * @param initValue 計算結(jié)果初始值 * @param <Input> collection對象類型 * @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; } /** * 自定義計算接口 * @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、測試輸出結(jié)果:
總共分數(shù):187.15
總共年齡:73
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。