我有一个代码,假定将交易对象的列表分为2类;
public class Transaction {
public String type;
public Integer amount;
}
以下功能通过检查条件将列表分为2类。流操作的输出映射为Map<Boolean, List<Transaction>>
,但我想使用String作为其键。所以我手动将它们转换。
public static Map<String, List<Transaction>> partitionTransactionArray(List<Transaction> t1) {
Map<Boolean, List<Transaction>> result = list.stream().collect(Collectors.groupingBy(e -> ((e.type.equals("BUY") || e.type.equals("SELL")) && e.amount < 1000)));
// I think this is not necessary
Map<String, List<Transaction>> result1 = new HashMap<>();
result1.put("APPROVED", result.get(true));
result1.put("PENDING", result.get(false));
return result1;
}
但是,我认为必须有一种聪明的方法,允许我在单个流操作中执行此操作。
有人可以帮忙吗?
编辑:
如果我想返回一个Map<String, List<Transactions>>
,而不是返回Map<String, List<Integer>>
,而列表仅包含交易金额的情况,该怎么办?
如何在单个流操作中做到这一点?
答案 0 :(得分:1)
替换
((e.type.equals("BUY") || e.type.equals("SELL")) && e.amount < 1000)
通过
((e.type.equals("BUY") || e.type.equals("SELL")) && e.amount < 1000) ? "APPROVED" : "PENDING"
您可能应该使用枚举代替魔术字符串常量。
答案 1 :(得分:0)
您可以将partitioningBy
用于groupingBy
的下游,而不是Collectors.mapping
,将Map<Boolean, List<Integer>> result = list.stream()
.collect(Collectors.partitioningBy(e ->
((e.type.equals("BUY") || e.type.equals("SELL")) && e.amount < 1000),
Collectors.mapping(Transaction::getAmount, Collectors.toList())));
用作:
echo Set oPlayer = CreateObject("WMPlayer.OCX")
echo While oPlayer.playState <> 1