如何用Stream解决两层List属性求和问题
更新时间:2023年05月29日 14:21:00 作者:CizelTian
这篇文章主要介绍了如何用Stream解决两层List属性求和问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
用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();
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
动态更改Spring定时任务Cron表达式的优雅方案实例详解
spring定时器非常强大,但是有时候我们需要在不需要重启应用就可以动态的改变Cron表达式的值,下面这篇文章主要给大家介绍了关于动态更改Spring定时任务Cron表达式的优雅方案,需要的朋友可以参考下2022-12-12
Spring Boot 2.x中Actuator的一些知识点
这篇文章主要给大家介绍了关于Spring Boot 2.x中Actuator的一些知识点,文中通过示例代码介绍的非常详细,对大家学习或者使用Spring Boot 2.x具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧2019-09-09
Java System.getProperty()-获取系统参数案例详解
这篇文章主要介绍了Java System.getProperty()-获取系统参数案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下2021-08-08
Java中实现Comparator接口和用法实例(简明易懂)
这篇文章主要介绍了Java中实现Comparator接口和用法实例(简明易懂),本文给出实现Comparator接口的实例和使用这个接口的代码实例,需要的朋友可以参考下2015-05-05
MyBatis验证多级缓存及 Cache Aside 模式的应用小结
本文介绍了MyBatis的多级缓存机制,包括本地缓存和全局缓存,并通过Spock测试框架验证了多级缓存的实现,本文结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧2024-12-12


最新评论