工具类——自定义Collections集合方法
2017-05-18 15:22
351 查看
项目中有需要多次统计 某些集合中 的某个属性值,所以考虑封装一个方法,让其其定义实现计算方式。 话不多说,看代码:
import java.util.Collection;
import org.apache.commons.collections.CollectionUtils;
/**
* 自定义集合处理类
* @author : shijing
* 2017年5月18日下午2:00:10
*/
public class CollectionsCustom {
/**
* 将传入的collection内对象进行计算后得出结果
* @author : shijing
* 2017年5月18日下午3:09:53
* @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;
}
/**
* 自定义计算接口
* @author : shijing
* 2017年5月18日下午3:09:53
* @param <Input>
* @param <Result>
*/
public interface ReduceFunction<Input, Result> {
Result apply(Input input, Result lastResult);
}
}
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
}
}
}
总共年龄:73
这里如果传入的是封装类型Integer等,最好自己做下非空处理。相信高质量的封装代码能为你自己加分的!
1、封装的自定义集合工具类:CollectionsCustom
package com.test.util;import java.util.Collection;
import org.apache.commons.collections.CollectionUtils;
/**
* 自定义集合处理类
* @author : shijing
* 2017年5月18日下午2:00:10
*/
public class CollectionsCustom {
/**
* 将传入的collection内对象进行计算后得出结果
* @author : shijing
* 2017年5月18日下午3:09:53
* @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;
}
/**
* 自定义计算接口
* @author : shijing
* 2017年5月18日下午3:09:53
* @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等,最好自己做下非空处理。相信高质量的封装代码能为你自己加分的!
相关文章推荐
- 工具类——自定义Collections集合方法
- 工具类——自定义Collections集合方法
- 19-集合框架工具类-16-常用对象API(集合框架-工具类-Collections-其他方法&将非同步集合转成同步集合的方法)
- java中的集合工具类Collections中的常用方法
- 集合工具类Collections.sort()排序方法
- 黑马程序员---集合框架工具类Collections,数组工具类Arrays常见方法以及1.5版本新特性(高级for,可变参数和静态导入)
- 集合框架_Collections工具类的常见方法讲解
- (集合框架)collections工具类的常用方法
- 黑马程序员——JAVA基础——集合---概述、Collection中共性方法、List、Map、工具类Collections和Arrays
- Java集合工具类Collections常用方法
- Java基础之集合框架--Collections工具类之max()方法
- 16-常用对象API(集合框架-工具类-Collections-其他方法&将非同步集合转成同步集合的方法) 17-常用对象API(集合框架-工具类-Arrays-方法介绍)_20171202
- Java基础之集合框架--Collections工具类中的sort()方法
- 集合:Collections工具类方法
- 集合工具类Collections中一些方法简介
- 黑马程序员-博客 (6) 集合之Collection接口和Iterator方法、Collection接口和Collections工具类
- Collections工具类方法回顾以及自定义实现
- 对于集合中的自定义对象使用collections.sort 进行排序,需要实现compartor接口
- Collection接口和Collections集合工具类
- 黑马程序员_Java第17天知识总结_集合框架的工具类_Collections_Arrays_将数组变成list集合_集合变数组_高级for循环_可变参数