computeIfAbsent() 的使用
参考链接 https://blog.youkuaiyun.com/qq_25074189/article/details/110530768
判断一个map中是否存在某个key,如果存在则返回这个key对应value的数据;如果不存在,则以该key为key,再以自定义的value为value放入map中,并返回这个value的数据。需要注意,对返回 value数据的修改会影响到map集合中原value的数据。
public class TestComputeIfAbsent {
public static void main(String[] args) {
HashMap<String, Set<String>> hashMap = new HashMap<>();
Set<String> set = new HashSet<>();
set.add("1");
// hashMap:{demo1=[1]}
hashMap.put("demo1", set);
// hashMap:{demo1=[1,2]} 注意会改变原来value的值
hashMap.computeIfAbsent("demo1", key -> new HashSet()).add("2");
// hashMap:{demo1=[1,2],demo2=[3]} 注意新建了一个key-value数据放入hashMap
hashMap.computeIfAbsent("demo2", key ->new HashSet()).add("3");
}
}
Json将字符串转换任意数据类型的对象
Map<String,List<String>> map=JSON.parseObject(json.toJSONString(),new TypeReference<Map<String,List<String>>>(){});