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]]*/
}
}