如何用Stream解決兩層List屬性求和問題
用Stream解決兩層List屬性求和
假設(shè)一個(gè)人有很多個(gè)銀行賬戶,每個(gè)銀行賬戶中存有不同金額的存款,那么我們?nèi)绾斡肧tream求一組人的所有存款呢?
首先,我們來建立一下所需的對(duì)象。
//賬戶對(duì)象
public class Account {
//賬號(hào)
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;
}
}//人對(duì)象
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流計(jì)算賬戶總余額的代碼
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class Sum {
public static void main(String[] args) {
//生成包含四個(gè)賬戶的賬戶列表
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);
//生成一個(gè)名為“zs“的人對(duì)象
Person person1 = new Person("zs",20,accounts1);
//生成包含三個(gè)賬戶的賬戶列表
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);
//生成一個(gè)”ls“的人對(duì)象
Person person2 = new Person("ls",30,accounts2);
//生成人列表
List<Person> persons = new ArrayList<>();
persons.add(person1);
persons.add(person2);
//計(jì)算總金額
Integer sum = persons.stream().map(Person::getAccounts).flatMap(Collection::stream).map(Account::getBalance).reduce(0,Integer::sum);
System.out.println(sum);
}
}其中flatMap是把兩個(gè)List<Account>合并為一個(gè)List,方便后續(xù)計(jì)算總額
stream計(jì)算一個(gè)List對(duì)象中某個(gè)字段總和
int total = list.stream().mapToInt(User::getAge).sum();
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java將時(shí)間按月份分段的實(shí)現(xiàn)思路與方法
這篇文章主要給大家介紹了關(guān)于Java將時(shí)間按月份分段的實(shí)現(xiàn)思路與方法,通過文中介紹的方法可以將時(shí)間分成我們想要的時(shí)間段,文中給出了詳細(xì)的實(shí)例代碼,需要的朋友可以參考下2021-07-07
java 生成有序賬號(hào)的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猨ava 生成有序賬號(hào)的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10
關(guān)于spring?data?jpa?模糊查詢like的坑點(diǎn)
這篇文章主要介紹了關(guān)于spring?data?jpa?模糊查詢like的坑點(diǎn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Spring Boot 多環(huán)境配置Maven Profile vs 啟
本文介紹了在Java項(xiàng)目開發(fā)和部署中使用多環(huán)境配置的方法,比較了兩種主要方式:MavenProfile方式和啟動(dòng)參數(shù)方式,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2025-12-12

