Json数组列表中的数据分组排序、组内排序
生活随笔
收集整理的這篇文章主要介紹了
Json数组列表中的数据分组排序、组内排序
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
- 問題描述
- 方式一:先全部排序,在分組排序
- 方式二:使用HashMap取出來分組再組內(nèi)排序
- 方式三:使用TreeMap取出來分組再組內(nèi)排序
- 測試代碼及耗時
問題描述
現(xiàn)在有一個用戶信息數(shù)組,用戶信息都在這個數(shù)組里,數(shù)組中有組號也有排序,現(xiàn)在需要將這些元素按照組號(groupNo)分組并排序,同時組內(nèi)元素也按照排序字段(sortNum)排序?
示例json數(shù)組,分組排序之前
分組排序之后:
[{"list":[{"sortNum":"2","userName":"neo","userId":"1","groupNo":"1"},{"sortNum":"3","userName":"zhangsan","userId":"3","groupNo":"1"},{"sortNum":"8","userName":"leo","userId":"2","groupNo":"1"}],"groupNo":"1"},{"list":[{"sortNum":"2","userName":"leo08","userId":"2","groupNo":"2"},{"sortNum":"6","userName":"neo1","userId":"4","groupNo":"2"},{"sortNum":"8","userName":"zhangsan1","userId":"3","groupNo":"2"}],"groupNo":"2"},{"list":[{"sortNum":"1","userName":"leo08","userId":"11","groupNo":"3"},{"sortNum":"2","userName":"neo1","userId":"7","groupNo":"3"},{"sortNum":"8","userName":"zhangsan","userId":"15","groupNo":"3"}],"groupNo":"3"} ]方式一:先全部排序,在分組排序
/*** 方式一:先全部排序,在分組排序** @return*/public static List<Map<String, Object>> sortByGroupNo1() {String jsonList = "[{\"userId\":\"7\",\"userName\":\"neo1\",\"groupNo\":\"3\",\"sortNum\":\"2\"},{\"userId\":\"11\",\"userName\":\"leo08\",\"groupNo\":\"3\",\"sortNum\":\"1\"},{\"userId\":\"15\",\"userName\":\"zhangsan\",\"groupNo\":\"3\",\"sortNum\":\"8\"},{\"userId\":\"1\",\"userName\":\"neo\",\"groupNo\":\"1\",\"sortNum\":\"2\"},{\"userId\":\"2\",\"userName\":\"leo\",\"groupNo\":\"1\",\"sortNum\":\"8\"},{\"userId\":\"3\",\"userName\":\"zhangsan\",\"groupNo\":\"1\",\"sortNum\":\"3\"},{\"userId\":\"4\",\"userName\":\"neo1\",\"groupNo\":\"2\",\"sortNum\":\"6\"},{\"userId\":\"2\",\"userName\":\"leo08\",\"groupNo\":\"2\",\"sortNum\":\"2\"},{\"userId\":\"3\",\"userName\":\"zhangsan1\",\"groupNo\":\"2\",\"sortNum\":\"8\"}]";List<Map<String, Object>> list = (List<Map<String, Object>>) JSON.parse(jsonList);List<String> groupNoList = new ArrayList<>();//使用比較器,先對分組進行排序list.sort(new Comparator<Map<String, Object>>() {@Overridepublic int compare(Map<String, Object> o1, Map<String, Object> o2) {if (!groupNoList.contains(o1.get("groupNo").toString())) {groupNoList.add(o1.get("groupNo").toString());}if (!groupNoList.contains(o2.get("groupNo").toString())) {groupNoList.add(o1.get("groupNo").toString());}if (Integer.valueOf(o1.get("groupNo").toString()) > Integer.valueOf(o2.get("groupNo").toString())) {return 1;} else if (Integer.valueOf(o1.get("groupNo").toString()) < Integer.valueOf(o2.get("groupNo").toString())) {return -1;} else {return 0;}}});//定義一個要返回的新的ListList<Map<String, Object>> returnList = new ArrayList<>();//先對分組列表排序Collections.sort(groupNoList);//對分組進行排序for (String groupNo : groupNoList) {Map<String, Object> newMap = new HashMap<>();newMap.put("groupNo", groupNo);newMap.put("list", new ArrayList<Map<String, Object>>());for (Map<String, Object> tmp : list) {if (groupNo.equals(tmp.get("groupNo").toString())) {List list1 = (List<Map<String, Object>>) newMap.get("list");list1.add(tmp);}}List<Map<String, Object>> list2 = (List<Map<String, Object>>) newMap.get("list");list2.sort(new Comparator<Map<String, Object>>() {@Overridepublic int compare(Map<String, Object> o1, Map<String, Object> o2) {if (Integer.valueOf(o1.get("sortNum").toString()) > Integer.valueOf(o2.get("sortNum").toString())) {return 1;} else if (Integer.valueOf(o1.get("sortNum").toString()) < Integer.valueOf(o2.get("sortNum").toString())) {return -1;} else {return 0;}}});returnList.add(newMap);}return returnList;}本來就寫了上面一個方法,但是作為程序員,需要盡可能的提高性能,我接下來又提供了兩種實現(xiàn)方式:
方式二:使用HashMap取出來分組再組內(nèi)排序
/*** 方式二:直接使用HashMap取出來所有分組,在進行分組排序,組內(nèi)排序*/public static List<Map<String, Object>> sortByGroupNo2() {String jsonList = "[{\"userId\":\"7\",\"userName\":\"neo1\",\"groupNo\":\"3\",\"sortNum\":\"2\"},{\"userId\":\"11\",\"userName\":\"leo08\",\"groupNo\":\"3\",\"sortNum\":\"1\"},{\"userId\":\"15\",\"userName\":\"zhangsan\",\"groupNo\":\"3\",\"sortNum\":\"8\"},{\"userId\":\"1\",\"userName\":\"neo\",\"groupNo\":\"1\",\"sortNum\":\"2\"},{\"userId\":\"2\",\"userName\":\"leo\",\"groupNo\":\"1\",\"sortNum\":\"8\"},{\"userId\":\"3\",\"userName\":\"zhangsan\",\"groupNo\":\"1\",\"sortNum\":\"3\"},{\"userId\":\"4\",\"userName\":\"neo1\",\"groupNo\":\"2\",\"sortNum\":\"6\"},{\"userId\":\"2\",\"userName\":\"leo08\",\"groupNo\":\"2\",\"sortNum\":\"2\"},{\"userId\":\"3\",\"userName\":\"zhangsan1\",\"groupNo\":\"2\",\"sortNum\":\"8\"}]";List<Map<String, Object>> list = (List<Map<String, Object>>) JSON.parse(jsonList);//定義一個要返回的新的ListList<Map<String, Object>> returnList = new ArrayList<>();Iterator iterator = list.iterator();Map<String, ArrayList<Map<String, Object>>> hashMap = new HashMap<>();while (iterator.hasNext()) {Map<String, Object> tmp = (Map<String, Object>) iterator.next();String groupNo = tmp.get("groupNo").toString();if (hashMap.containsKey(groupNo)) {ArrayList<Map<String, Object>> list2 = hashMap.get(groupNo);list2.add(tmp);} else {hashMap.put(groupNo, new ArrayList<>());ArrayList<Map<String, Object>> list1 = hashMap.get(groupNo);list1.add(tmp);}}//取出來key進行排序List<String> keySet = new ArrayList<>(hashMap.keySet());Collections.sort(keySet);Iterator iterator1 = keySet.iterator();while (iterator1.hasNext()) {String groupNo = (String) iterator1.next();Map<String, Object> newMap = new HashMap<>();List<Map<String, Object>> list2 = hashMap.get(groupNo);Collections.sort(list2, (o1, o2) -> {if (Integer.valueOf(o1.get("sortNum").toString()) > Integer.valueOf(o2.get("sortNum").toString())) {return 1;} else if (Integer.valueOf(o1.get("sortNum").toString()) < Integer.valueOf(o2.get("sortNum").toString())) {return -1;} else {return 0;}});newMap.put("groupNo", groupNo);newMap.put("list", list2);returnList.add(newMap);}return returnList;}方式三:使用TreeMap取出來分組再組內(nèi)排序
/*** 方式三:使用TreeMap先分組排序,在組內(nèi)排序*/public static List<Map<String, Object>> sortByGroupNo3() {String jsonList = "[{\"userId\":\"7\",\"userName\":\"neo1\",\"groupNo\":\"3\",\"sortNum\":\"2\"},{\"userId\":\"11\",\"userName\":\"leo08\",\"groupNo\":\"3\",\"sortNum\":\"1\"},{\"userId\":\"15\",\"userName\":\"zhangsan\",\"groupNo\":\"3\",\"sortNum\":\"8\"},{\"userId\":\"1\",\"userName\":\"neo\",\"groupNo\":\"1\",\"sortNum\":\"2\"},{\"userId\":\"2\",\"userName\":\"leo\",\"groupNo\":\"1\",\"sortNum\":\"8\"},{\"userId\":\"3\",\"userName\":\"zhangsan\",\"groupNo\":\"1\",\"sortNum\":\"3\"},{\"userId\":\"4\",\"userName\":\"neo1\",\"groupNo\":\"2\",\"sortNum\":\"6\"},{\"userId\":\"2\",\"userName\":\"leo08\",\"groupNo\":\"2\",\"sortNum\":\"2\"},{\"userId\":\"3\",\"userName\":\"zhangsan1\",\"groupNo\":\"2\",\"sortNum\":\"8\"}]";List<Map<String, Object>> list = (List<Map<String, Object>>) JSON.parse(jsonList);//這里使用treeMap的自動排序機制,對groupNo進行排序TreeMap<String, ArrayList<Map<String, Object>>> treeMap = new TreeMap<>();Iterator iterator = list.iterator();while (iterator.hasNext()) {Map<String, Object> tmp = (Map<String, Object>) iterator.next();String groupNo = tmp.get("groupNo").toString();if (treeMap.containsKey(groupNo)) {ArrayList<Map<String, Object>> list2 = treeMap.get(groupNo);list2.add(tmp);Collections.sort(list2, (o1, o2) -> {if (Integer.valueOf(o1.get("sortNum").toString()) > Integer.valueOf(o2.get("sortNum").toString())) {return 1;} else if (Integer.valueOf(o1.get("sortNum").toString()) < Integer.valueOf(o2.get("sortNum").toString())) {return -1;} else {return 0;}});} else {treeMap.put(groupNo, new ArrayList<Map<String, Object>>());ArrayList<Map<String, Object>> list1 = treeMap.get(groupNo);list1.add(tmp);}}//定義一個要返回的新的ListList<Map<String, Object>> returnList = new ArrayList<>();Iterator iter = treeMap.entrySet().iterator();while (iter.hasNext()) {Map<String, Object> newMap = new HashMap<>();Map.Entry<String, ArrayList<Map<String, Object>>> entry = (Map.Entry<String, ArrayList<Map<String, Object>>>) iter.next();String keyGroupNo = entry.getKey();newMap.put("groupNo", keyGroupNo);newMap.put("list", entry.getValue());returnList.add(newMap);}return returnList;}測試代碼及耗時
/*** 花費時間測試*/public static void costTime() {System.out.println("sortByGroupNo1排序結(jié)果:" + JSON.toJSONString(sortByGroupNo1()));System.out.println("sortByGroupNo2排序結(jié)果:" + JSON.toJSONString(sortByGroupNo2()));System.out.println("sortByGroupNo3排序結(jié)果:" + JSON.toJSONString(sortByGroupNo3()));int loopTimes = 100000;long startTime1 = System.currentTimeMillis();for (int i = 0; i < loopTimes; i++) {sortByGroupNo1();}System.out.println(loopTimes + "次循環(huán),sortByGroupNo1耗時:" + (System.currentTimeMillis() - startTime1) + "ms");long startTime2 = System.currentTimeMillis();for (int i = 0; i < loopTimes; i++) {sortByGroupNo2();}System.out.println(loopTimes + "次循環(huán),sortByGroupNo2耗時:" + (System.currentTimeMillis() - startTime2) + "ms");long startTime3 = System.currentTimeMillis();for (int i = 0; i < loopTimes; i++) {sortByGroupNo3();}System.out.println(loopTimes + "次循環(huán),sortByGroupNo3耗時:" + (System.currentTimeMillis() - startTime3) + "ms");}結(jié)果如下
sortByGroupNo1排序結(jié)果:[{"list":[{"sortNum":"2","userName":"neo","userId":"1","groupNo":"1"},{"sortNum":"3","userName":"zhangsan","userId":"3","groupNo":"1"},{"sortNum":"8","userName":"leo","userId":"2","groupNo":"1"}],"groupNo":"1"},{"list":[{"sortNum":"2","userName":"leo08","userId":"2","groupNo":"2"},{"sortNum":"6","userName":"neo1","userId":"4","groupNo":"2"},{"sortNum":"8","userName":"zhangsan1","userId":"3","groupNo":"2"}],"groupNo":"2"},{"list":[{"sortNum":"1","userName":"leo08","userId":"11","groupNo":"3"},{"sortNum":"2","userName":"neo1","userId":"7","groupNo":"3"},{"sortNum":"8","userName":"zhangsan","userId":"15","groupNo":"3"}],"groupNo":"3"}] sortByGroupNo2排序結(jié)果:[{"list":[{"sortNum":"2","userName":"neo","userId":"1","groupNo":"1"},{"sortNum":"3","userName":"zhangsan","userId":"3","groupNo":"1"},{"sortNum":"8","userName":"leo","userId":"2","groupNo":"1"}],"groupNo":"1"},{"list":[{"sortNum":"2","userName":"leo08","userId":"2","groupNo":"2"},{"sortNum":"6","userName":"neo1","userId":"4","groupNo":"2"},{"sortNum":"8","userName":"zhangsan1","userId":"3","groupNo":"2"}],"groupNo":"2"},{"list":[{"sortNum":"1","userName":"leo08","userId":"11","groupNo":"3"},{"sortNum":"2","userName":"neo1","userId":"7","groupNo":"3"},{"sortNum":"8","userName":"zhangsan","userId":"15","groupNo":"3"}],"groupNo":"3"}] sortByGroupNo3排序結(jié)果:[{"list":[{"sortNum":"2","userName":"neo","userId":"1","groupNo":"1"},{"sortNum":"3","userName":"zhangsan","userId":"3","groupNo":"1"},{"sortNum":"8","userName":"leo","userId":"2","groupNo":"1"}],"groupNo":"1"},{"list":[{"sortNum":"2","userName":"leo08","userId":"2","groupNo":"2"},{"sortNum":"6","userName":"neo1","userId":"4","groupNo":"2"},{"sortNum":"8","userName":"zhangsan1","userId":"3","groupNo":"2"}],"groupNo":"2"},{"list":[{"sortNum":"1","userName":"leo08","userId":"11","groupNo":"3"},{"sortNum":"2","userName":"neo1","userId":"7","groupNo":"3"},{"sortNum":"8","userName":"zhangsan","userId":"15","groupNo":"3"}],"groupNo":"3"}] 100000次循環(huán),sortByGroupNo1耗時:3433ms 100000次循環(huán),sortByGroupNo2耗時:1338ms 100000次循環(huán),sortByGroupNo3耗時:992ms總結(jié)
以上是生活随笔為你收集整理的Json数组列表中的数据分组排序、组内排序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java将数组元素倒置(反转)的方法?我
- 下一篇: java抽象类到底能不能够实例化?