用Stream解决两层List属性求和

  • Post author:
  • Post category:其他


假设一个人有很多个银行账户,每个银行账户中存有不同金额的存款,那么我们如何用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,方便后续计算总额



版权声明:本文为CizelTian原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。