Java 如何将groupingBy分组到映射中并更改键类型

Java 如何将groupingBy分组到映射中并更改键类型,java,java-8,java-stream,partitioning,collectors,Java,Java 8,Java Stream,Partitioning,Collectors,我有一个代码,假设将事务对象列表分为两类 public class Transaction { public String type; public Integer amount; } 下面的函数通过检查条件将列表分为两类。流操作的输出映射是map,但我想使用字符串作为其键。所以我手动转换了它们 public static Map<String, List<Transaction>> partitionTransactionArray(List<T

我有一个代码,假设将事务对象列表分为两类

public class Transaction {
    public String type;
    public Integer amount;
}
下面的函数通过检查条件将列表分为两类。流操作的输出映射是
map
,但我想使用字符串作为其键。所以我手动转换了它们

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;
}
公共静态映射分区TransactionArray(列表t1){
Map result=list.stream().collect(Collectors.groupingBy(e->(e.type.equals(“BUY”)| | e.type.equals(“SELL”)&&e.amount<1000));
//我认为这是没有必要的
Map result1=新的HashMap();
结果1.put(“已批准”,result.get(true));
result1.put(“挂起”,result.get(false));
返回结果1;
}
然而,我认为一定有一种聪明的方法可以让我在单流操作中完成这个操作

有人能帮忙吗

编辑:

如果我不想返回
Map
,而是希望返回一个
Map
,其中列表只包含交易金额,该怎么办

如何在单流操作中执行此操作?

替换

((e.type.equals("BUY") || e.type.equals("SELL")) && e.amount < 1000)
((e.type.equals(“买入”)| | e.type.equals(“卖出”)和&e.amount<1000)

((e.type.equals(“买入”)| | e.type.equals(“卖出”)和&e.amount<1000)?“已批准”:“待定”

您可能应该使用枚举而不是神奇的字符串常量。

您可以使用下游到
分区方式,而不是使用
收集器的
分组方式。映射为:

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())));
Map result=list.stream()
.collect(收集器)分区方式(e->
((e.type.等于(“买入”)| e.type.等于(“卖出”)&&e.amount<1000),
Collectors.mapping(Transaction::getAmount,Collectors.toList());

谢谢你的回答。我编辑了这篇文章并添加了另一个问题。你能帮忙吗?
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())));