java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Stream解决两层List属性求和

如何用Stream解决两层List属性求和问题

作者:CizelTian

这篇文章主要介绍了如何用Stream解决两层List属性求和问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

用Stream解决两层List属性求和

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

stream计算一个List对象中某个字段总和

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

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文