日期:2014-05-20 浏览次数:20703 次
private static TreeMap<TreeElement, Integer> sortTreeMap (Map<TreeElement, Integer> tree) {
//TreeElement自行写个class即可, 只有一个Int,一个string
Comparator<TreeElement> co = new Comparator<TreeElement>() {
@Override
public int compare (TreeElement o1, TreeElement o2) {
if(o1.value >= o2.value) {
return -1;
}
else {
return 1;
}
}
};
TreeMap<TreeElement, Integer> ret = new TreeMap<TreeElement, Integer>(co);
for (Entry<TreeElement, Integer> ele : tree.entrySet()) {
ret.put(ele.getKey(), ele.getValue());
ret.remove(ele.getKey());
}
for (Entry<TreeElement, Integer> ele : ret.entrySet()) {
System.out.println(">>>>>" + ele.getKey().name + ";value="+ele.getValue());
}
return ret;
}