Java8集合及Stream操作

集合、流Stream操作测试:


import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class CollectionOperation {

    //array convert stream
    @Test
    public void testStr2Stream() {
        //skuId1,num;skuId2,num
        String skuNum = "10001,1;10002,2";
        Stream<String> stream = Arrays.stream(skuNum.split(";"));
        stream.forEach(System.out::println);
    }

    //stream flatMap 
    @Test
    public void testStreamFlatMap() {
        //skuId1,num;skuId2,num
        String skuNum = "10001,1;10002,2";
        Arrays.stream(skuNum.split(";"))
                .flatMap(str -> Arrays.stream(str.split(",")))
                .forEach(System.out::println);
    }

    // stream map
    @Test
    public void testStreamMap() {
        //skuId1,num;skuId2,num
        String skuNum = "10001,1;10002,2";
        Arrays.stream(skuNum.split(";"))
                .map(str -> str.split(","))
                .forEach(arr -> System.out.println(Arrays.toString(arr)));
    }

    // string stream map Object
    @Test
    public void testStr2OrderSkuInfo() {
        //skuId1,num;skuId2,num
        String skuNum = "10001,1;10002,2";

        List<OrderSkuInfo> skuInfoStream = Arrays.stream(skuNum.split(";"))
                .map(str -> str.split(","))
                .map(arr -> new OrderSkuInfo(Long.valueOf(arr[0]), Integer.valueOf(arr[1])))
                .collect(Collectors.toList());

        System.out.println(skuInfoStream);
    }

    // string stream convert Map Collection
    @Test
    public void testStrConvertMap() {
        //skuId1,num;skuId2,num
        List<String> bookOrderList = Arrays.asList("10001,1;10002,2");

        Map<Long, OrderSkuInfo> skuInfoMap = extractOrderSkuInfo2(bookOrderList);

        System.out.println(skuInfoMap);
    }

    // 使用java8 api 将固定格式字符串转为Map结构
    private Map<Long, OrderSkuInfo> extractOrderSkuInfo2(List<String> bookOrderList) {
        //k->v: 实物子单skuId -> OrderSkuInfo
        Map<Long, OrderSkuInfo> map = bookOrderList.stream()
                .flatMap(skuInfo -> Arrays.stream(skuInfo.split(";")))
                .map(skuInfo -> skuInfo.split(","))
                .collect(Collectors.toMap(
                        arr -> Long.valueOf(arr[0]),
                        arr -> new OrderSkuInfo(Long.valueOf(arr[0]), Integer.valueOf(arr[1])),
                        (oldValue, newValue) -> new OrderSkuInfo(oldValue.getSkuId(), oldValue.getQuantity() + newValue.getQuantity()))
                );
        return map;
    }

    static class OrderSkuInfo {
        private long skuId;
        private int quantity;

        public OrderSkuInfo(long skuId, int quantity) {
            this.skuId = skuId;
            this.quantity = quantity;
        }

        public long getSkuId() {
            return skuId;
        }

        public void setSkuId(long skuId) {
            this.skuId = skuId;
        }

        public int getQuantity() {
            return quantity;
        }

        public void setQuantity(int quantity) {
            this.quantity = quantity;
        }

        @Override
        public String toString() {
            return "OrderSkuInfo{" +
                    "skuId=" + skuId +
                    ", quantity=" + quantity +
                    '}';
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值