复杂模式的两个List与Map合并为一个Map的拼接;笛卡尔乘积处理数据问题
-
简介 (Introduction):
-
背景
数据从多个表中获取,每个表的数据条数不是唯一的,最后结果要拼接成一个Map<String,Object>的模式封装所有数据,每个数据是一条. -
结构图
要把如下图的数据模型,转化成第二个json的数据结构1 [ 2 { 3 "layer": "2", 4 "middlePhone": [ 5 { 6 "cardId": "3561245787803", 7 "hardCode": "40080095621", 8 "phoneNumber": "13564151251" 9 }, 10 { 11 "cardId": "356124580222356", 12 "hardCode": "5008009565822", 13 "phoneNumber": "12564151252" 14 }, 15 { 16 "cardId": "3561245801", 17 "hardCode": "60080095623", 18 "phoneNumber": "14564151253" 19 } 20 ], 21 "phoneOne": [ 22 { 23 "cardId": "3569999999", 24 "hardCode": "4008009556" 25 }, 26 { 27 "cardId": "45699888888", 28 "hardCode": "5008009557" 29 } 30 ], 31 "phoneTwo": [ 32 { 33 "cardId": "35699966666", 34 "hardCode": "4008009558" 35 }, 36 { 37 "cardId": "45699888777", 38 "hardCode": "5008009559" 39 } 40 ], 41 "times": "20200326,20200508" 42 } 43 ]
转化为
1 [ 2 { 3 "cardIdA": "3569999999", 4 "cardIdB": "35699966666", 5 "hardCodeB": "4008009558", 6 "hardCodeA": "4008009556", 7 "cardId": "3561245803", 8 "hardCode": "3561245803", 9 "middlePhone": "13564151251" 10 }, 11 { 12 "cardIdA": "3569999999", 13 "cardIdB": "35699966666", 14 "hardCodeB": "4008009558", 15 "hardCodeA": "4008009556", 16 "cardId": "3561245802", 17 "hardCode": "3561245802", 18 "middlePhone": "12564151252" 19 }, 20 { 21 "cardIdA": "3569999999", 22 "cardIdB": "35699966666", 23 "hardCodeB": "4008009558", 24 "hardCodeA": "4008009556", 25 "cardId": "3561245801", 26 "hardCode": "3561245801", 27 "middlePhone": "14564151253" 28 }, 29 { 30 "cardIdA": "45699888888", 31 "cardIdB": "35699966666", 32 "hardCodeB": "4008009558", 33 "hardCodeA": "5008009557", 34 "cardId": "3561245803", 35 "hardCode": "3561245803", 36 "middlePhone": "13564151251" 37 }, 38 { 39 "cardIdA": "45699888888", 40 "cardIdB": "35699966666", 41 "hardCodeB": "4008009558", 42 "hardCodeA": "5008009557", 43 "cardId": "3561245802", 44 "hardCode": "3561245802", 45 "middlePhone": "12564151252" 46 }, 47 { 48 "cardIdA": "45699888888", 49 "cardIdB": "35699966666", 50 "hardCodeB": "4008009558", 51 "hardCodeA": "5008009557", 52 "cardId": "3561245801", 53 "hardCode": "3561245801", 54 "middlePhone": "14564151253" 55 }, 56 { 57 "cardIdA": "3569999999", 58 "cardIdB": "45699888777", 59 "hardCodeB": "5008009559", 60 "hardCodeA": "4008009556", 61 "cardId": "3561245803", 62 "hardCode": "3561245803", 63 "middlePhone": "13564151251" 64 }, 65 { 66 "cardIdA": "3569999999", 67 "cardIdB": "45699888777", 68 "hardCodeB": "5008009559", 69 "hardCodeA": "4008009556", 70 "cardId": "3561245802", 71 "hardCode": "3561245802", 72 "middlePhone": "12564151252" 73 }, 74 { 75 "cardIdA": "3569999999", 76 "cardIdB": "45699888777", 77 "hardCodeB": "5008009559", 78 "hardCodeA": "4008009556", 79 "cardId": "3561245801", 80 "hardCode": "3561245801", 81 "middlePhone": "14564151253" 82 }, 83 { 84 "cardIdA": "45699888888", 85 "cardIdB": "45699888777", 86 "hardCodeB": "5008009559", 87 "hardCodeA": "5008009557", 88 "cardId": "3561245803", 89 "hardCode": "3561245803", 90 "middlePhone": "13564151251" 91 }, 92 { 93 "cardIdA": "45699888888", 94 "cardIdB": "45699888777", 95 "hardCodeB": "5008009559", 96 "hardCodeA": "5008009557", 97 "cardId": "3561245802", 98 "hardCode": "3561245802", 99 "middlePhone": "12564151252" 100 }, 101 { 102 "cardIdA": "45699888888", 103 "cardIdB": "45699888777", 104 "hardCodeB": "5008009559", 105 "hardCodeA": "5008009557", 106 "cardId": "3561245801", 107 "hardCode": "3561245801", 108 "middlePhone": "14564151253" 109 } 110 ]
-
- 核心
存在多对多的数据结构变化,考虑了很多方法和遍历方式,最后,采用了笛卡尔乘积的算法模式,实现了功能- 快速上手(Getting Started)
实体类模型
1 public class NumPhone { 2 3 private String hardCode; 4 5 private String cardId; 6 7 8 public String getHardCode() { 9 return hardCode; 10 } 11 12 public void setHardCode(String hardCode) { 13 this.hardCode = hardCode; 14 } 15 16 public String getCardId() { 17 return cardId; 18 } 19 20 public void setCardId(String cardId) { 21 this.cardId = cardId; 22 } 23 }
1 public class AllPhone { 2 3 private List<NumPhone> phoneOne; 4 5 private List<NumPhone> phoneTwo; 6 7 private String times; 8 9 private String layer; 10 11 private List<MiddleData> middlePhone; 12 13 14 public List<NumPhone> getPhoneOne() { 15 return phoneOne; 16 } 17 18 public void setPhoneOne(List<NumPhone> phoneOne) { 19 this.phoneOne = phoneOne; 20 } 21 22 public List<NumPhone> getPhoneTwo() { 23 return phoneTwo; 24 } 25 26 public void setPhoneTwo(List<NumPhone> phoneTwo) { 27 this.phoneTwo = phoneTwo; 28 } 29 30 public String getTimes() { 31 return times; 32 } 33 34 public void setTimes(String times) { 35 this.times = times; 36 } 37 38 public String getLayer() { 39 return layer; 40 } 41 42 public void setLayer(String layer) { 43 this.layer = layer; 44 } 45 46 public List<MiddleData> getMiddlePhone() { 47 return middlePhone; 48 } 49 50 public void setMiddlePhone(List<MiddleData> middlePhone) { 51 this.middlePhone = middlePhone; 52 } 53 }
测试代码
1 public class Testphone { 2 3 4 public static void main(String[] args) { 5 6 7 AllPhone dateDemo = createDateDemo(); 8 9 10 List<Map<String, Object>> stringsMap = new ArrayList<>(); 11 12 for (MiddleData middleData : dateDemo.getMiddlePhone()) { 13 14 Map<String, Object> stringMap = new HashMap<>(); 15 16 stringMap.put("middlePhone", middleData.getPhoneNumber()); 17 stringMap.put("hardCode", middleData.getCardId()); 18 stringMap.put("cardId", middleData.getCardId()); 19 20 stringsMap.add(stringMap); 21 } 22 23 List<Map<String, Object>> stringsMap2 = getResult(dateDemo.getPhoneOne(), "hardCodeA", "cardIdA"); 24 List<Map<String, Object>> stringsMap4 = getResult(stringsMap, stringsMap2); 25 List<Map<String, Object>> result1 = getResult(dateDemo.getPhoneTwo(), "hardCodeB", "cardIdB"); 26 List<Map<String, Object>> stringsMap5 = getResult(stringsMap4, result1); 27 28 29 System.out.println("map:" + JSONArray.fromObject(stringsMap4)); 30 System.out.println("map:" + JSONArray.fromObject(stringsMap5)); 31 System.out.println("map:" + JSONArray.fromObject(dateDemo)); 32 33 34 35 36 37 } 38 39 private static List<Map<String, Object>> getResult(List<Map<String, Object>> result, 40 List<Map<String, Object>> stringsMap 41 ){ 42 43 List<List<Map<String, Object>>> stringsMapAll = getCombinResult(stringsMap, result); 44 List<List<Map<String, Object>>> result2 = new ArrayList<>(); 45 descartes(stringsMapAll, result2, 0, new ArrayList<>()); 46 47 System.out.println("map:" + JSONArray.fromObject(result2)); 48 return getCombineFinalResult(result2); 49 } 50 51 private static List<Map<String, Object>> getCombineFinalResult(List<List<Map<String, Object>>> result) { 52 List<Map<String, Object>> stringsMap4 = new ArrayList<>(); 53 54 for (List<Map<String, Object>> maps : result) { 55 Map<String, Object> stringsMap3 = new HashMap<>(); 56 Map<String, Object> objectMap = maps.get(0); 57 58 Map<String, Object> objectMap1 = maps.get(1); 59 60 stringsMap3.putAll(objectMap); 61 stringsMap3.putAll(objectMap1); 62 63 stringsMap4.add(stringsMap3); 64 } 65 return stringsMap4; 66 } 67 68 private static List<List<Map<String, Object>>> getCombinResult(List<Map<String, Object>> stringsMap, List<Map<String, Object>> stringsMap2) { 69 List<List<Map<String, Object>>> stringsMapAll = new ArrayList<>(); 70 stringsMapAll.add(stringsMap); 71 stringsMapAll.add(stringsMap2); 72 return stringsMapAll; 73 } 74 75 private static List<Map<String, Object>> getResult(List<NumPhone> phoneNumbers,String hardKey,String cardKey) { 76 77 List<Map<String, Object>> stringsMap = new ArrayList<>(); 78 for (NumPhone numPhone : phoneNumbers) { 79 80 Map<String, Object> stringMap = new HashMap<>(); 81 82 stringMap.put(hardKey, numPhone.getHardCode()); 83 stringMap.put(cardKey, numPhone.getCardId()); 84 85 stringsMap.add(stringMap); 86 } 87 return stringsMap; 88 } 89 90 91 public static AllPhone createDateDemo() { 92 93 AllPhone allPhone = new AllPhone(); 94 95 96 List<NumPhone> numPhonesOne = new ArrayList<>(); 97 98 99 NumPhone numPhone = new NumPhone(); 100 101 numPhone.setHardCode("4008009556"); 102 numPhone.setCardId("3569999999"); 103 104 NumPhone numPhone2 = new NumPhone(); 105 106 numPhone2.setHardCode("5008009557"); 107 numPhone2.setCardId("45699888888"); 108 109 numPhonesOne.add(numPhone); 110 numPhonesOne.add(numPhone2); 111 112 113 List<NumPhone> numPhonesTwo = new ArrayList<>(); 114 115 NumPhone numPhone3 = new NumPhone(); 116 117 numPhone3.setHardCode("4008009558"); 118 numPhone3.setCardId("35699966666"); 119 120 NumPhone numPhone4 = new NumPhone(); 121 122 numPhone4.setHardCode("5008009559"); 123 numPhone4.setCardId("45699888777"); 124 125 numPhonesTwo.add(numPhone3); 126 numPhonesTwo.add(numPhone4); 127 128 129 List<MiddleData> middlePhone = new ArrayList<>(); 130 131 MiddleData middleData1 = new MiddleData(); 132 133 middleData1.setPhoneNumber("13564151251"); 134 middleData1.setHardCode("40080095621"); 135 middleData1.setCardId("3561245803"); 136 137 MiddleData middleData2 = new MiddleData(); 138 middleData2.setPhoneNumber("12564151252"); 139 middleData2.setHardCode("50080095622"); 140 middleData2.setCardId("3561245802"); 141 142 MiddleData middleData3 = new MiddleData(); 143 middleData3.setPhoneNumber("14564151253"); 144 middleData3.setHardCode("60080095623"); 145 middleData3.setCardId("3561245801"); 146 147 middlePhone.add(middleData1); 148 middlePhone.add(middleData2); 149 middlePhone.add(middleData3); 150 151 allPhone.setPhoneOne(numPhonesOne); 152 allPhone.setPhoneTwo(numPhonesTwo); 153 allPhone.setMiddlePhone(middlePhone); 154 155 allPhone.setLayer("2"); 156 157 allPhone.setTimes("20200326,20200508"); 158 159 160 return allPhone; 161 162 } 163 164 165 private static void descartes(List<List<Map<String, Object>>> dimvalue, 166 List<List<Map<String, Object>>> result, 167 int layer, 168 List<Map<String, Object>> curList) { 169 if (layer < dimvalue.size() - 1) { 170 if (dimvalue.get(layer).size() == 0) { 171 descartes(dimvalue, result, layer + 1, curList); 172 } else { 173 for (int i = 0; i < dimvalue.get(layer).size(); i++) { 174 List<Map<String, Object>> list = new ArrayList<>(curList); 175 list.add(dimvalue.get(layer).get(i)); 176 descartes(dimvalue, result, layer + 1, list); 177 } 178 } 179 } else if (layer == dimvalue.size() - 1) { 180 if (dimvalue.get(layer).size() == 0) { 181 result.add(curList); 182 } else { 183 for (int i = 0; i < dimvalue.get(layer).size(); i++) { 184 List<Map<String, Object>> list = new ArrayList<>(curList); 185 list.add(dimvalue.get(layer).get(i)); 186 result.add(list); 187 } 188 } 189 } 190 } 191 }
- 快速上手(Getting Started)
-
环境设置 (Prerequisite):
-
环境
JDK1.8 -
配置
windows和mac/linux环境自行配置 -
存在问题
暂无
-
-
进阶篇 (Advanced):
待开发 ......
十年磨一剑,一剑破万法