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