zoukankan      html  css  js  c++  java
  • Java 8 Streams

    public class Employee {
    
        private Long id;
    
        private String name;
    
        private Integer age;
    
        public Employee() {
        }
    
        public Employee(Long id, String name, Integer age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return new ToStringBuilder(this)
                    .append("id", id)
                    .append("name", name)
                    .append("age", age)
                    .toString();
        }
    }
    public class MyTest {
    
        public static void main(String[] args) {
    
            Employee employee1 = new Employee(11L, "hello1", 21);
            Employee employee2 = new Employee(12L, "hello2", 22);
            Employee employee3 = new Employee(13L, "hello3", 23);
            Employee employee4 = new Employee(14L, "hello4", 24);
            List<Employee> employees = Lists.newArrayList();
            employees.add(employee1);
            employees.add(employee2);
            employees.add(employee3);
            employees.add(employee4);
    
            List<Employee> ems = employees.stream().filter(e -> e.getId() >= 12).collect(toList());
            for (Employee e : ems) {
                System.out.println(e);
            }
    
            List<String> ems1 = employees.stream().filter(e -> e.getId() >= 12).map(Employee::getName).collect(toList());
            for (String s : ems1) {
                System.out.println(s);
            }
    
    
            /**
             * @apiNote
             * The following will accumulate strings into an ArrayList:
             * <pre>{@code
             *     List<String> asList = stringStream.collect(Collectors.toList());
             * }</pre>
             *
             * <p>The following will classify {@code Person} objects by city:
             * <pre>{@code
             *     Map<String, List<Person>> peopleByCity
             *         = personStream.collect(Collectors.groupingBy(Person::getCity));
             * }</pre>
             *
             * <p>The following will classify {@code Person} objects by state and city,
             * cascading two {@code Collector}s together:
             * <pre>{@code
             *     Map<String, Map<String, List<Person>>> peopleByStateAndCity
             *         = personStream.collect(Collectors.groupingBy(Person::getState,
             *                                                      Collectors.groupingBy(Person::getCity)));
             * }</pre>
             */
    
            Map<Long, List<Employee>> empById = ems.stream().collect(Collectors.groupingBy(Employee::getId));
            List<Employee> emps = empById.get(12L);
            for (Employee e : emps) {
                System.out.println(e);
            }
    
    
            List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
            String mergedString = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(", "));
            System.out.println("合并字符串: " + mergedString);
    
        }
    }

     参考文献

    Introduction to Java 8 Streams

    https://www.baeldung.com/java-8-streams-introduction

    A Guide to Streams in Java 8: In-Depth Tutorial with Examples

    https://stackify.com/streams-guide-java-8/

  • 相关阅读:
    poj3673
    poj3438
    poj3461
    poj3518
    poj3672
    变秃了,也变强了!爆肝吐血整理出的超硬核JVM笔记分享!
    左手字节,右手阿里,我是如何通阿里架构师的java面试文档,拿到多家大厂offer的
    Java异常处理与常用类
    copy_{to, from}_user()的思考
    vi文本编辑器常用指令功能
  • 原文地址:https://www.cnblogs.com/parkdifferent/p/10676608.html
Copyright © 2011-2022 走看看