hashMap转换为list并排序

Iterator it = Names.keySet().iterator();
List<KeyAndValue> tempList = new ArrayList<SystemDBHelper.KeyAndValue>();
while(it.hasNext()){
@SuppressWarnings("unchecked")
Map.Entry<String, Integer> key =(Entry<String, Integer>) it.next();
tempList.add(new KeyAndValue(key.getKey(), key.getValue()));
LogManager.e("key "+key.getKey() +" value "+key.getValue());
}

java.util.Collections.sort(tempList, new Comparator<KeyAndValue>() {
@Override
public int compare(KeyAndValue object1, KeyAndValue object2) {
// TODO Auto-generated method stub
if (object1.value > object2.value) {
return -1;
} else if (object1.value< object2.value) {
return 1;
}
return 0;
}
});

posted @ 2012-08-03 20:38  beiju  阅读(915)  评论(0编辑  收藏  举报