如何用Stream解決兩層List屬性求和問(wèn)題
用Stream解決兩層List屬性求和
假設(shè)一個(gè)人有很多個(gè)銀行賬戶,每個(gè)銀行賬戶中存有不同金額的存款,那么我們?nèi)绾斡肧tream求一組人的所有存款呢?
首先,我們來(lái)建立一下所需的對(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)文章
利用EasyPOI實(shí)現(xiàn)多sheet和列數(shù)的動(dòng)態(tài)生成
EasyPoi功能如同名字,主打的功能就是容易,讓一個(gè)沒(méi)見(jiàn)接觸過(guò)poi的人員就可以方便的寫出Excel導(dǎo)出,Excel導(dǎo)入等功能,本文主要來(lái)講講如何利用EasyPOI實(shí)現(xiàn)多sheet和列數(shù)的動(dòng)態(tài)生成,需要的可以了解下2025-03-03
java實(shí)現(xiàn)多人多牌數(shù)比較游戲
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)多人多牌數(shù)比較游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
動(dòng)態(tài)更改Spring定時(shí)任務(wù)Cron表達(dá)式的優(yōu)雅方案實(shí)例詳解
spring定時(shí)器非常強(qiáng)大,但是有時(shí)候我們需要在不需要重啟應(yīng)用就可以動(dòng)態(tài)的改變Cron表達(dá)式的值,下面這篇文章主要給大家介紹了關(guān)于動(dòng)態(tài)更改Spring定時(shí)任務(wù)Cron表達(dá)式的優(yōu)雅方案,需要的朋友可以參考下2022-12-12
Spring Boot 2.x中Actuator的一些知識(shí)點(diǎn)
這篇文章主要給大家介紹了關(guān)于Spring Boot 2.x中Actuator的一些知識(shí)點(diǎn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot 2.x具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
Java System.getProperty()-獲取系統(tǒng)參數(shù)案例詳解
這篇文章主要介紹了Java System.getProperty()-獲取系統(tǒng)參數(shù)案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Java8 將List轉(zhuǎn)換為用逗號(hào)隔開(kāi)的字符串的多種方法
這篇文章主要介紹了Java8 將List轉(zhuǎn)換為用逗號(hào)隔開(kāi)的字符串的幾種方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
Java中實(shí)現(xiàn)Comparator接口和用法實(shí)例(簡(jiǎn)明易懂)
這篇文章主要介紹了Java中實(shí)現(xiàn)Comparator接口和用法實(shí)例(簡(jiǎn)明易懂),本文給出實(shí)現(xiàn)Comparator接口的實(shí)例和使用這個(gè)接口的代碼實(shí)例,需要的朋友可以參考下2015-05-05
MyBatis驗(yàn)證多級(jí)緩存及 Cache Aside 模式的應(yīng)用小結(jié)
本文介紹了MyBatis的多級(jí)緩存機(jī)制,包括本地緩存和全局緩存,并通過(guò)Spock測(cè)試框架驗(yàn)證了多級(jí)緩存的實(shí)現(xiàn),本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-12-12

