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/

  • 相关阅读:
    What is a .Net Assembly?
    Reading Assembly attributes in VB.NET
    Debugging With Visual Studio 2005
    The Rules for GetHashCode
    The article discusses a couple of new features introduced for assemblies and versioning in Visual Studio 2005.
    Getting a copy of a DLL in the GAC
    Modeling SingleNeuron Dynamics and Computations: A Balance of Detail and Abstraction
    从数学到密码学(八)
    从数学到密码学(十)
    从数学到密码学(三)
  • 原文地址:https://www.cnblogs.com/parkdifferent/p/10676608.html
Copyright © 2011-2022 走看看