建迅教育根据map集合的value进⾏排序
本项⽬⽤到排序
lebritee
1.map集合类型 Map
//⾃定义⽐较器
Comparator<Map.Entry<String, Double>> valueComparator = new Comparator<Map.Entry<String, Double>>() { @Override
public int compare(Entry<String, Double> o1, Entry<String, Double> o2) {
Value().Value());
}
};
// map转换成list进⾏排序
List<Map.Entry<String, Double>> list = new ArrayList<Map.Entry<String, Double>>(Set());
cet4准考证打印// 排序
Collections.sort(list, valueComparator);
⾃定义demo
@Test
public void demo2() {
Map<String, Double> map = new TreeMap<String, Double>();
map.put("acb1", 5.1);
map.put("bac1", 3.1);
map.put("bca1", 2.9);
map.put("cab1", 80.1);
map.put("cba1", 1.1);
愁苦map.put("abc1", 10.1);
blame是什么意思map.put("abc2", 12.1);
Comparator<Map.Entry<String, Double>> valueComparator = new Comparator<Map.Entry<String, Double>>() { @Override
public int compare(Entry<String, Double> o1, Entry<String, Double> o2) {
Value().Value());
}
};
// map转换成list进⾏排序
List<Map.Entry<String, Double>> list = new ArrayList<Map.Entry<String, Double>>(Set());
// 排序
Collections.sort(list, valueComparator);
}
输出结果:
[cba1=1.1, bca1=2.9, bac1=3.1, acb1=5.1, abc1=10.1, abc2=12.1, cab1=80.1]
获取对应的key, 即cba1,bca1 等等
<(i).getKey
receive的用法2.对list中的类的某个属性的值进⾏排序
需求:根据timeProportion的⼤⼩进⾏排序
public class InterestStudentLocation implements Comparable<InterestStudentLocation>, Serializable { private static final long rialVersionUID = -5411406470341636658L;
/**
英文简历字体
* 时间占⽐
*/cassadee pope
private Double timeProportion;
public Double getTimeProportion() {
return timeProportion;definition
}
public void tTimeProportion(Double timeProportion) {
this.timeProportion = timeProportion;
}
@Override
cwi
public int compareTo(InterestStudentLocation o) {
Double id1 = timeProportion;
Double id2 = o.getTimeProportion();
return id1 > id2 ? 1 : id1 == id2 ? 0 : -1;
}
}
//调⽤⽅法查询出list
List<InterestStudentLocation> list = ClassLocationAndTime(t);
//对list进⾏排序
Collections.sort(list);
list就根据timeProportion的⼤⼩进⾏排序了