一位map,二位map变成字符串后,再变成map的解析过程

public class G
{
    public static void main(String[] args){
       
         Map map=new HashMap();
         map.put("a", 1);
         map.put("b", 2);
         map.put("c", 33);
         map.put("d", "");
         map.put("","");
         
         Map map2=new HashMap();
         map2.put("a", map);
         map2.put("b", map);
         map2.put("", map);
         
         String m1=map.toString();
         String m= map2.toString();
        
         System.out.println("原始的一位map:"+m1);
         System.out.println("原始的二位map:"+m);
         System.out.println("-----------------------------------------------------");
        
//        字符串中有一个map的输出
         System.out.println(mp1(m1)); 
        
//        字符串中有两个map的输出
         System.out.println(mp2(m));
    }
   
    public static Map mp2(String m){
        ArrayList al=new ArrayList();
        ArrayList ak=new ArrayList();
        ArrayList aa=new ArrayList();   
        ArrayList ab=new ArrayList();
        ArrayList ac=new ArrayList();       
//放一位里面的map值       
         HashMap hm=new HashMap();   
         String[] sar=m.split("},");
         for(String s:sar){
             al.add(s);
         }
        String sm1=al.get(0).toString();
        String stt="=\\{";
        String[] smm1=sm1.split(stt);
        for(String ss:smm1){
            ak.add(ss);
        }
         String[] ssw=ak.get(1).toString().replace(" ", "").split(",");
         for(String stst:ssw){
         aa.add(stst);
         }
         for(int i=0;i<aa.size();i++){
        if(aa.get(i).toString().length()==1){
            hm.put("", "");
        }else{
             String[] strs=aa.get(i).toString().split("=",2);
             for(String st:strs){
                ab.add(st);
                }
             }
         }
         for(int i=0;i<ab.size();i++){
             if(i%2==0){
                 hm.put(ab.get(i), ab.get(i+1));
             }
         }
    //  二位map数组        
         String syiwei=hm.toString();
         String[] erwei=m.replace(syiwei, "").replace("{", "").replace("}", "").replace(" ", "").split(",");
         ArrayList alist=new ArrayList();
         ArrayList alistb=new ArrayList();
         HashMap hmm= new HashMap();
         for(String stmap:erwei){
             alist.add(stmap);
            }
              for(int i=0;i<alist.size();i++){
             if(alist.get(i).toString().length()==1){
                 hmm.put("", hm);
             }else{
                  String[] strs=alist.get(i).toString().split("=",2);
                  for(String st:strs){
                     alistb.add(st);
                     }
                  }
              }
              for(int i=0;i<alistb.size();i++){
                  if(i%2==0){
                      hmm.put(alistb.get(i), hm);
                  }
              }   
             return hmm;
    }
   
public static Map mp1(String m){
         String[] erwei=m.replace("{", "").replace("}", "").replace(" ", "").split(",");
         ArrayList alist=new ArrayList();
         ArrayList alistb=new ArrayList();
         HashMap hmm= new HashMap();
         for(String stmap:erwei){
             alist.add(stmap);
            }
 
              for(int i=0;i<alist.size();i++){
             if(alist.get(i).toString().length()==1){
                 hmm.put("", "");
             }else{
                  String[] strs=alist.get(i).toString().split("=",2);
                  for(String st:strs){
                     alistb.add(st);
                     }
                  }
              }
              for(int i=0;i<alistb.size();i++){
                  if(i%2==0){
                      hmm.put(alistb.get(i), alistb.get(i+1));
                  }
              }   
             return hmm;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值