我得到阵列和优先级队列的不同输出,请你告诉我它的原因
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
public class Employee {
private Long id;
private String name;
private int age;
private int salary;
public Employee(Long id , String name , int age , int salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int getSalary() {
return salary;
}
public static Comparator<Employee> salaryComparator = new Comparator<Employee>() {
public int compare(Employee e1 , Employee e2) {
return e1.getSalary() - e2.getSalary();
}
};
public static void main(String[] args) {
PriorityQueue<Employee> pe = new PriorityQueue<>(20, new Comparator<Employee>() {
public int compare(Employee e1 , Employee e2) {
return e1.getSalary() - e2.getSalary();
}
});
pe.add(new Employee(1L, "jack", 24, 9));
pe.add(new Employee(4L, "jill", 40, 12));
pe.add(new Employee(2L, "jill1", 30, 100));
pe.add(new Employee(3L, "jack2", 35, 11));
System.out.println("priority queue " + pe);
Employee[] empArr = new Employee[4];
empArr[0] = new Employee(1L, "jack", 24, 9);
empArr[1] = new Employee(4L, "jill", 40, 12);
empArr[2] = new Employee(2L, "jill1", 30, 100);
empArr[3] = new Employee(3L, "jack2", 35, 11);
Arrays.sort(empArr, Employee.salaryComparator);
System.out.println(" array " + Arrays.toString(empArr));
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + "]";
}
}
输出:
priority queue [Employee [id=1, name=jack, age=24, salary=9], Employee [id=3, name=jack2, age=35, salary=11], Employee [id=2, name=jill1, age=30, salary=100], Employee [id=4, name=jill, age=40, salary=12]]
array [Employee [id=1, name=jack, age=24, salary=9], Employee [id=3, name=jack2, age=35, salary=11], Employee [id=4, name=jill, age=40, salary=12], Employee [id=2, name=jill1, age=30, salary=100]]