map反转key value

2021-02-25 15:24:21 浏览数 (1)

 以下是反转并且替换的效果,根据您的实际需求,自行修改

代码语言:javascript复制
/**
 * @param messageTemplateJson {"{1}":"企业名称","{2}":"试工名称","{3}":"试工时间","{4}":"试工地址","{5}":"联系人","{6}":"联系电话","{7}":"注意事项内容"}
 * @return {"企业名称":"%s","试工名称":"%s","试工时间":"%s","试工地址":"%s","联系人":"%s","联系电话":"%s","注意事项内容":"%s"}
 */
代码语言:javascript复制
String json = "{"{1}":"企业名称","{2}":"试工名称","{3}":"试工时间","{4}":"试工地址","{5}":"联系人","{6}":"联系电话","{7}":"注意事项内容"}";
TreeMap stringStringMap = JSONObject.parseObject(json, new TypeReference>() {});
HashBiMap stringStringHashBiMap = HashBiMap.create(stringStringMap);

BiMap inverse = stringStringHashBiMap.inverse();

String inverseEdStr = JSON.toJSONString(inverse);
System.out.println("inverseEdStr = "   inverseEdStr);

List collect = Stream.iterate(1, i -> i   1).limit(inverse.size()).collect(Collectors.toList())
        .parallelStream().map(i -> "{"   i   "}").collect(Collectors.toList());

System.out.println("collect = "   JSON.toJSONString(collect));
for (String s1 : collect) {
    inverseEdStr = inverseEdStr.replace(s1,"%s");
}
System.out.println("s final  = "   inverseEdStr);
String format = String.format(inverseEdStr,
        "111", "111", "111", "111", "111", "111", "111");

System.out.println("format = "   format);

BiMap biMap = HashBiMap.create();
biMap.put("1","Tom");
biMap.put("2","Jerry");
// 放入重复值value  将会报错
// biMap.put("2","Tom");
// 相同Value覆盖前一个
//biMap.forcePut("2","Tom");

System.out.println(biMap);

// key value反转
System.out.println(biMap.inverse());

0 人点赞