栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA集合框架工具类自定义Collections集合方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA集合框架工具类自定义Collections集合方法

项目中有需要多次统计 某些集合中 的某个属性值,所以考虑封装一个方法,让其其定义实现计算方式。 话不多说,看代码:

1、封装的自定义集合工具类:CollectionsCustom

package com.test.util;
import java.util.Collection;
import org.apache.commons.collections.CollectionUtils;

public class CollectionsCustom { 
  
  public static  Output reduce(Collection original, Output initValue, ReduceFunction 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;
  }
  
  public interface ReduceFunction {
    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 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() {
  @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() {
  @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等,最好自己做下非空处理。相信高质量的封装代码能为你自己加分的!

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对考高分网的支持。如果你想了解更多相关内容请查看下面相关链接

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/139530.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号