Map的List集合排序
程序员文章站
2024-02-15 17:42:06
...
Map的List集合排序
package abcdefg;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Com {
/**
* @param args
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("ss_id", "1");
map1.put("distance", "0.7245");
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("ss_id", "2");
map2.put("distance", "4.3839");
Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("ss_id", "3");
map3.put("distance", "1.449");
list.add(map1);
list.add(map2);
list.add(map3);
// 排序前
for (Map<String, Object> map : list) {
System.out.println(map.get("distance"));
}
Collections.sort(list, new Comparator<Map<String, Object>>() {
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
String name1 = (String) o1.get("distance");
String name2 = (String) o2.get("distance");
return name1.compareTo(name2);
}
});
// 排序后
System.out.println("-------------------");
for (Map<String, Object> map : list) {
System.out.println(map.get("distance"));
}
}
}
上一篇: 1 Gitlab和Git安装
下一篇: java Map集合的排序