Map<String, List<String>> 用putIfAbsent添加元素
时间: 2024-10-21 07:18:39 浏览: 38
在 `Map<String, List<String>>` 类型的数据结构中,当你想用 `putIfAbsent` 方法添加元素时,实际上是将一个新的列表(List<String>)关联到给定的字符串键上。如果这个键已经存在于 map 中,`putIfAbsent` 将不会替换当前关联的列表,而是在原列表的基础上增加新的元素。如果没有找到对应的键,那么就会创建一个新的键值对,其中键是字符串,值是包含指定元素的新列表。
下面是一个示例:
```java
Map<String, List<String>> map = new ConcurrentHashMap<>(); // 使用并发哈希映射以保证线程安全性
List<String> elementsToAdd = Arrays.asList("elem1", "elem2");
// 如果键 "key" 还未存在,就添加元素
List<String> newList = map.computeIfAbsent("key", k -> new ArrayList<>());
newList.addAll(elementsToAdd);
// 如果键已存在,新元素会被追加到已有列表里
// 由于 computeIfAbsent 提供了默认的空列表,所以不需要额外的判断
newList = map.get("key"); // 这里就是原列表加上新元素后的结果
```
注意,`computeIfAbsent` 方法结合了 `putIfAbsent` 和 `get` 的功能,使得操作更为简洁。
相关问题
List<Map<String,String>> 去重
List<Map<String, String>> 是一个包含多个键值对(String 对 String)的地图(Map)的列表,要去重通常是基于某个或某些键(Key)的值。由于每个map都是独立的,并非基于某种特定顺序或关联关系,所以直接去重可能会有些复杂。这里有几个常见的方法:
1. 使用Set:如果允许丢失部分信息,可以直接将整个map转换成set(Set不存储重复元素),然后再转回list。例如:
```java
Set<Map<String, String>> distinctMaps = new HashSet<>(list);
List<Map<String, String>> uniqueList = new ArrayList<>(distinctMaps);
```
但这只会保留第一个匹配项的map,其他具有相同键值对的map都会被丢弃。
2. 自定义比较:如果需要保留完整的map并基于某个键的值判断是否重复,可以自定义比较规则,如使用HashMap,因为它的键值对是基于哈希值存取的,理论上不会有重复键值对:
```java
Map<String, Map<String, String>> mapSet = new HashMap<>();
for (Map<String, String> map : list) {
mapSet.putIfAbsent(map.get("uniqueKey"), map);
}
List<Map<String, String>> uniqueList = new ArrayList<>(mapSet.values());
```
这里假设"uniqueKey"是你希望作为唯一标识的键。
stream流对List<Map<String,String>>某个字段去重
你好,关于你的问题,可以使用Java 8中的stream流的distinct()方法对List<Map<String,String>>某个字段进行去重。具体代码演示可以参考以下示例:
```
List<Map<String,String>> list = new ArrayList<>();
list.add(new HashMap<String,String>(){{put("id","1"); put("name","John");}});
list.add(new HashMap<String,String>(){{put("id","2"); put("name","Peter");}});
list.add(new HashMap<String,String>(){{put("id","3"); put("name","John");}});
list.add(new HashMap<String,String>(){{put("id","4"); put("name","Mary");}});
List<Map<String,String>> distinctList = list.stream().filter(
distinctByKey(map -> map.get("name"))
).collect(Collectors.toList());
System.out.println(distinctList);
// Output: [{id=1, name=John}, {id=2, name=Peter}, {id=4, name=Mary}]
public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
Map<Object, Boolean> seen = new ConcurrentHashMap<>();
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
```
希望能帮到你,如果你有其他问题可以继续提出。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)