欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何用Stream解決兩層List屬性求和問題

 更新時(shí)間:2023年05月29日 14:21:00   作者:CizelTian  
這篇文章主要介紹了如何用Stream解決兩層List屬性求和問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

用Stream解決兩層List屬性求和

假設(shè)一個(gè)人有很多個(gè)銀行賬戶,每個(gè)銀行賬戶中存有不同金額的存款,那么我們?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流計(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“的人對象
        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“的人對象
        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對象中某個(gè)字段總和

int total = list.stream().mapToInt(User::getAge).sum();

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論