实际开发中会用到的常用lambda表达式

public class Test01 {
    public static void main(String[] args) {
        List<User> users = new Test01().init();
        //按照年龄排序
        users.sort(Comparator.comparing(User::getAge));
        users.forEach(System.out::println);
//        输出结果
//        User{id=3, name='xiaoxing', sex='male', age=21, height=174.0, weight=160.0, job='programmer', salary='8000'}
//        User{id=4, name='xiaohua', sex='female', age=21, height=160.0, weight=91.0, job='student', salary='0'}
//        User{id=2, name='xiaofang', sex='female', age=22, height=161.0, weight=90.0, job='teacher', salary='7000'}
//        User{id=1, name='xiaoming', sex='male', age=24, height=185.0, weight=130.0, job='actor', salary='20000'}
        //找出工作为程序员的用户
        System.out.println(users.stream().filter(e -> "programmer".equals(e.getJob())).findFirst());
//        输出结果
//        Optional[User{id=3, name='xiaoxing', sex='male', age=21, height=174.0, weight=160.0, job='programmer', salary='8000'}]
        //将list转换为map
        Map<Integer, User> userMap = users.stream().collect(Collectors.toMap(User::getId, Function.identity()));
        //遍历map
        userMap.forEach((id, user) -> {
            System.out.println("id" + id + ",user:" + user);
        });
//        输出结果
//        id1,user:User{id=1, name='xiaoming', sex='male', age=24, height=185.0, weight=130.0, job='actor', salary='20000'}
//        id2,user:User{id=2, name='xiaofang', sex='female', age=22, height=161.0, weight=90.0, job='teacher', salary='7000'}
//        id3,user:User{id=3, name='xiaoxing', sex='male', age=21, height=174.0, weight=160.0, job='programmer', salary='8000'}
//        id4,user:User{id=4, name='xiaohua', sex='female', age=21, height=160.0, weight=91.0, job='student', salary='0'}
        //按照部门进行分类
        Map<String, List<User>> departMap = users.stream().collect(Collectors.groupingBy(User::getDepartment));
        //遍历map
        departMap.forEach((id, user) -> {
            System.out.println("id" + id + ",user:" + user);
        });
        //输出结果
//        idstar,user:[User{id=2, name='xiaofang', sex='female', age=22, height=161.0, weight=90.0, job='teacher', salary='7000', department='star'}, User{id=1, name='xiaoming', sex='male', age=24, height=185.0, weight=130.0, job='actor', salary='20000', department='star'}]
//        idschool,user:[User{id=4, name='xiaohua', sex='female', age=21, height=160.0, weight=91.0, job='student', salary='0', department='school'}]
    }

    List<User> init() {
        List<User> res = new ArrayList<>();
        User u1 = new User(001, "xiaoming", "male", 24, 185.00, 130.00, "actor", "20000", "star");
        User u2 = new User(002, "xiaofang", "female", 22, 161.00, 90.00, "teacher", "7000", "star");
        User u3 = new User(003, "xiaoxing", "male", 21, 174.00, 160.00, "programmer", "8000", "computer");
        User u4 = new User(004, "xiaohua", "female", 21, 160.00, 91.00, "student", "0", "school");
        res.add(u1);
        res.add(u2);
        res.add(u3);
        res.add(u4);
        return res;
    }
}


class User {
    private Integer id;
    private String name;
    private String sex;
    private Integer age;
    private Double height;
    private Double weight;
    private String job;
    private String salary;
    private String department;

    public User(Integer id, String name, String sex, Integer age, Double height, Double weight, String job, String salary, String department) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.height = height;
        this.weight = weight;
        this.job = job;
        this.salary = salary;
        this.department = department;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", height=" + height +
                ", weight=" + weight +
                ", job='" + job + '\'' +
                ", salary='" + salary + '\'' +
                ", department='" + department + '\'' +
                '}';
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public User(Integer id, String name, String sex, Integer age, Double height, Double weight, String job, String salary) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.height = height;
        this.weight = weight;
        this.job = job;
        this.salary = salary;
    }

    public User() {

    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Double getHeight() {
        return height;
    }

    public void setHeight(Double height) {
        this.height = height;
    }

    public Double getWeight() {
        return weight;
    }

    public void setWeight(Double weight) {
        this.weight = weight;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public String getSalary() {
        return salary;
    }

    public void setSalary(String salary) {
        this.salary = salary;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值