• Java8 Lambda 之 Collection Stream


    Lambda 之 Collection Stream

    Collection.stream()

    测试实体类

    class Demo {
        private Long id;
        private String name;
    
       	// Getters and Setters   
    }
    
    // 实例化并组成一个List
    List<Demo> demos = Lists.newArrayList(new Demo(1L, "SF"), new Demo(2L, "AXE"));
    
    

    1.map + collect用法

    场景1:获取List列表的所有id

    No Lambda

    public static List<Long> getIds(List<Demo> demos){
            List<Long> ids = Lists.newArrayList();
            for (Demo d : demos) {
                ids.add(d.getId());
            }
            return ids;
    }
    
    

    Lambda’s show

    List<Long> ids = demos.stream().map(Demo::getId).collect(toList());
    

    最后toList() 可以有更多的实现,比如

    Set<Long> ids = demos.stream().map(Demo::getId).collect(toSet());
    

    场景2:list变成map

    有时候我们把list变成map,将多次List的O(n)遍历变成Map的O(1)查询,拿空间换时间

    No lambda

    public static Map<Long, Demo> getIds(List<Demo> demos){
            Map<Long, Demo> map = Maps.newHashMap;
            for (Demo d : demos) {
                map.put(d.getId, d);
            }
            return map;
    }
    
    

    Lambda‘s show

     Map<Long, Demo> map = demos.stream().collect(toMap(Demo::getId, o -> o));
     
    

    2.filter

    场景1:从List中找到name="SF"的Demo实例

    No lambda

    public static Demo getSpecifyDemo(String name, List<Demo> demos){
    
        Demo target = null;
        for (Demo demo : demos) {
            if (name.equals(demo.getName())) {
                target = demo;
            }
        }
        return target;
    }
        
    

    Lambda's show--粗犷的老汉推车

    String targetName = "SF";
    Demo target = demos.stream().filter(d -> targetName.equals(d.getName())).findFirst().orElse(null);
    
    

    这种写法非常简单,但是调用链太长,一个lambda能够绕地球好几圈,最好写成以下格式,防止步子迈得太大。

    Lambda's show--优雅传教士

    Demo target = demos.stream()
                .filter(d -> targetName.equals.equals(d.getName()))
                .findFirst()
                .orElse(null);
    
    

    find()的结果是Optional,Optional号称NPE终结者,于是对于find()的结果你可以随意使用终结者携带的任何武器,例如orElse(),ifPresent(),isPresent()...每个用起来都是那种哒哒哒冒蓝火的,更多姿势详见Optional的裸体源码。

    3.match

    match()filter()的缩写版本,返回结果只有boolean类型,返回是否匹配。

    场景1:当前list中某个元素是否符合某个条件
    这个例子,给出另一个用法Demo::getId

    No lambda

    List<String> condition = new ArrayList<>();
    condition.add("SF");
    
    public static boolean isExist(List<String> condition, List<Demo> demos){
        boolean flag = false;
    
        for (Demo demo : demos) {
            if (condition.contains(demo.getName())) {
                flag = true;
                break;
            }
        }
    
        return flag;
    }
    
    

    Lambda's show 使用filter

    boolean flag = demos.stream()
                .map(Demo::getName)
                .filter(condition::contains)
                .findAny()
                .isPresent();
    
    

    Lambda's show 直接使用match

    boolean flag = demos.stream()
                .map(Demo::getName)
                .anyMatch(condition::contains);
    
  • 相关阅读:
    docker相关资源监控采集方法整理
    三节课MINI计划第四周
    三节课MINI计划第五周
    三节课MINI计划第四周
    三节课MINI计划第三周
    三节课MINI计划第二周
    Bilibili用户需求分析报告
    三节课MINI计划第一周
    《产品思维30讲 》学习笔记
    PHP-FPM未授权访问漏洞
  • 原文地址:https://www.cnblogs.com/AaronCui/p/10437866.html
Copyright © 2020-2023  润新知