两个嵌套map合并

目的是为了解决两个map合并,思路仅供参考

做的是模型转换,绞尽脑汁也没有想到好的解决方案,后来突然想到,我以一个为目标,另外一个一层一层写是不是就可以了呢?

package com.cic.businsurance.regulatory.vehmodeltrans.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections4.map.MultiValueMap;

import java.util.*;

public class ceshisuccess {
    public static void main(String[] args) {
        MultiValueMap<String, IdentityHashMap<String, String>> destObjectHashMap = new MultiValueMap<>();

        IdentityHashMap<String, String> stringIdentityHashMap = new IdentityHashMap<>();
        stringIdentityHashMap.put("querySequenceNo", "342423423432");
        stringIdentityHashMap.put("querySequenceNo", "1111");
        stringIdentityHashMap.put("checkType", "VESESDSDRWER");
        stringIdentityHashMap.put("checkType", "2222");
        destObjectHashMap.put("/proposal1/demo/demo2/pasepart", stringIdentityHashMap);
        MultiValueMap<String, IdentityHashMap<String, String>> destObjectHashMap2 = new MultiValueMap<>();

        IdentityHashMap<String, String> stringIdentityHashMap1 = new IdentityHashMap<>();
        stringIdentityHashMap1.put("vehicleClassCode", "J");
        destObjectHashMap2.put("/proposal1/demo/demo2/amvehicle", stringIdentityHashMap1);

        IdentityHashMap<String, String> stringIdentityHashMap3 = new IdentityHashMap<>();
        stringIdentityHashMap3.put("thisisademo", "1");
        destObjectHashMap.put("/fanhuikong", stringIdentityHashMap3);

        IdentityHashMap<String, String> stringIdentityHashMap4 = new IdentityHashMap<>();
        stringIdentityHashMap4.put("thisisademo", "1");
        destObjectHashMap.put("/fanhuikong1", stringIdentityHashMap4);
//        String s = "proposal1.demo1.amvehicle.12";
//        String s1 = "proposal2.demo.amvehicle1.23";
//        String s2 = "/proposal1/demo/amvehicle";
//        int indexOf = s2.lastIndexOf("/");
//        System.out.println(s2.substring(0, indexOf));
        digui(destObjectHashMap, destObjectHashMap2);
//        System.out.println(s);
//        String[] dem1 = {"1", "2", "3", "4"};
//        for (int i = 0; i < demo.length; i++) {
//            if (demo[i].equals(dem1[i])) {
//                System.out.println(demo[i]);
//            } else {
//                continue;
//            }
//        }
    }

