• 简介 (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 }
  • 环境设置 (Prerequisite):

    • 环境
      JDK1.8

    • 配置
      windows和mac/linux环境自行配置

    • 存在问题
      暂无

  • 进阶篇 (Advanced): 
    待开发 ......

复杂模式的两个List与Map合并为一个Map的拼接;笛卡尔乘积处理数据问题的更多相关文章

  1. Clojure:将两个list合并成一个map

    假设我们有两个list,分别是: (def a [“one” “two” “three”]) (def b [1 2 3]) 我们要把它们合为一个键值对应的map,做法很简单: 1. 先将a和b合为一 ...

  2. java map添加另一个map时候 键值对的类型要一致

    java map添加另一个map时候 键值对的类型要一致

  3. Mysql训练:两个表中使用 Select 语句会导致产生 笛卡尔乘积 ,两个表的前后顺序决定查询之后的表顺序

    力扣:超过经理收入的员工 Employee 表包含所有员工,他们的经理也属于员工.每个员工都有一个 Id,此外还有一列对应员工的经理的 Id. +----+-------+--------+----- ...

  4. Java 将两个Map对象合并为一个Map对象

    实现方式是通过 putAll() 方法将多个 map 对象中的数据放到另外一个全新的 map 对象中,代码如下所示,展示了两个 map 对象的合并,如果是多个 map 合并也是用这种方式. publi ...

  5. 怎样将结构完全一样的两个表的内容合并到一个表中,SQL语句

      标签: SQL合并数据 2013-08-21 10:41 489人阅读 评论(0) 收藏 举报  分类: Oracle数据库(14)  select * into 新表名 from (select ...

  6. C++ map insert 另一个map的子集

    C++map中 会有insert操作,举个例子 存在map A,我们截取一部分到map B中,void insert (InputIterator first, InputIterator last) ...

  7. PHP将两个二维数组合并为一个二维数组的方法

    $a = array(0 => Array(id => 66,class_name => math),1 => Array(id => 67,class_name =&g ...

  8. C语言两个升序递增链表逆序合并为一个降序递减链表,并去除重复元素

    #include"stdafx.h" #include<stdlib.h> #define LEN sizeof(struct student) struct stud ...

  9. 探究Repository模式的两种写法与疑惑

    现如今DDD越来越流行,园子里漫天都是介绍关于它的文章.说到DDD就不能不提Repository模式了,有的地方也叫它仓储模式. 很多时候我们对Repository都还停留在Copy然后使用的阶段, ...

  10. 通知模式实现两个textField传值及模态视图——iOS开发

    通知模式实现两个textField传值及模态视图--iOS开发 利用通知模式,实现两个不同界面的textField之间的传值,在界面二输入字符,传值到前一界面的textField. 界面的切换,这里临 ...

随机推荐

  1. #树状数组,CDQ分治#洛谷 4390 [BOI2007]Mokia 摩基亚

    题目 分析 考虑离线处理,那么询问区间和就可以转换为四个询问, CDQ分治按横坐标处理询问,树状数组维护前缀和就可以了 代码 #include <cstdio> #include < ...

  2. 开发板如何适配OpenHarmony 3.2

      简介 OpenAtom OpenHarmony(以下简称"OpenHarmony") 3.2 Beta5版本在OpenHarmony 3.1 Release版本的基础上,有以下 ...

  3. JVM—垃圾收集器

    JVM-垃圾收集器 什么是垃圾 没有被引用的对象就是垃圾. 怎么找到垃圾 引用计数法 当对象引用消失,对象就称为垃圾. 对象消失一个引用,计数减去一,当引用都消失了,计数就会变为0.此时这个对象就会变 ...

  4. openGauss单机部署

    openGauss 单机部署 一.安装环境 操作系统:虚拟机 VMware.CentOS7.9 环境设置: (1)虚拟机内存 3G.磁盘 100G (2)系统版本修改 一开始使用了 centos8,无 ...

  5. 什么是coredump

    什么是 coredump 介绍 在 Linux 开发中,我们经常听到程序员说我的程序 core 掉了,通常出现这类的问题是低级 bug 中的内存访问越界.使用空指针.堆栈溢出等情况.使程序运行过程中异 ...

  6. 【VMware vCenter】使用Reduced Downtime Update (RDU)升级更新vCenter Server。

    Reduced Downtime Upgrade (RDU) 减少停机时间更新是用于vCenter Server更新升级的一种新的方式,顾名思义,主要目的就是为了减少在vCenter Server更新 ...

  7. CentOS6.5安装与配置JDK-7

    系统环境:centos-6.5 安装方式:rpm安装 软件:jdk-7-linux-i586.rpm 下载地址:http://www.oracle.com/technetwork/java/javas ...

  8. spring boot yaml 配置[三]

    前言 我们知道java 因为历史的原因,一直有一个配置地狱的痛点.那么如何解决掉它呢? spring boot 是一柄利器,但是呢,还是要配置. 看来配置的避免不了的了. 那么如何可以减轻这种痛苦呢? ...

  9. bookstack书栈网docker搭建

    准备好数据后,直接运行以下命令即可. docker run -d --name bookstack \ --restart always \ --privileged=true\ -p 8181:81 ...

  10. axios请求时获取不到错误提示问题。

    前端方面使用axios请求,由于新增时,有的条件格式填写错误.后端返回412状态码. ,axios可能封装不完善,他获取数据使状态码为4开头的统统不暴露出去,导致请求时,412这样的状态码,获取不到里 ...