首页 > 代码库 > Java8 新特性 Lambda学习

Java8 新特性 Lambda学习

import java.util.ArrayList;
import java.util.Collections;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.stream.Collectors;

public class TestLambda
{   
    public static void main(String[] args)
    {
        List<Employee> employees = new ArrayList<Employee>();
        employees.add(new Employee("张三", "男", 25));
        employees.add(new Employee("李四", "女", 24));
        employees.add(new Employee("王五", "女", 23));
        employees.add(new Employee("周六", "男", 22));
        employees.add(new Employee("孙七", "女", 21));
        employees.add(new Employee("刘八", "男", 20));
        // ---------------------------------------------- 输出employees
        System.out.println("##############输出employees##############");
        employees.forEach(e -> System.out.println(e));
        System.out.println("----------------------------------------------");
        employees.stream().forEach(e -> System.out.println(e));
        // ---------------------------------------------- 按年龄排序
        System.out.println("##############按年龄排序##############");
        Collections.sort(employees, (e1, e2) -> e1.getAge() - e2.getAge());
        employees.forEach(e -> System.out.println(e));
        System.out.println("----------------------------------------------");
        employees.stream().sorted((e1, e2) -> e1.getAge() - e2.getAge()).forEach(e -> System.out.println(e));
        // ---------------------------------------------- 打印年龄最大的女员工
        System.out.println("##############打印年龄最大的女员工##############");
        Employee maxAgeFemaleEmployee =
            employees.stream().filter(e -> "女".equals(e.getSex())).max((e1, e2) -> e1.getAge() - e2.getAge()).get();
        System.out.println(maxAgeFemaleEmployee);
        // ---------------------------------------------- 打印出年龄最大的2名男员工
        System.out.println("##############打印出年龄最大的2名男员工##############");
        employees.stream()
            .filter(e -> "男".equals(e.getSex()))
            .sorted((e1, e2) -> e2.getAge() - e1.getAge())
            .limit(2)
            .forEach(e -> System.out.println(e));
        // ---------------------------------------------- 打印出所有男员工的姓名,使用 ,分隔
        System.out.println("##############打印出所有男员工的姓名,使用 ,分隔##############");
        String maleEmployeesNames = employees.stream().map(e -> e.getName()).collect(Collectors.joining(","));
        System.out.println(maleEmployeesNames);       
        // ---------------------------------------------- 统计信息
        System.out.println("##############统计信息##############");
        IntSummaryStatistics stat = employees.stream().mapToInt(Employee::getAge).summaryStatistics();
        System.out.println("员工总数:" + stat.getCount());
        System.out.println("最高年龄:" + stat.getMax());
        System.out.println("最小年龄:" + stat.getMin());
        System.out.println("平均年龄:" + stat.getAverage());
    }
}

 

public class Employee
{
    private String name;
    
    private String sex;
    
    private int age;
    
    public Employee(String name, String sex, int age)
    {
        super();
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
    
    public String getName()
    {
        return name;
    }
    
    public String getSex()
    {
        return sex;
    }
    
    public int getAge()
    {
        return age;
    }
    
    @Override
    public String toString()
    {
        StringBuilder builder = new StringBuilder();
        builder.append("Employee {name=")
            .append(name)
            .append(", sex=")
            .append(sex)
            .append(", age=")
            .append(age)
            .append("}");
        return builder.toString();
    }
}

Java8 新特性 Lambda学习