简介:
Stream 是对集合(Collection)对象功能的增强,它专注于对集合对象进行各种非常便利、高效的聚合操作,或者大批量数据操作。通常我们需要多行代码才能完成的操作,借助于Stream流式处理可以很简单的实现。
本篇文章将分别使用传统的foreach 和 stream流 进行集合的求和汇总运算,废话不多说,直接上代码
1、创建测试的实体类
@Data
@ToString
public class Sys_testClass implements Serializable {
private String name;//名称
private double price;//价格
private int number;//数量
private double sum_price;//商品总价格
}
插个题外话,上面的 @Date和@ToString 注解代替了我们自动生成的getting,setting和toString方法,让类看上去更简洁明了,并且当你重新添加或删除字段的时候,不用去管getting,setting方法,大大节省了开发时间及效率,如果想用的小伙伴,直接导入 lombok依赖即可
2、创建 Main方法,并且往集合中添加测试数据
public static void main(String[] args) {
List<Sys_testClass> listTest = new ArrayList<>();
Sys_testClass test1 = new Sys_testClass();
test1.setName("商品1");
test1.setPrice(3200.0);
test1.setNumber(3);
test1.setSum_price(test1.getPrice()*test1.getNumber());
listTest.add(test1);
Sys_testClass test2 = new Sys_testClass();
test2.setName("商品2");
test2.setPrice(1599.0);
test2.setNumber(6);
test2.setSum_price(test2.getPrice()*test2.getNumber());
listTest.add(test2);
Sys_testClass test3 = new Sys_testClass();
test3.setName("商品3");
test3.setPrice(999.0);
test3.setNumber(2);
test3.setSum_price(test3.getPrice()*test3.getNumber());
listTest.add(test3);
}
3、利用传统foreach循环遍历,得到集合中所有价格的和,代码如下
double sum_price=0.00; //价格总和的初始值
for (Sys_testClass sys_testClass : listTest) {
sum_price+=sys_testClass.getSum_price();//循环相加 集合中的总价格
}
System.err.println(sum_price);// 打印输出值为: 21192.0
4、利用java8新特性(Stream流)遍及集合,只需要一行代码
double sum_price = listTest.stream().mapToDouble(Sys_testClass::getSum_price).sum();
System.err.println(sum_price);//打印输出值为: 21192.0
注:价格是double 类型,所以这里用了mapToDouble方法,如果是int类型求和 替换成 mapToInt方法即可
两者相比可以看出传统循环需要 4 行代码,而新特性只需要 1 行代码,速度上其实一样的,底层都是循环遍历得到结果,但是集合多了,开发时间就将会大大节省