一、使用排序算法对map进行排序
public class test {
public static void main(String[] args) {
Map<String, String> map = new TreeMap<String, String>(new Comparator<String>() {
public int compare(String obj1, String obj2) {
// 降序排序
return obj2.compareTo(obj1);
}
});
map.put("c", "ccccc");
map.put("a", "aaaaa");
map.put("b", "bbbbb");
map.put("d", "ddddd");
Set<String> keySet = map.keySet();
Iterator<String> iter = keySet.iterator();
while (iter.hasNext()) {
String key = iter.next();
System.out.println(key + ":" + map.get(key));
}
}
}
上面使用的是compare方法进行对map排序,可是我们还是没有到原有的排序算法对map进行排序。这个排序是非常简单也是经常用到的。
很多时候我们在进行的数据,从xml查出来的是一个list,因为大多数我们是利用 list来接受从数据库查出来的数据。如下面的一个案例:
//声明
Map<String,Integer> hashMap = new HashMap<String,Integer>();
//向Map中添加数据
//.....
//转换
ArrayList<Entry<String, Integer>> arrayList = new ArrayList<Entry<String, Integer>>(
hashMap.entrySet());
//排序
Collections.sort(arrayList, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> map1,
Map.Entry<String, Integer> map2) {
return (map2.getValue() - map1.getValue());
}
});
上面的案列使用了list来转换map的数据,然后集合类的Collections的排序方法,对map进行排序。居然使用的是集合类对list排序,那么我们就去查看Collections中sort的源码。
第一种:
public static <T extends Comparable<? super T>> void sort(List<T> list) {
Object[] a = list.toArray();
Arrays.sort(a);
ListIterator<T> i = list.listIterator();
for (int j=0; j<a.length; j++) {
i.next();
i.set((T)a[j]);
}
}
注意第一行代码,Object[] a = list.toArray();原来源码把list转化了object[],这句话就是我们想要的,因为我们以前学的冒泡排序都是以数组为原型的,就转化了我们熟悉的了,后面就简单了。
跟踪源码Arrays.sort:
public static void sort(long[] a) {
sort1(a, 0, a.length);
}
需要注意的是,sort1算法,根据不同的数组大小,采用了不同的排序方法。其实下面的可以替换我们所学的比如归并排序,自己写。
private static void sort1(int x[], int off, int len) {
// Insertion sort on smallest arrays
if (len < 7) {
for (int i=off; i<len+off; i++)
for (int j=i; j>off && x[j-1]>x[j]; j--)
swap(x, j, j-1);
return;
}
// Choose a partition element, v
int m = off + (len >> 1); // Small arrays, middle element
if (len > 7) {
int l = off;
int n = off + len - 1;
if (len > 40) { // Big arrays, pseudomedian of 9
int s = len/8;
l = med3(x, l, l+s, l+2*s);
m = med3(x, m-s, m, m+s);
n = med3(x, n-2*s, n-s, n);
}
m = med3(x, l, m, n); // Mid-size, med of 3
}
int v = x[m];
// Establish Invariant: v* (<v)* (>v)* v*
int a = off, b = a, c = off + len - 1, d = c;
while(true) {
while (b <= c && x[b] <= v) {
if (x[b] == v)
swap(x, a++, b);
b++;
}
while (c >= b && x[c] >= v) {
if (x[c] == v)
swap(x, c, d--);
c--;
}
if (b > c)
break;
swap(x, b++, c--);
}
// Swap partition elements back to middle
int s, n = off + len;
s = Math.min(a-off, b-a ); vecswap(x, off, b-s, s);
s = Math.min(d-c, n-d-1); vecswap(x, b, n-s, s);
// Recursively sort non-partition-elements
if ((s = b-a) > 1)
sort1(x, off, s);
if ((s = d-c) > 1)
sort1(x, n-s, s);
}
第二种,就是对第一种方法的重载。
public static <T> void sort(List<T> list, Comparator<? super T> c) {
Object[] a = list.toArray();
Arrays.sort(a, (Comparator)c);
ListIterator i = list.listIterator();
for (int j=0; j<a.length; j++) {
i.next();
i.set(a[j]);
}
}
分析和第一种一样。具体的方法和Arrays.sort调用那种排序的算法,可以自己去查看源码。
二、对Java对象进行排序。
后续进行补充。