Stream流将List转Map

 使用Stream流将List转Map,并指定对象中数据作为Key

1. 根据id分组map 

        //获取订单list集合
        List PurchaseOrderList = b2bPurchaseOrderService.list();

        //根据id分组map
        Map collect =             
        PurchaseOrderList.stream().collect(Collectors.toMap(PurchaseOrder::getId, s -> s));
        String string1 = JSON.toJSONString(collect);
        System.out.println("string1: " + string1);

Stream流将List转Map_第1张图片

 2. 根据id分组map>

        //获取订单list集合
        List PurchaseOrderList = b2bPurchaseOrderService.list();


        //根据id分组map>
        Map> map = PurchaseOrderList.stream()
                .collect(Collectors.groupingBy(PurchaseOrder::getId));
        String string2 = JSON.toJSONString(map);
        System.out.println("string2: "+string2);

 Stream流将List转Map_第2张图片

你可能感兴趣的:(Java驿站,java)