    private static MultiValueMap<String, Map> digui(MultiValueMap<String, IdentityHashMap<String, String>> destObjectHashMap, MultiValueMap<String, IdentityHashMap<String, String>> destObjectHashMap2) {
        Map lastObjectHashMap = new HashMap();
        Iterator<String> firstIterator = destObjectHashMap.keySet().iterator();
        Iterator<String> secondIterator = destObjectHashMap2.keySet().iterator();
        String secondKeyPath = "";
        String secondLastKeyPath = "";
        IdentityHashMap secondIdentityHashMap = new IdentityHashMap();
        if (secondIterator.hasNext()) {
            secondKeyPath = secondIterator.next();
            secondLastKeyPath = secondKeyPath;
            ArrayList arrayList = (ArrayList) destObjectHashMap2.get(secondKeyPath);
            secondIdentityHashMap = (IdentityHashMap) arrayList.get(0);
        }
//        int size = destObjectHashMap.keySet().iterator().
        int totalSize = destObjectHashMap.totalSize();
        int lastLength = 1;
//        是否从来都没有将map2 放入到map1中
        boolean neverGet = true;
        //拼接map2的key信息
        String secondTargetString = "";
        while (firstIterator.hasNext()) {
            System.out.println(lastLength);
            secondKeyPath = secondLastKeyPath;
            String firstKeyPath = firstIterator.next();
            ArrayList arrayList = (ArrayList) destObjectHashMap.get(firstKeyPath);
            IdentityHashMap firstIdentityHashMap = (IdentityHashMap) arrayList.get(0);
            int firstIndex = 1;
            int secondIndex = 1;
//            拼接map1的key信息
            String firstTargetString = "";

//            boolean secondSwitch = true;

            while (firstIndex != 0) {
//                firstKeyPath
                firstIndex = firstKeyPath.lastIndexOf("/");

                IdentityHashMap firstObjectHashMap = new IdentityHashMap<>();

                firstTargetString = firstKeyPath.substring(firstIndex + 1);
                firstKeyPath = firstKeyPath.substring(0, firstIndex);
                firstObjectHashMap.put(firstTargetString, firstIdentityHashMap);
                firstIdentityHashMap = firstObjectHashMap;
                JSONObject jsonObject = new JSONObject(firstObjectHashMap);
                System.out.println(jsonObject.toJSONString());
                if (!"".equals(secondTargetString)) {
                    if (!firstKeyPath.contains(secondTargetString)) {
                        if (totalSize == lastLength&&neverGet) {
                            lastObjectHashMap.putAll(secondIdentityHashMap);
                        }
                        continue;
                    }
                }
                if (neverGet) {
                    while (secondIndex != 0) {
                        IdentityHashMap secondObjectHashMap = new IdentityHashMap<>();
                        secondIndex = secondKeyPath.lastIndexOf("/");
                        if ("".equals(secondKeyPath)) {
                            break;
                        }
                        if (firstKeyPath.equals(secondKeyPath)) {
                            firstObjectHashMap.putAll(secondIdentityHashMap);
//                            secondSwitch = false;
                            neverGet = false;
                            break;
                        } else {
                            secondTargetString = secondKeyPath.substring(secondIndex + 1);
                            secondKeyPath = secondKeyPath.substring(0, secondIndex);
                            secondObjectHashMap.put(secondTargetString, secondIdentityHashMap);
                            secondIdentityHashMap = secondObjectHashMap;

                        }

                        if (secondIndex == 0) {
                            System.out.println(secondTargetString);
                        }
                        System.out.println("cishu----" + secondTargetString);
                    }
                }


            }
            if (firstIndex == 0) {
                lastObjectHashMap.putAll(firstIdentityHashMap);
                System.out.println(new JSONObject(lastObjectHashMap));
            }
            lastLength++;
        }
//        if (s.equals(s1)) {
//            return s;
//        } else {
//            while (s.lastIndexOf(".") > -1) {
//                s = s.substring(0, s.lastIndexOf("."));
//                int i = s.split("\\.").length;
//                System.out.println("第一个的个数"+i);
//                System.out.println("修改后s-----"+s);
//                while (s1.lastIndexOf(".") >= -1) {
//                    int j = s1.split("\\.").length;
//                    System.out.println("第二个的个数"+j);
//                    System.out.println("修改前s1-----"+s1);
//                    if (s.equals(s1)) {
//                        System.out.println("true");
//                        System.out.println(s);
//                        return s;
//                    }
//                    if (i >= j) {
//                        break;
//                    }
//                    s1 = s1.substring(0, s1.lastIndexOf("."));
//                    System.out.println("修改后s1-----"+s1);
//                }
//            }
//
//        }
        System.out.println(new
                JSONObject(lastObjectHashMap));
        return new MultiValueMap<>();

    }

    private static void AssemblySecondMapInfo(String secondKeyPath, IdentityHashMap secondIdentityHashMap) {
        int secondIndex = 1;
        String secondTargetString;
        while (secondIndex != 0) {
            IdentityHashMap secondObjectHashMap = new IdentityHashMap<>();
            secondIndex = secondKeyPath.lastIndexOf("/");
            secondTargetString = secondKeyPath.substring(secondIndex + 1);
            secondKeyPath = secondKeyPath.substring(0, secondIndex);
            secondObjectHashMap.put(secondTargetString, secondIdentityHashMap);
            secondIdentityHashMap = secondObjectHashMap;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值