首页 > 其他分享 >Map<String, Map<String, String>>转String,再转回Map

Map<String, Map<String, String>>转String,再转回Map

时间:2023-04-12 18:31:47浏览次数:35  
标签:Map HashMap put entry 转回 new String


import org.junit.Test;

import java.util.*;
import java.util.regex.Pattern;

/**
 *
 */
public class Test2 {

    @Test
    public void test() {
        Map<String, String> testMap1 = new HashMap<String, String>();
        testMap1.put("key1", "val1");
        testMap1.put("key2", "val2");

        Map<String, String> testMap2 = new HashMap<String, String>();
        testMap2.put("key21", "val21");
        Map<String, Map<String, String>> testMapMap1 = new HashMap<String, Map<String, String>>();
        testMapMap1.put("key1", testMap1);
        testMapMap1.put("key2", testMap2);

        System.out.println(map2String(testMapMap1));
        System.out.println(mapString2Map(map2String(testMapMap1)));
    }


    public static String map2String(Map map){
        java.util.Map.Entry entry;
        StringBuffer sb = new StringBuffer();
        for(Iterator iterator = map.entrySet().iterator(); iterator.hasNext();)
        {
            entry = (java.util.Map.Entry)iterator.next();
            sb.append(entry.getKey().toString()).append( "'" ).append(null==entry.getValue()?"":
                    entry.getValue().toString().substring(1, entry.getValue().toString().length() - 1)).append (iterator.hasNext() ? "^" : "");
        }
        return sb.toString();
    }

    public static Map mapString2Map(String mapString){
        Map map = new HashMap();
        java.util.StringTokenizer items;
        for(StringTokenizer entrys = new StringTokenizer(mapString, "^"); entrys.hasMoreTokens();
            map.put(items.nextToken(), items.hasMoreTokens() ? (mapStringToMap((String) (items.nextToken()))) : null))
            items = new StringTokenizer(entrys.nextToken(), "'");
        return map;
    }

    public static Map mapStringToMap(String text){
        HashMap<String,String> data = new HashMap<String,String>();
        Pattern p = Pattern.compile("[\\{\\}\\=\\, ]++");
        String[] split = p.split(text);
        for ( int i=0; i+2 <= split.length; i+=2 ){
            data.put( split[i], split[i+1] );
        }
        return data;
    }

}




标签:Map,HashMap,put,entry,转回,new,String
From: https://blog.51cto.com/u_16068296/6186083

相关文章

  • vue map 从一组对象中得到一个新的对象
    示例数据:letlist=[{id:1,name:"张三"},{id:2,name:"李四"}]1、使用map 取name属性得到一个string数组letarr= list.map(pro=>pro.name);2、使用map得到一个新的对象集合letarr=list.map(pro=>({lable:pro.name,value:pro.id})) ......
  • 简单的nmap扫描脚本
    简单的nmap扫描脚本下载链接:https://github.com/baimao-box/nmapscan/tree/main一次扫描命令解释:二次扫描命令解释:总结用这个脚本扫描时,比较隐蔽和快速,二次扫描时,扫描的端口信息也很详细。一次扫描时只需要输入目标ip即可,二次扫描时只输入要扫描的端口即可。扫描后的文件的存储位......
  • 使用 InterpolatedString 减少字符串拼接的 GC
    原视频链接考虑到Unity准备在2024年前后,推出基于dotnetRuntime的版本,本篇文章也标记为Unity分类,等后面Unity准备好之后,再对新版的客户端进行改造在日常开发过程中,字符串的拼接通常会占用大量的GC,通常拼接字符串我们会使用如下几种方法1.1+"/"+2+"/"+32......
  • MyBatis中XXMapper示例记录
    XXMapper.xml的结构示例如下,包括<resultMap>、<id>、<result>、<select>、<update>、<foreach>、<if>标签的使用:<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper......
  • MapStruct使用说明
    目录1、简介2、构建2.1、ApacheMaven3、定义映射器3.1、基本映射器3.2、表达式3.3、自定义方法3.4、多个源对象3.4.1、源数据来源多个对象3.4.2、源数据来源普通参数3.5、嵌套对象属性3.6、更新bean实例3.7、public访问权限修饰符修饰的成员3.8、生成器3.9、构造器3.10、映射Map......
  • Logistic map
    https://en.wikipedia.org/wiki/Logistic_mapPeriodThreeImpliesChaos......
  • 【工具类】-Map
    MapentrySet遍历,在键和值都需要时使用(最常用)Map<Integer,Integer>map=newHashMap<Integer,Integer>();map.put(1,2);for(Map.Entry<Integer,Integer>entry:map.entrySet()){System.out.println("key="......
  • 论文解析 -- A Systematic Mapping Study in AIOps
    AIOPS论文的综述如何挑选论文,如何选取keywords 搜索的3个论文库, Weselectthreeonlinesearchdatabasesthatareappropriateforthescopeofinvestigation:IEEEXplore,ACMDigitalLibraryandarXiv. 对于挑选出的论文进行分类,分类标准是,targetcomponents......
  • 【随手记】解决mybatis返回List<map>类型的数据时 无序 并且 不能返回空值
    返回结果无序希望表格的列能根据数据库查出来的数据保持一致,但是返回页面的结果集是无序在mybatis中使用List<Map>结构接收数据,发现输入的sql语句结果并不是按照输入的字段名顺序返回的。例如输入selectcol1,col2,col3fromtable却返回col2col3col1***......
  • 第九篇 手写原理代码 - 数组 【 实现 forEach、map、filter、every、some 】
    1、forEachArray.prototype.my_forEach=function(callback){for(leti=0;i<this.length;i++){callback(this[i],i,this);}};2、mapArray.prototype.my_map=function(callback){constarr=[];for(leti=0;i<this.length;......