map数据按照list排序
-
简介 (Introduction):
-
背景
需要对字段进行排序,但是,一个字段的结果存在一个map中,一个存在list中,需要让map的数组,按照list的顺序进行排序 -
结构图
list顺序1 [ 2 { 3 "fieldKey":"关键字", 4 "alias":"ID", 5 "owner":"mysql", 6 "field":"ID", 7 "fieldNumber":1 8 }, 9 { 10 "fieldKey":"名称", 11 "alias":"CHNAME", 12 "owner":"mysql", 13 "field":"NAME", 14 "fieldNumber":2 15 }, 16 { 17 "fieldKey":"是否删除", 18 "alias":"DELETED", 19 "owner":"mysql", 20 "field":"ISDELETED", 21 "fieldNumber":3 22 }, 23 { 24 "fieldKey":"序号", 25 "alias":"ORDERNO", 26 "owner":"mysql", 27 "field":"ORDERNO", 28 "fieldNumber":4 29 } 30 ]
map顺序
1 { 2 "ORDERNO":{ 3 "fieldKey":"序号", 4 "alias":"ORDERNO", 5 "owner":"mysql", 6 "field":"ORDERNO", 7 "fieldNumber":4 8 }, 9 "NAME":{ 10 "fieldKey":"名称", 11 "alias":"CHNAME", 12 "owner":"mysql", 13 "field":"NAME", 14 "fieldNumber":2 15 }, 16 "ISDELETED":{ 17 "fieldKey":"是否删除", 18 "alias":"DELETED", 19 "owner":"mysql", 20 "field":"ISDELETED", 21 "fieldNumber":3 22 }, 23 "ID":{ 24 "fieldKey":"关键字", 25 "alias":"ID", 26 "owner":"mysql", 27 "field":"ID", 28 "fieldNumber":1 29 } 30 }
-
-
核心
-
快速上手(Getting Started)
模型
list模型:
1 /** 2 * 生成资源数组 3 * 4 * @return 结果集 5 */ 6 private static List<FieldDemo> getListSource() { 7 List<FieldDemo> fieldDemos = new ArrayList<>(); 8 FieldDemo fieldDemo = new FieldDemo(); 9 fieldDemo.setField("ID"); 10 fieldDemo.setAlias("ID"); 11 fieldDemo.setFieldKey("关键字"); 12 fieldDemo.setOwner("mysql"); 13 fieldDemo.setFieldNumber(1); 14 15 FieldDemo fieldDemo2 = new FieldDemo(); 16 fieldDemo2.setField("NAME"); 17 fieldDemo2.setAlias("CHNAME"); 18 fieldDemo2.setFieldKey("名称"); 19 fieldDemo2.setOwner("mysql"); 20 fieldDemo2.setFieldNumber(2); 21 22 FieldDemo fieldDemo3 = new FieldDemo(); 23 fieldDemo3.setField("ISDELETED"); 24 fieldDemo3.setAlias("DELETED"); 25 fieldDemo3.setFieldKey("是否删除"); 26 fieldDemo3.setOwner("mysql"); 27 fieldDemo3.setFieldNumber(3); 28 29 FieldDemo fieldDemo4 = new FieldDemo(); 30 fieldDemo4.setField("ORDERNO"); 31 fieldDemo4.setAlias("ORDERNO"); 32 fieldDemo4.setFieldKey("序号"); 33 fieldDemo4.setOwner("mysql"); 34 fieldDemo4.setFieldNumber(4); 35 36 fieldDemos.add(fieldDemo); 37 fieldDemos.add(fieldDemo2); 38 fieldDemos.add(fieldDemo3); 39 fieldDemos.add(fieldDemo4); 40 41 return fieldDemos; 42 }
map模型:
1 /** 2 * 生成map 3 * 4 * @return map结果集 5 */ 6 private static Map<FieldDemo, FieldDemo> getMapSource() { 7 Map<FieldDemo, FieldDemo> map = new HashMap<>(); 8 List<FieldDemo> listSource = getListSource(); 9 List<FieldDemo> listSource2 = getListSource2(); 10 for (FieldDemo fieldDemo : listSource) { 11 for (FieldDemo demo : listSource2) { 12 if (fieldDemo.getField().equalsIgnoreCase(demo.getField())) { 13 map.put(fieldDemo, demo); 14 } 15 } 16 } 17 return map; 18 } 19 20 21 private static List<FieldDemo> getListSource2() { 22 List<FieldDemo> fieldDemos = new ArrayList<>(); 23 FieldDemo fieldDemo = new FieldDemo(); 24 fieldDemo.setField("ID"); 25 fieldDemo.setAlias("ID"); 26 fieldDemo.setFieldKey("关键字"); 27 fieldDemo.setOwner("mysql"); 28 fieldDemo.setFieldNumber(1); 29 30 31 FieldDemo fieldDemo2 = new FieldDemo(); 32 fieldDemo2.setField("NAME"); 33 fieldDemo2.setAlias("CHNAME"); 34 fieldDemo2.setFieldKey("名称"); 35 fieldDemo2.setOwner("mysql"); 36 fieldDemo2.setFieldNumber(2); 37 38 39 FieldDemo fieldDemo3 = new FieldDemo(); 40 fieldDemo3.setField("ISDELETED"); 41 fieldDemo3.setAlias("DELETED"); 42 fieldDemo3.setFieldKey("是否删除"); 43 fieldDemo3.setOwner("mysql"); 44 fieldDemo3.setFieldNumber(3); 45 46 47 FieldDemo fieldDemo4 = new FieldDemo(); 48 fieldDemo4.setField("ORDERNO"); 49 fieldDemo4.setAlias("ORDERNO"); 50 fieldDemo4.setFieldKey("序号"); 51 fieldDemo4.setOwner("mysql"); 52 fieldDemo4.setFieldNumber(4); 53 54 55 fieldDemos.add(fieldDemo4); 56 fieldDemos.add(fieldDemo3); 57 fieldDemos.add(fieldDemo); 58 fieldDemos.add(fieldDemo2); 59 return fieldDemos; 60 }
业务
核心处理
1 /** 2 * 让复杂map按照list的顺序排序 3 * 4 * @param list 5 * @param map 6 * @param newMap 7 */ 8 public static void sortMapFromList(List<FieldDemo> list, Map<FieldDemo, FieldDemo> map, Map<FieldDemo, FieldDemo> newMap) { 9 10 Map<FieldDemo, Integer> middleMap = new HashMap<>(16); 11 12 for (FieldDemo fieldDemo : list) { 13 for (Map.Entry<FieldDemo, FieldDemo> fieldDemoFieldDemoEntry : map.entrySet()) { 14 String field = fieldDemoFieldDemoEntry.getKey().getField(); 15 16 if (field.equalsIgnoreCase(fieldDemo.getField())) { 17 // 生成中间存储map,存入数据和对应的序号 18 middleMap.put(fieldDemoFieldDemoEntry.getKey(), fieldDemo.getFieldNumber()); 19 } 20 } 21 } 22 23 // 生成的结果集加入到list中 24 List<Map.Entry<FieldDemo, Integer>> entryList = new ArrayList(middleMap.entrySet()); 25 26 // 根据value 进行排序 27 Collections.sort(entryList, new Comparator<Map.Entry<FieldDemo, Integer>>() { 28 @Override 29 public int compare(Map.Entry<FieldDemo, Integer> o1, Map.Entry<FieldDemo, Integer> o2) { 30 return o1.getValue().compareTo(o2.getValue()); 31 } 32 }); 33 34 System.out.println("entryList:" + new Gson().toJson(entryList)); 35 36 for (Map.Entry<FieldDemo, Integer> fieldDemoFieldDemoEntry : entryList) { 37 38 for (Map.Entry<FieldDemo, FieldDemo> demoFieldDemoEntry : map.entrySet()) { 39 40 String field = demoFieldDemoEntry.getKey().getField(); 41 42 if (field.equalsIgnoreCase(fieldDemoFieldDemoEntry.getKey().getField())) { 43 44 System.out.println("fieldNumber:" + new Gson().toJson(fieldDemoFieldDemoEntry.getValue())); 45 System.out.println("fieldDemoFieldDemoEntry:" + new Gson().toJson(fieldDemoFieldDemoEntry.getKey())); 46 System.out.println("demoFieldDemoEntry:" + new Gson().toJson(demoFieldDemoEntry.getValue())); 47 48 newMap.put(fieldDemoFieldDemoEntry.getKey(), demoFieldDemoEntry.getValue()); 49 } 50 } 51 } 52 }
测试
1 public static void main(String[] args) { 2 3 FieldDemo fieldDemo = new FieldDemo(); 4 5 System.out.println("fieldDemo顺序:" + new Gson().toJson(fieldDemo)); 6 7 // 让listSource2跟listSource顺序一致 8 Map<FieldDemo, FieldDemo> mapSource = getMapSource(); 9 10 System.out.println("map顺序:" + new Gson().toJson(mapSource)); 11 12 List<FieldDemo> listSource = getListSource(); 13 14 System.out.println("list顺序:" + new Gson().toJson(listSource)); 15 16 // mapSource 跟 listSource顺序一致,去除不是listSource理的字段 17 Map<FieldDemo, FieldDemo> newMap = new HashMap<>(); 18 19 sortMapFromList(listSource, mapSource, newMap); 20 21 System.out.println("排序后:" + newMap); 22 }
待开发中
-
环境设置 (Prerequisite):
- 环境
JDK1.8 - 配置
IDEA 2019 -
存在问题
暂无
- 环境
-
进阶篇 (Advanced):
研究中 ......