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/

  • 相关阅读:
    SpringBoot实现原理
    常见Http状态码大全
    forward(转发)和redirect(重定向)有什么区别
    1094. Car Pooling (M)
    0980. Unique Paths III (H)
    1291. Sequential Digits (M)
    0121. Best Time to Buy and Sell Stock (E)
    1041. Robot Bounded In Circle (M)
    0421. Maximum XOR of Two Numbers in an Array (M)
    0216. Combination Sum III (M)
  • 原文地址:https://www.cnblogs.com/parkdifferent/p/10676608.html
Copyright © 2011-2022 走看看