JAVA集合框架工具類自定義Collections集合方法

今天小編就爲大家分享一篇關於JAVA集合框架工具類自定義Collections集合方法,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

項目中有需要多次統計 某些集合中 的某個屬性值,所以考慮封裝一個方法,讓其其定義實現計算方式。 話不多說,看代碼:

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) {
 //統計集合內分數之和
 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等,最好自己做下非空處理。相信高質量的封裝代碼能爲你自己加分的!

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對神馬文庫的支持。如果你想了解更多相關內容請查看下面相關鏈接

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章