Java8 Map集合中put()与putIfAbsent()的区别

Map集合中put与putIfAbsent的区别

put方法:

V put(K key, V value);

putIfAbsent方法:

V putIfAbsent(K key, V value);

这两种方法都是以key-value键值对的形式存在到map集合中,那么它们两个有什么区别呢?

我们可以从map官网注释中看出:

1.使用put方法添加键值对,如果map集合中没有该key对应的值,则直接添加,并返回null,如果已经存在对应的值,则会覆盖旧值,value为新的值。

2.使用putIfAbsent方法添加键值对,如果map集合中没有该key对应的值,则直接添加,并返回null,如果已经存在对应的值,则依旧为原来的值。

    /**
     * Associates the specified value with the specified key in this map
     * (optional operation).  If the map previously contained a mapping for
     * the key, the old value is replaced by the specified value.  (A map
     * m is said to contain a mapping for a key k if and only
     * if {@link #containsKey(Object) m.containsKey(k)} would return
     * true.)
     *
     * @param key key with which the specified value is to be associated
     * @param value value to be associated with the specified key
     * @return the previous value associated with key, or
     *         null if there was no mapping for key.
     *         (A null return can also indicate that the map
     *         previously associated null with key,
     *         if the implementation supports null values.)
     * @throws UnsupportedOperationException if the put operation
     *         is not supported by this map
     * @throws ClassCastException if the class of the specified key or value
     *         prevents it from being stored in this map
     * @throws NullPointerException if the specified key or value is null
     *         and this map does not permit null keys or values
     * @throws IllegalArgumentException if some property of the specified key
     *         or value prevents it from being stored in this map
     */
    V put(K key, V value);

   
    /**
     * If the specified key is not already associated with a value (or is mapped
     * to {@code null}) associates it with the given value and returns
     * {@code null}, else returns the current value.
     *
     * @implSpec
     * The default implementation is equivalent to, for this {@code
     * map}:
     *
     * 
 {@code
     * V v = map.get(key);
     * if (v == null)
     *     v = map.put(key, value);
     *
     * return v;
     * }
* *

The default implementation makes no guarantees about synchronization * or atomicity properties of this method. Any implementation providing * atomicity guarantees must override this method and document its * concurrency properties. * * @param key key with which the specified value is to be associated * @param value value to be associated with the specified key * @return the previous value associated with the specified key, or * {@code null} if there was no mapping for the key. * (A {@code null} return can also indicate that the map * previously associated {@code null} with the key, * if the implementation supports null values.) * @throws UnsupportedOperationException if the {@code put} operation * is not supported by this map * (optional) * @throws ClassCastException if the key or value is of an inappropriate * type for this map * (optional) * @throws NullPointerException if the specified key or value is null, * and this map does not permit null keys or values * (optional) * @throws IllegalArgumentException if some property of the specified key * or value prevents it from being stored in this map * (optional) * @since 1.8 */ default V putIfAbsent(K key, V value) { V v = get(key); if (v == null) { v = put(key, value); } return v; }

example:

Map map = new HashMap<>();
System.out.println(map.put("1", "4"));
System.out.println(map.get("1"));

System.out.println(map.put("1", "5"));
System.out.println(map.get("1"));

System.out.println(map.putIfAbsent("1", "6"));
System.out.println(map.get("1"));

System.out.println(map.putIfAbsent("2","1"));
System.out.println(map.get("2"));

返回结果:

null
4
4
5
5
5
null
1

 

 

你可能感兴趣的:(map)