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/

posted @   parkdifferent  阅读(122)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示