【问题标题】:Java8: Streams filtering out items from list and get distinct entriesJava8:流从列表中过滤出项目并获得不同的条目
【发布时间】:2020-07-17 00:41:09
【问题描述】:

我现在使用 java8 steams 有一段时间了,我想将它用于以下场景。我有银行课。银行有两种类型的账户 - 支票和储蓄,并且有三种类型的活动:取款,存款和转账。

import java.util.Arrays;
import java.util.List;

public class Chasebank {


    public static void main(String[] args) {
        Customer john_checking_w = new Customer("1234", "John", "ChaseBanking",
                "checkingAccount", "Withdrawal", "500");

        Customer john_checking_d = new Customer("1234", "John", "ChaseBanking",
                "checkingAccount", "Deposit", "1000");

        Customer john_checking_t = new Customer("1234", "John", "ChaseBanking",
                "checkingAccount", "Transfer", "100");


        Customer john_saving_w = new Customer("1234", "John", "ChaseBanking",
                "savingAccount", "Withdrawal", "500");

        Customer john_saving_d = new Customer("1234", "John", "ChaseBanking",
                "savingAccount", "Deposit", "10000");


        Customer john_saving_t = new Customer("1234", "John", "ChaseBanking",
                "savingAccount", "Transfer", "200");



        Customer mary_saving_d = new Customer("2222", "Mary", "ChaseBanking",
                "savingAccount", "Deposit", "100");


        Customer mary_saving_t = new Customer("1234", "Mary", "ChaseBanking",
                "savingAccount", "Transfer", "50");


        Customer joseph_checking_w = new Customer("3333", "Joseph", "ChaseBanking",
                "checkingAccount", "Withdrawal", "760");




        List<Customer> customers = Arrays.asList(john_checking_w,
                john_checking_d, john_checking_t, john_saving_w, john_saving_d,
                john_saving_t, mary_saving_d, mary_saving_t, joseph_checking_w);

    }



    public static class Customer {
        final String Id;
        final String Name;
        final String pCode;
        final String accountType;
        final String activity;
        final String amount;

        public Customer(String id, String name, String pCode, String accountType, String activity, String amount) {
            Id = id;
            Name = name;
            this.pCode = pCode;
            this.accountType = accountType;
            this.activity = activity;
            this.amount = amount;
        }
    }
}

我在这里尝试做的是检查是否有 6 个不同的客户条目 1234 在列表中具有以下组合:

Id = 1234 accountType = savingAccount activity = Withdrawal value = 500
Id = 1234 accountType = savingAccount activity = Deposit value = 10000
Id = 1234 accountType = savingAccount activity = Transfer value = 200
Id = 1234 accountType = checkingAccount activity = Withdrawal value = 500
Id = 1234 accountType = checkingAccount activity = Deposit value = 1000
Id = 1234 accountType = checkingAccount activity = Transfer value = 100

值字段可以是任何东西。

到目前为止我尝试了什么:

Predicate<KonaFileLineItem> condition = k -> k.getId().equals("1234")
                && (k.getAccountType().equals("savingAccount")
                || k.getAccountType().equals("checkingAccount"))
                && (k.getActivity().equals("Withdrawal")
                || k.getActivity().equals("Deposit")
                || k.getActivity().equals("Transfer")

boolean result = customers.stream()
                .map(k -> k.getId().concat(k.getAccountType).concat(k.getActivity())
                .distinct().count() == 6; 

这在没有值字段的情况下按预期工作。我不确定如何验证值字段。需要帮助。

【问题讨论】:

  • 为客户提供 6 个不同的条目,您是指accountType * activity 的这六种可能组合吗?

标签: java list java-8 java-stream


【解决方案1】:

你必须通过谓词来过滤,我认为你错过了过滤器。请检查以下代码:

customers.stream().filter(t-&gt;t.getId().equals("1234")).map(k -&gt; k.getId().concat(k.getAccountType()).concat(k.getActivity())).distinct().count()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多