我有一个嵌套的地图列表,如下所示
地图示例列表:
[{owner=trader1, ord_id=[ord1, ord2], watchlist=trader1_WL}, {owner=trader2, ord_id=[ord3, ord4], watchlist=trader2_WL}]
我需要将其转换为ord_id:owner的映射
所需输出:
{ord1:trader1, ord2:trader1, ord3:trader2, ord4.trader2}
示例代码:[不起作用,因为 ord_id 有项目列表]
import java.util.*;
import java.util.stream.Collectors;
public class Main
{
public static void main (String[]args)
{
Map < String, Object > order1 = new HashMap <> ();
order1.put ("ord_id", Arrays.asList ("ord1", "ord2"));
order1.put ("owner", "trader1");
order1.put ("watchlist", "trader1_WL");
Map < String, Object > order2 = new HashMap <> ();
order2.put ("ord_id", Arrays.asList ("ord3", "ord4"));
order2.put ("owner", "trader2");
order2.put ("watchlist", "trader2_WL");
List < Map < String, Object >> open_order_list = new ArrayList <> ();
open_order_list.add (order1);
open_order_list.add (order2);
System.out.println (open_order_list);
Map < String, String > result =
open_order_list.stream ().collect (Collectors.
toMap (s->(String) s.get ("ord_id"),
s->(String) s.get ("owner")));
System.out.println (result);
}
}
这很难看,但你可以这样做:
Map<String, String> result = open_order_list.stream()
.flatMap(obj -> {
String owner = (String) obj.get("owner");
@SuppressWarnings("unchecked")
List<String> ord_id = (List<String>) obj.get("ord_id");
return ord_id.stream().map(id -> new String[] { owner, id });
})
.collect(Collectors.toMap(a -> a[1], a -> a[0]));
如果将 JSON 文本解析为 POJO,而不是通用集合对象,代码会好很多。
List<Order> open_order_list = Arrays.asList(
new Order("trader1", "trader1_WL", "ord1","ord2"),
new Order("trader2", "trader2_WL", "ord3","ord4") );
System.out.println(open_order_list);
Map<String, String> result = open_order_list.stream()
.flatMap(ord -> ord.getOrd_id().stream().map(id -> new String[] { ord.getOwner(), id }))
.collect(Collectors.toMap(a -> a[1], a -> a[0]));
System.out.println (result);
class Order {
private final String owner;
private final String watchlist;
private final List<String> ord_id;
public Order(String owner, String watchlist, String... ord_id) {
this.owner = owner;
this.watchlist = watchlist;
this.ord_id = Arrays.asList(ord_id);
}
public String getOwner() {
return this.owner;
}
public String getWatchlist() {
return this.watchlist;
}
public List<String> getOrd_id() {
return this.ord_id;
}
@Override
public String toString() {
return "Order[owner=" + this.owner + ", watchlist=" + this.watchlist + ", ord_id=" + this.ord_id + "]";
}
}