Collectionclass的sort()方法如何调用Comparable的compareTo()?
How does the sort() method of the Collection class call the Comparable's compareTo()?
假设我想对 Employee
个对象的列表进行排序:
Employee emp1 = new Employee("Abhijit", 10);
Employee emp2 = new Employee("Aniket", 5);
Employee emp3 = new Employee("Chirag", 15);
List<Employee> employees = new ArrayList<Employee>();
employees.add(emp1);
employees.add(emp2);
employees.add(emp3);
Collections.sort(employees);
System.out.println("sorted List is: "+employees);
而我的 Employee
class 实现了 Comparable
,因此它必须覆盖 compareTo()
方法。
而且我必须在 compareTo()
方法中编写我的排序逻辑。
class Employee implements Comparable<Employee>
{
String name;
int empId;
public Employee(String name, int empId)
{
this.name= name;
this.empId = empId;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getEmpId()
{
return empId;
}
public void setEmpId(int empId)
{
this.empId = empId;
}
@Override
public int compareTo(Employee e)
{
// TODO Auto-generated method stub
return this.empId > e.empId ? 1 : (this.empId < e.empId ? -1 : 0);
//return 0;
}
@Override
public String toString()
{
return String.valueOf(empId);
}
}
sort()
如何在内部调用compareTo()
方法?
请查看打开的 jdk 源代码。我想这有帮助。
132 public static <T extends Comparable<? super T>> void More ...sort(List<T> list) {
133 Object[] a = list.toArray();
134 Arrays.sort(a);
135 ListIterator<T> i = list.listIterator();
136 for (int j=0; j<a.length; j++) {
137 i.next();
138 i.set((T)a[j]);
139 }
140 }
集合排序调用Arrays.sort
1218 public static <T> void More ...sort(T[] a, Comparator<? super T> c) {
1219 T[] aux = (T[])a.clone();
1220 if (c==null)
1221 mergeSort(aux, a, 0, a.length, 0);
1222 else
1223 mergeSort(aux, a, 0, a.length, 0, c);
1224 }
Arrays.sort 呼叫 Arrays.mergeSort,您的答案在第 1157 行。
1145
1146 private static void More ...mergeSort(Object[] src,
1147 Object[] dest,
1148 int low,
1149 int high,
1150 int off) {
1151 int length = high - low;
1152
1153 // Insertion sort on smallest arrays
1154 if (length < INSERTIONSORT_THRESHOLD) {
1155 for (int i=low; i<high; i++)
1156 for (int j=i; j>low &&
1157 ((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
1158 swap(dest, j, j-1);
1159 return;
1160 }
1161
1162 // Recursively sort halves of dest into src
1163 int destLow = low;
1164 int destHigh = high;
1165 low += off;
1166 high += off;
1167 int mid = (low + high) >>> 1;
1168 mergeSort(dest, src, low, mid, -off);
1169 mergeSort(dest, src, mid, high, -off);
1170
1171 // If list is already sorted, just copy from src to dest. This is an
1172 // optimization that results in faster sorts for nearly ordered lists.
1173 if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) {
1174 System.arraycopy(src, low, dest, destLow, length);
1175 return;
1176 }
1177
1178 // Merge sorted halves (now in src) into dest
1179 for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
1180 if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0)
1181 dest[i] = src[p++];
1182 else
1183 dest[i] = src[q++];
1184 }
1185 }
假设我想对 Employee
个对象的列表进行排序:
Employee emp1 = new Employee("Abhijit", 10);
Employee emp2 = new Employee("Aniket", 5);
Employee emp3 = new Employee("Chirag", 15);
List<Employee> employees = new ArrayList<Employee>();
employees.add(emp1);
employees.add(emp2);
employees.add(emp3);
Collections.sort(employees);
System.out.println("sorted List is: "+employees);
而我的 Employee
class 实现了 Comparable
,因此它必须覆盖 compareTo()
方法。
而且我必须在 compareTo()
方法中编写我的排序逻辑。
class Employee implements Comparable<Employee>
{
String name;
int empId;
public Employee(String name, int empId)
{
this.name= name;
this.empId = empId;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getEmpId()
{
return empId;
}
public void setEmpId(int empId)
{
this.empId = empId;
}
@Override
public int compareTo(Employee e)
{
// TODO Auto-generated method stub
return this.empId > e.empId ? 1 : (this.empId < e.empId ? -1 : 0);
//return 0;
}
@Override
public String toString()
{
return String.valueOf(empId);
}
}
sort()
如何在内部调用compareTo()
方法?
请查看打开的 jdk 源代码。我想这有帮助。
132 public static <T extends Comparable<? super T>> void More ...sort(List<T> list) {
133 Object[] a = list.toArray();
134 Arrays.sort(a);
135 ListIterator<T> i = list.listIterator();
136 for (int j=0; j<a.length; j++) {
137 i.next();
138 i.set((T)a[j]);
139 }
140 }
集合排序调用Arrays.sort
1218 public static <T> void More ...sort(T[] a, Comparator<? super T> c) {
1219 T[] aux = (T[])a.clone();
1220 if (c==null)
1221 mergeSort(aux, a, 0, a.length, 0);
1222 else
1223 mergeSort(aux, a, 0, a.length, 0, c);
1224 }
Arrays.sort 呼叫 Arrays.mergeSort,您的答案在第 1157 行。
1145
1146 private static void More ...mergeSort(Object[] src,
1147 Object[] dest,
1148 int low,
1149 int high,
1150 int off) {
1151 int length = high - low;
1152
1153 // Insertion sort on smallest arrays
1154 if (length < INSERTIONSORT_THRESHOLD) {
1155 for (int i=low; i<high; i++)
1156 for (int j=i; j>low &&
1157 ((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
1158 swap(dest, j, j-1);
1159 return;
1160 }
1161
1162 // Recursively sort halves of dest into src
1163 int destLow = low;
1164 int destHigh = high;
1165 low += off;
1166 high += off;
1167 int mid = (low + high) >>> 1;
1168 mergeSort(dest, src, low, mid, -off);
1169 mergeSort(dest, src, mid, high, -off);
1170
1171 // If list is already sorted, just copy from src to dest. This is an
1172 // optimization that results in faster sorts for nearly ordered lists.
1173 if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) {
1174 System.arraycopy(src, low, dest, destLow, length);
1175 return;
1176 }
1177
1178 // Merge sorted halves (now in src) into dest
1179 for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
1180 if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0)
1181 dest[i] = src[p++];
1182 else
1183 dest[i] = src[q++];
1184 }
1185 }