[英]Java8: Streams filtering out items from list and get distinct entries
我現在使用 java8 steams 有一段時間了,我想將它用於以下場景。 我有一家銀行 class。 銀行有兩種類型的賬戶 - 支票和儲蓄,並且有三種類型的活動:取款,存款和轉賬。
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;
這在沒有值字段的情況下按預期工作。 我不確定如何驗證值字段。 需要幫助。
您必須通過謂詞進行過濾,我認為您錯過了過濾器。 請檢查以下代碼:
customers.stream().filter(t->t.getId().equals("1234")).map(k -> k.getId().concat(k.getAccountType()).concat(k.getActivity())).distinct().count()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.