如何用Stream解决两层List属性求和问题
作者:CizelTian 时间:2022-07-31 20:32:35
用Stream解决两层List属性求和
假设一个人有很多个银行账户,每个银行账户中存有不同金额的存款,那么我们如何用Stream求一组人的所有存款呢?
首先,我们来建立一下所需的对象。
//账户对象
public class Account {
//账号
private String accountNumber;
//余额
private Integer balance;
public Account() {
}
public Account(String accountNumber, Integer balance) {
this.accountNumber = accountNumber;
this.balance = balance;
}
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public Integer getBalance() {
return balance;
}
public void setBalance(Integer balance) {
this.balance = balance;
}
}
//人对象
public class Person {
private String name;
private Integer age;
//账户列表
private List<Account> accounts;
public Person() {
}
public Person(String name, Integer age, List<Account> accounts) {
this.name = name;
this.age = age;
this.accounts = accounts;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public List<Account> getAccounts() {
return accounts;
}
public void setAccounts(List<Account> accounts) {
this.accounts = accounts;
}
}
建立用Stream流计算账户总余额的代码
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class Sum {
public static void main(String[] args) {
//生成包含四个账户的账户列表
List<Account> accounts1 = new ArrayList<>();
Account account1 = new Account("0001",100);
Account account2 = new Account("0001",200);
Account account3 = new Account("0001",300);
Account account4 = new Account("0001",400);
accounts1.add(account1);
accounts1.add(account2);
accounts1.add(account3);
accounts1.add(account4);
//生成一个名为“zs“的人对象
Person person1 = new Person("zs",20,accounts1);
//生成包含三个账户的账户列表
List<Account> accounts2 = new ArrayList<>();
Account account5 = new Account("0001",500);
Account account6 = new Account("0001",600);
Account account7 = new Account("0001",700);
accounts2.add(account5);
accounts2.add(account6);
accounts2.add(account7);
//生成一个”ls“的人对象
Person person2 = new Person("ls",30,accounts2);
//生成人列表
List<Person> persons = new ArrayList<>();
persons.add(person1);
persons.add(person2);
//计算总金额
Integer sum = persons.stream().map(Person::getAccounts).flatMap(Collection::stream).map(Account::getBalance).reduce(0,Integer::sum);
System.out.println(sum);
}
}
其中flatMap是把两个List<Account>合并为一个List,方便后续计算总额
stream计算一个List对象中某个字段总和
int total = list.stream().mapToInt(User::getAge).sum();
来源:https://blog.csdn.net/CizelTian/article/details/121774855
标签:Stream,List属性,求和
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
jdbc+jsp实现简单员工管理系统
2021-08-31 23:08:15
ClassLoader类加载源码解析
2023-11-25 18:17:09
![](https://img.aspxhome.com/file/2023/6/61826_0s.jpg)
Spring注解之@Lazy注解使用解析
2023-08-28 23:12:23
学生视角看Java 面向对象的继承本质
2022-04-23 21:51:10
![](https://img.aspxhome.com/file/2023/0/59740_0s.png)
Spring AOP原理及动态代理
2023-06-19 18:59:56
![](https://img.aspxhome.com/file/2023/3/61873_0s.png)
java 使用idea将工程打成jar并创建成exe文件类型执行的方法详解
2022-12-07 02:49:30
![](https://img.aspxhome.com/file/2023/2/60702_0s.png)
Zookeeper和Eureka哪个更好?
2023-11-10 02:57:35
C语言文件操作之fread函数详解
2023-07-06 18:24:15
![](https://img.aspxhome.com/file/2023/7/103077_0s.jpg)
简单探索 Java 中的惰性计算
2023-11-16 20:25:28
java异常处理详细介绍及实例
2023-11-28 05:07:02
![](https://img.aspxhome.com/file/2023/3/60193_0s.jpg)
spring mvc 组合mybatis框架实例详解
2023-11-28 04:56:04
![](https://img.aspxhome.com/file/2023/2/59752_0s.png)
java + dom4j.jar提取xml文档内容
2023-11-29 03:55:10
Gradle的安装和环境变量的配置详解
2023-07-11 04:56:37
![](https://img.aspxhome.com/file/2023/2/83552_0s.png)
使用@pathvariable与@requestparam碰到的一些问题及解决
2023-11-27 16:30:34
![](https://img.aspxhome.com/file/2023/9/60459_0s.png)
基于spring boot 的配置参考大全(推荐)
2021-11-28 23:32:41
SpringBoot异步调用方法并接收返回值
2023-08-16 22:33:49
![](https://img.aspxhome.com/file/2023/5/58055_0s.jpg)
java判断http地址是否连通(示例代码)
2023-08-05 03:24:05
Android控件之SlidingDrawer(滑动式抽屉)详解与实例分享
2023-07-27 09:26:18
![](https://img.aspxhome.com/file/2023/2/85982_0s.jpg)
Springboot Vue可配置调度任务实现示例详解
2023-11-09 03:33:19
Java数据结构之链表相关知识总结
2023-11-02 00:29:28
![](https://img.aspxhome.com/file/2023/4/58724_0s.png)