// PriorityQueue默认大小 privatestaticfinalint DEFAULT_INITIAL_CAPACITY = 11; /** * Priority queue represented as a balanced binary heap: the two * children of queue[n] are queue[2*n+1] and queue[2*(n+1)]. The * priority queue is ordered by comparator, or by the elements' * natural ordering, if comparator is null: For each node n in the * heap and each descendant d of n, n <= d. The element with the * lowest value is in queue[0], assuming the queue is nonempty. */ // 数组来存放元素 privatetransient Object[] queue; // PriorityQueue中元素的个数 privateint size = 0; // 初始化PriorityQueue时传入比较器的接收者 privatefinal Comparator<? super E> comparator; // PriorityQueue被操作的次数 privatetransientint modCount = 0;
public E poll(){ if (size == 0) returnnull; // queue中元素个数减一,也就是最后一个元素的索引 int s = --size; // 操作次数递增 modCount++; // 得到堆顶元素 E result = (E) queue[0]; // 得到queue中的最后一个元素 E x = (E) queue[s]; queue[s] = null; // queue中的元素个数不为0,则调整堆 if (s != 0) siftDown(0, x); return result; }
privatevoidsiftDown(int k, E x){ if (comparator != null) siftDownUsingComparator(k, x); else siftDownComparable(k, x); }
@Override publicintcompareTo(Employee emp){ //let's sort the employee based on id in ascending order //returns a negative integer, zero, or a positive integer as this employee id //is less than, equal to, or greater than the specified object. return (this.id - emp.id); }
@Override //this is required to print the user friendly information about the Employee public String toString(){ return"[id=" + this.id + ", name=" + this.name + ", age=" + this.age + ", salary=" + this.salary + "]"; }
}
// main method //sorting object array Employee[] empArr = new Employee[4]; empArr[0] = new Employee(10, "Mikey", 25, 10000); empArr[1] = new Employee(20, "Arun", 29, 20000); empArr[2] = new Employee(5, "Lisa", 35, 5000); empArr[3] = new Employee(1, "Pankaj", 32, 50000);
//sorting employees array using Comparable interface implementation Arrays.sort(empArr); System.out.println("Default Sorting of Employees list:\n"+Arrays.toString(empArr));
@Override //this is required to print the user friendly information about the Employee public String toString(){ return"[id=" + this.id + ", name=" + this.name + ", age=" + this.age + ", salary=" + this.salary + "]"; } }
publicclassJavaObjectSorting{ // 在客户端修改 /** * Comparator to sort employees list or array in order of Salary */ // salary比较器 publicstatic Comparator<Employee> SalaryComparator = new Comparator<Employee>() {
//sorting custom object array Employee[] empArr = new Employee[4]; empArr[0] = new Employee(10, "Mikey", 25, 10000); empArr[1] = new Employee(20, "Arun", 29, 20000); empArr[2] = new Employee(5, "Lisa", 35, 5000); empArr[3] = new Employee(1, "Pankaj", 32, 50000); //sort employees array using Comparator by Salary // 调用sort方法时,传入比较器 Arrays.sort(empArr, JavaObjectSorting.SalaryComparator); System.out.println("Employees list sorted by Salary:\n"+Arrays.toString(empArr)); //sort employees array using Comparator by Age Arrays.sort(empArr, JavaObjectSorting.AgeComparator); System.out.println("Employees list sorted by Age:\n"+Arrays.toString(empArr)); //sort employees array using Comparator by Name Arrays.sort(empArr, JavaObjectSorting.NameComparator); System.out.println("Employees list sorted by Name:\n"+Arrays.toString(empArr)); } }