@Test
public void testMap() {
// Comparator.reverseOrder()倒序排序
Map<BigDecimal, String> infoMap = new TreeMap<>(Comparator.reverseOrder());
infoMap.put(new BigDecimal(1), "Str1");
infoMap.put(new BigDecimal(3), "Str3");
infoMap.put(new BigDecimal(2), "Str2");
infoMap.put(new BigDecimal(4), "Str4");
for (Map.Entry<BigDecimal, String> entry : infoMap.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}
输出
4:Str4
3:Str3
2:Str2
1:Str1