使用Java8转换为map(SplitSpring和make键,对象的其他值作为值)

使用Java8转换为map(SplitSpring和make键,对象的其他值作为值),java,Java,我的订单类别如下: 类顺序{ 字符串orderId; 字符串executionId; 订单(字符串orderId、字符串executionId){ this.orderId=orderId; this.executionId=executionId; } 公共字符串getOrderId(){ 返回订单ID; } 公共无效setOrderId(字符串orderId){ this.orderId=orderId; } 公共字符串getExecutionId(){ 返回executionId; } p

我的
订单
类别如下:

类顺序{
字符串orderId;
字符串executionId;
订单(字符串orderId、字符串executionId){
this.orderId=orderId;
this.executionId=executionId;
}
公共字符串getOrderId(){
返回订单ID;
}
公共无效setOrderId(字符串orderId){
this.orderId=orderId;
}
公共字符串getExecutionId(){
返回executionId;
}
public void setExecutionId(字符串executionId){
this.executionId=executionId;
}
}
列出订单=
数组.asList(新命令(“2”,“23,21,25”),
新命令(“4”、“22、24”),
新命令(“6”、“27”),
新订单(“2”、“28,30”),
新命令(“4”、“29”),
新命令(“5”、“26”);
我需要一个映射对象输出,根据下面的结构来获得与执行相关的命令

Map<String, String> = {"23","2"},{"21","2"},{"25","2"},{"22","4"},{"24","4"},{"27","6"},{"28","2"},{"30","2"},{"29","4"},{"26","5"};
Map={“23”,“2”},{“21”,“2”},{“25”,“2”},{“22”,“4”},{“24”,“4”},{“27”,“6”},{“28”,“2”},{“30”,“2”},{“29”,“4”},{“26”,“5”};

逗号分隔的值应视为映射键,对象的其他值应为值

我喜欢下面这样的东西-

    Map<String, String> flatMap = new HashMap<>();

    orders.forEach(order -> {
        String k = order.orderId;
        String values = order.executionId;

        for (String v : values.split(",")) {
            flatMap.put(v, k);
        }

    });
Map flatMap=newhashmap();
订单。forEach(订单->{
字符串k=order.orderId;
字符串值=order.executionId;
对于(字符串v:values.split(“,”)){
平面图放置(v,k);
}
});
不是一种简单的方法,但可读性方面我更喜欢

以下是不太受欢迎的方法-

Map<String, String> flatMap = orders.stream()
        .map(order -> Arrays.stream(order.executionId.split(","))
                .collect(Collectors.toMap(Function.identity(), x -> order.orderId)))
        .flatMap(map -> map.entrySet().stream())
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
Map flatMap=orders.stream()
.map(order->Arrays.stream(order.executionId.split(“,”))
.collect(Collectors.toMap(Function.identity(),x->order.orderId)))
.flatMap(map->map.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue));

我想这就是你想要的

        List<Order> orders = Arrays.asList(
                new Order("2", "23,21,25"),
                new Order("4", "22,24"), new Order("6", "27"),
                new Order("2", "28,30"), new Order("4", "29"),
                new Order("8", "26"), //<-- added this for demo
                new Order("5", "26"));

        Map<String, String> map = orders.stream()
                .flatMap(order -> Arrays
                        .stream(order.getExecutionId()
                                .split(","))
                        .map(a -> new String[] { a,
                                order.getOrderId() }))
                .collect(Collectors.toMap(info -> info[0], info -> info[1],
                        (value, oId) -> value + "," + oId,
                        LinkedHashMap::new));

        System.out.println(map);

您可以分享
订单
课程吗?
        Map<String,String> map2 = new LinkedHashMap<>();
        for (Order order : orders) {
            for (String eId : order.getExecutionId().split(",")) {
                map2.merge(eId, order.getOrderId(), (value,oId)->  value + "," + oId);
            }
        }
        System.out.println(map2);