Java8新特性_Lambda表达式map遍历写法(个人补充)

该博客介绍了如何利用Java的Stream API对Employee对象进行复杂分组,首先根据员工的状态(Status)进行分组,然后在每个状态组内根据年龄范围(青年、中年、老年)进一步细分。通过示例展示了如何使用Lambda表达式遍历和打印分组后的结果,展示了Java8的高级特性在数据处理上的应用。
public class Employee implements Comparable<Employee>{

    private int id;
    private String name;
    private int age;
    private double salary;
    private Status status;

    public Employee() {
    }

    public Employee(String name) {
        this.name = name;
    }

    public Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Employee(int id, String name, int age, double salary) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.salary = salary;
    }



    public Employee(String name, int age, double salary, Status status) {
        this.name = name;
        this.age = age;
        this.salary = salary;
        this.status = status;
    }

    public Employee(String name, int age, double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public String show() {
        return "测试方法引用!";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + age;
        result = prime * result + id;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        long temp;
        temp = Double.doubleToLongBits(salary);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Employee other = (Employee) obj;
        if (age != other.age)
            return false;
        if (id != other.id)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (Double.doubleToLongBits(salary) != Double.doubleToLongBits(other.salary))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + ", status=" + status
                + "]";
    }

    @Override
    public int compareTo(Employee o) {
        /*if(String.valueOf(this.getAge()).equals(o.getAge())){
            return this.getName().compareTo(o.getName());
        }else{
            return String.valueOf(this.getAge()).compareTo(String.valueOf(o.getAge()));
        }*/

        if(this.getAge() > o.getAge()){
            return String.valueOf(this.getAge()).compareTo(String.valueOf(o.getAge()));
        }else if(this.getAge() < o.getAge()){
            return String.valueOf(this.getAge()).compareTo(String.valueOf(o.getAge()));
        }else{//如果年龄相等,比较姓名
            return this.getName().compareTo(o.getName());
        }
    }



    /*@Override
    public int compareTo(Object o) {
        if(o instanceof Goods) {
            Goods goods = (Goods)o;
            if(this.price > goods.price) {
                return 1;
            }else if(this.price < goods.price) {
                return -1;
            }else {
                return this.name.compareTo(goods.name);
            }
        }
        throw new RuntimeException("传入的数据类型不一致");
    }*/

    public enum Status {
        FREE, BUSY, VOCATION;
    }



}

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


public class Test {
    public static void main(String[] args) {

        List<Employee> employees = Arrays.asList(
                new Employee("刘德华",48,8000, Employee.Status.FREE),
                new Employee("张学友",28,8500, Employee.Status.BUSY),
                new Employee("黎明",51,6500, Employee.Status.VOCATION),
                new Employee("郭富城",51,5500, Employee.Status.FREE));

        Map<Employee.Status, Map<String, List<Employee>>> map = employees.stream()
                .collect(Collectors.groupingBy(Employee::getStatus,Collectors.groupingBy(e -> {
                    if(e.getAge() <= 35){
                        return "青年";
                    }else if(e.getAge() <= 50){
                        return "中年";
                    }else{
                        return "老年";
                    }
                })));

        //传统map遍历
        /*for (Map.Entry<Employee.Status, Map<String, List<Employee>>> statusMapEntry : map.entrySet()) {
            Employee.Status key = statusMapEntry.getKey();
            Map<String, List<Employee>> strMap = statusMapEntry.getValue();
            System.out.println(key+"---------"+strMap);
        }*/

        //----------------------------------Java8 Lambda表达式map遍历写法--------------------------------------------

        //map.forEach((key,value)-> System.out.println(key+"---"+value));

        /*VOCATION---{老年=[Employee [id=0, name=黎明, age=51, salary=6500.0, status=VOCATION]]}
        BUSY---{青年=[Employee [id=0, name=张学友, age=28, salary=8500.0, status=BUSY]]}
        FREE---{老年=[Employee [id=0, name=郭富城, age=51, salary=5500.0, status=FREE]], 中年=[Employee [id=0, name=刘德华, age=48, salary=8000.0, status=FREE]]}*/

        map.forEach((key,value) -> {
            Employee.Status status = key;
            value.forEach((employeekey,employeesvalue)->{
                String name = employeekey;
                List<Employee> employeeList = employeesvalue;
                System.out.println(status+"---"+name+"-------"+employeeList);
            });
        });
        /*VOCATION---老年-------[Employee [id=0, name=黎明, age=51, salary=6500.0, status=VOCATION]]
        BUSY---青年-------[Employee [id=0, name=张学友, age=28, salary=8500.0, status=BUSY]]
        FREE---老年-------[Employee [id=0, name=郭富城, age=51, salary=5500.0, status=FREE]]
        FREE---中年-------[Employee [id=0, name=刘德华, age=48, salary=8000.0, status=FREE]]*/


    }


}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZHOU_VIP

您的鼓励将是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值