zoukankan      html  css  js  c++  java
  • java8 stream 流式编程

    list转map

    @Data
    public class Person {
        private String id;
        private String name;
        private int age;
    
        public Person(String id, String name, int age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }
    }
    
    public class StreamTest {
    
        public static void main(String[] args) {
            List<Person> people = new ArrayList<>();
            Person person1 = new Person("1", "张三", 10);
            Person person2 = new Person("2", "李四", 20);
            Person person3 = new Person("3", "王五", 10);
            people.add(person1);
            people.add(person2);
            people.add(person3);
    
            // list 转 map
            Map<String, Person> collect = people.stream().collect(Collectors.toMap(Person::getId, k -> k));
            // System.out::println 可以看作 lambda表达式 e -> System.out.println(e) 的缩写形式。
            collect.values().forEach(System.out::println);
        }
    }
    


    过滤

    public class StreamTest {
    
        public static void main(String[] args) {
            List<Person> people = new ArrayList<>();
            Person person1 = new Person("1", "张三", 10);
            Person person2 = new Person("2", "李四", 20);
            Person person3 = new Person("3", "王五", 10);
            people.add(person1);
            people.add(person2);
            people.add(person3);
    
            // list 过滤年龄==10的 
            List<Person> collect = people.stream().filter(p -> p.getAge() == 10).collect(Collectors.toList());
            collect.forEach(System.out::println);
        }
    }
    

    分组操作

    Map<String, List<DoExtchCfg>> channelMap =  doExtchCfgs.stream().collect(Collectors.groupingBy(DoExtchCfg::getExtChannelCode));
    
  • 相关阅读:
    本地blast用法
    linux挂载移动硬盘
    酶设计软件rosetta安装
    redhat 6.7 安装nvidia显卡驱动时出现的问题
    分子模拟软件Schrodinger Suites 2015安装
    Python_二维数组
    Python_递归
    Python_装饰器
    Python_生成器generator
    Python_迭代器
  • 原文地址:https://www.cnblogs.com/stubborn-dude/p/15254323.html
Copyright © 2011-2022 走看看