9. 几种Stream流优化实例

前几天入职新公司,这几天一直在看项目代码,发现有些地方写法略显古老,尝试看能不能用stream流优化一下代码,以下是3种优化实例(都是项目中的真实逻辑处理,以后遇到新的内容可能会补充)。方法一基本上都是原先的代码处理逻辑,方便理解。

实例1:优化List<Map<Object, Object>>写法 老List中每个值都经过不同方法处理封装进一个map,每个元素封装成的map再组成一个List集合

public class ListMapTest {
    public static void main(String[] args) {
        System.out.println(method1());
        System.out.println(method2());
        System.out.println(method3());
    }

    public static String method1() {
        List<Map<Object, Object>> arrays = new ArrayList<>();
        List<Integer> integers = new ArrayList() {{
            add(1);
            add(2);
            add(3);
        }};
        for (Integer i : integers) {
            Map<Object, Object> map = new HashMap<>();
            // 此处简化,实际分别对应3种不同的方法,都要用到列表中的变量进行处理
            map.put("firstNo", i + 2);
            map.put("secondNo", i - 2);
            map.put("thirdNo", i * 2);
            arrays.add(map);
        }
        return JSONArray.toJSONString(arrays);
    }

    public static String method2() {
        List<Integer> integers = new ArrayList() {{
            add(1);
            add(2);
            add(3);
        }};
        return JSONArray.toJSONString(integers.stream().map(x ->
                {
                    Map<Object, Object> map = new HashMap<>();
                    map.put("firstNo", x + 2);
                    map.put("secondNo", x - 2);
                    map.put("thirdNo", x * 2);
                    return map;
                }
        ).collect(Collectors.toList()));
    }

    public static String method3() {
        List<Integer> integers = new ArrayList() {{
            add(1);
            add(2);
            add(3);
        }};
        return JSONArray.toJSONString(integers.stream().map(x ->
                Arrays.stream(new Object[][]{
                        {"firstNo", x + 2},
                        {"secondNo", x - 2},
                        {"thirdNo", x * 2},
                }).collect(Collectors.toMap(kv -> kv[0], kv -> kv[1]))
        ).collect(Collectors.toList()));
    }
}

运行结果:
在这里插入图片描述

实例2:优化判断值是否在 逗号分割长字符串 中校验

public class SplitCommaTest {
    public static void main(String[] args) {
        String products = "1234,5678,9090";
        int prodCode1 = 1234;
        int prodCode2 = 12;

        System.out.println(check1(products, prodCode1) + "---" + check1(products, prodCode2));
        System.out.println(check2(products, prodCode1) + "---" + check2(products, prodCode2));
    }

    public static Boolean check1(String products, int prodCode) {
        String[] productsArray = products.split(",");
        List<String> prodCodeList = Arrays.asList(productsArray);
        return prodCodeList.contains(String.valueOf(prodCode));
    }

    public static Boolean check2(String products, int prodCode) {
        return Stream.of(products.split(",")).collect(toList()).stream().anyMatch(x -> String.valueOf(prodCode).equals(x));
    }
}

运行结果:
在这里插入图片描述

实例3:根据字符串找出List中某属性等于该字符串的对象

@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserEntity {

    private String userName;
    private Integer userAge;
}
public class FindListDataTest {
    public static void main(String[] args) throws Exception {
        List<UserEntity> userEntities = new ArrayList() {{
            add(new UserEntity("zhagnsan", 11));
            add(new UserEntity("lisi", 12));
            add(new UserEntity("wangwu", 13));
        }};
        System.out.println(find1("zhagnsan", userEntities));
//        System.out.println(find1("zhagnsan1", userEntities));

        System.out.println(find2("zhagnsan", userEntities));
        System.out.println(find2("zhagnsan1", userEntities));

    }

    public static UserEntity find1(String s, List<UserEntity> userEntities) throws Exception {
        for (UserEntity user : userEntities) {
            if (s.equals(user.getUserName())) {
                return user;
            }
        }
        throw new Exception("业务类型不匹配");
    }

    public static UserEntity find2(String s, List<UserEntity> userEntities) throws Exception {
        return userEntities.stream().filter(x -> s.equals(x.getUserName())).findFirst().orElseThrow(() -> new Exception("业务类型不匹配"));
    }
}

运行结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值