java 各种集合排序锦集
1.在JSONObject中保留LinkedHashMap(存储的是插入的顺序)排序?
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
Map<String, String> stateMap = new LinkedHashMap<String, String>(); JSONObject json = new JSONObject(); /* //这样存储时,没有LinkedHashMap的顺序 json.putAll(stateMap); */ //这样存储时,会有LinkedHashMap的顺序 与JSONObject不同,JSONArray是一个有序的值序列. 因此,如果要保留地图的顺序,可以使用两个键构造json对象: >第一个键可以称为数据,并将保存您的stateMap数据,如下所示: json.element('data', stateMap) >第二个键可以被称为键,它将是一个JSONArray对象,它将保存地图键的有序列表,如下所示: JSONArray array = new JSONArray(); array.addAll(stateMap.keySet()) json.put('keys', array)
2.LinkedHashMap排序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
LinkedHashMap<String, Float> map = new LinkedHashMap<>(); //先转成ArrayList集合 ArrayList<Entry<String, Float>> list = new ArrayList<Map.Entry<String, Float>>(map.entrySet()); //从小到大排序(从大到小将o1与o2交换即可) Collections.sort(list, new Comparator<Map.Entry<String, Float>>() { @Override public int compare(Entry<String, Float> o1, Entry<String, Float> o2) { return ((o1.getValue() - o2.getValue() == 0) ? 0: (o1.getValue() - o2.getValue() > 0) ? 1: -1); } }); //新建一个LinkedHashMap,把排序后的List放入 LinkedHashMap<String, Float> map2 = new LinkedHashMap<>(); for (Map.Entry<String, Float> entry : list) { map2.put(entry.getKey(), entry.getValue()); } //遍历输出 for (Map.Entry<String, Float> entry : map2.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); }
3.map排序,map转json防止顺序错乱
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public static void main(String[] args) { Map<String, String> mapsort = new TreeMap<String, String>( new Comparator<String>() { public int compare(String obj1, String obj2) { // 降序排序 return obj2.compareTo(obj1); } }); Map<String,String> map = new HashMap<String, String>(); map.put("2018-04", "123"); map.put("2018-01", "123"); map.put("2018-05", "123"); map.put("2018-02", "123"); map.put("2018-06", "123"); mapsort.putAll(map); for (String key : mapsort.keySet()) { System.out.println("Key = " + key); } } //map转json mapsort=JSON.parseObject(mapsort.toString(),new TypeReference<TreeMap<String, String>>(){} ,Feature.OrderedField);
4.JsonObject数据排序(顺序)问题
JsonObject内部是用Hashmap来存储的,所以输出是按key的排序来的,如果要让JsonObject按固定顺序(put的顺序)排列,可以修改JsonObject的定义HashMap改为LinkedHashMap。
public JSONObject() {
this.map = new LinkedHashMap(); //new HashMap();
}
即定义JsonObject可以这样:JSONObject jsonObj = new JSONObject(new LinkedHashMap());
5.map排序
5map排序(顺序、倒序)
package com.actionsoft.apps.ram.test; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; public class javaTest { public static void main(String[] args) { Map<String, Integer> map = new HashMap<String, Integer>(); map.put("d", 4); map.put("a", 1); map.put("c", 3); map.put("e", 5); map.put("b", 2); //排序前 System.out.println("before sort"); for (Map.Entry<String, Integer> entry : map.entrySet()) { System.out.println(entry.getKey() + "->" + entry.getValue()); } System.out.println(); //将map转成list List<Map.Entry<String, Integer>> infos = new ArrayList<Map.Entry<String, Integer>>(map.entrySet()); //对list排序,实现新的比较器 Collections.sort(infos, new Comparator<Map.Entry<String, Integer>>() { @Override public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { return o1.getValue() - o2.getValue(); } }); //申明新的有序 map,根据放入的数序排序 Map<String, Integer> lhm = new LinkedHashMap<String, Integer>(); //遍历比较过后的map,将结果放到LinkedHashMap for (Map.Entry<String, Integer> entry : infos) { lhm.put(entry.getKey(), entry.getValue()); } //遍历LinkedHashMap,打印值 System.out.println("after sort"); for (Map.Entry<String, Integer> entry : lhm.entrySet()) { System.out.println(entry.getKey() + "->" + entry.getValue()); } } }