putIfAbsent
时间: 2023-07-27 22:12:52 浏览: 61
putIfAbsent 是Java中的一个Map接口的方法。它的作用是向Map中添加一个键值对,如果该键已经存在则不进行添加,直接返回原有的值。
该方法的定义为:
```java
V putIfAbsent(K key, V value)
```
其中,K代表键的类型,V代表值的类型。当Map中不存在指定的键时,该方法将键值对添加到Map中,并返回null;如果Map中已经存在指定的键,则该方法不进行任何操作,直接返回原有的值。
示例代码:
```java
Map<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);
map.putIfAbsent("one", 3);
System.out.println(map.get("one")); // 输出1
```
在上面的代码中,我们先向Map中添加了两个键值对:"one"对应的值为1,"two"对应的值为2。然后使用putIfAbsent方法向Map中添加了一个键值对:"one"对应的值为3。由于Map中已经存在键为"one"的键值对,因此putIfAbsent方法不进行任何操作,直接返回原有的值1。
需要注意的是,putIfAbsent方法是线程安全的,多线程环境下可以安全地使用。
相关问题
ConcurrentHashMap putIfAbsent
ConcurrentHashMap's `putIfAbsent(key, value)` is used to atomically associate the specified key with the given value if it is not already associated with a value in the map. It returns the previous value associated with the key, or `null` if there was no mapping for the key.
Here's an example of how to use `putIfAbsent()`:
```java
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
map.put("key1", 1);
// putIfAbsent will only add the mapping if there's no existing mapping for the key
Integer previousValue = map.putIfAbsent("key1", 2);
System.out.println(previousValue); // Output: 1
previousValue = map.putIfAbsent("key2", 2);
System.out.println(previousValue); // Output: null
```
In the first `putIfAbsent()` call, since "key1" already has a value of 1 associated with it, the method does not modify the map and returns the previous value (1).
In the second `putIfAbsent()` call, "key2" has no existing mapping, so the method adds a new mapping with the specified value of 2 and returns `null`.
The `putIfAbsent()` operation is atomic, meaning that multiple threads can safely invoke this method concurrently without running into issues like race conditions.
java putIfAbsent
`putIfAbsent` 是 Java 中 `Map` 接口定义的一个方法。它的作用是在指定的键不存在时,将指定的值与键关联起来,并将其放入 `Map` 中。
具体语法如下:
```java
V putIfAbsent(K key, V value)
```
参数解释:
- `key`:要关联的键。
- `value`:要与键关联的值。
该方法的返回值表示与给定键关联的旧值。如果键不存在,则返回 `null`。
示例用法:
```java
Map<Integer, String> map = new HashMap<>();
map.put(1, "Java");
map.putIfAbsent(2, "Python");
map.putIfAbsent(1, "C++");
System.out.println(map); // 输出: {1=Java, 2=Python}
```
在上面的示例中,我们先使用 `put` 方法将键值对 `(1, "Java")` 放入 `map` 中。然后使用 `putIfAbsent` 方法尝试将键值对 `(2, "Python")` 放入 `map` 中,因为键 `2` 之前并不存在,所以成功放入。最后又使用 `putIfAbsent` 方法尝试将键值对 `(1, "C++")` 放入 `map` 中,但由于键 `1` 已经存在,所以不会发生变化。最终输出 `map` 的内容为 `{1=Java, 2=Python}`。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)