Methods an overview

If the value of the specified key exists and is not empty, a new mapping is attempted based on the given key and its current mapping value.

If the function returns NULL, the mapping is removed. If the function itself throws an (unchecked) exception, the exception is rethrown and the current mapping remains unchanged.

Request parameters

  • The value specified by key will be associated with it.
  • MappingFunction A function that evaluates a value.

The return value

The new value associated with the specified key; If not, null is returned.

Abnormal situation

  • Throws a NullPointerException if the specified key is empty and the mapping does not support empty keys, or if the mapping function is empty.
  • If the collection does not support the put operation, abnormal UnsupportedOperationException.
  • If the class of the specified key or value prevents it from being stored in this map, the ClassCastException exception is raised.

Source code analysis

    default V computeIfPresent(K key,
            BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
        // If the function that evaluates the value is null, a null-pointer exception is thrown
        Objects.requireNonNull(remappingFunction);
        V oldValue;
        // Get the value based on the specified key and assign it to oldValue
        if((oldValue = get(key)) ! =null) {
            // The old value exists, and the new value is computed using the specified key and the old value
            V newValue = remappingFunction.apply(key, oldValue);
            // Check whether the new value is null
            if(newValue ! =null) {
                // The new value is not empty, and the new value is added to the ma p collection
                put(key, newValue);
                // Return a new value
                return newValue;
            } else {
                // If the new value is null, delete the specified key
                remove(key);
                / / returns null
                return null; }}else {
            // Return null if the specified key cannot be retrieved
            return null; }}Copy the code

conclusion

  • The given key cannot fetch the old val in the collection, or if the old val is null, null is returned.
  • Compute a new val based on our custom evaluation function and the given key and the old val obtained by the given key.
  • If the new val is not null, the key and the new val are added to the collection. Otherwise the new val is null, delete the key and return NULL.

Contains test cases with specified keys

	@Test
	public void test_computeIfPresent(a){
		Map<String, Set<String>> dependentBeanMap = new ConcurrentHashMap<>(64);
		Set<String> s = new HashSet<>();
		String canonicalName = "m";
		s.add("mm");
		dependentBeanMap.put(canonicalName, s);
		// Check whether the val corresponding to this key exists. If there is a new val for this key, the return value is the new val after the calculation function
		Set<String> dependentBeans =
				dependentBeanMap.computeIfPresent(canonicalName, (k, v) -> {
					v.add("hahh");
					return v;
				});
		// Get the value of val for this element and iterate over it. This will print out that there are two values in the set of val for this key
		dependentBeanMap.get(canonicalName).forEach(System.out::println);
		System.out.println();
		// Set returns the value of val corresponding to key
		assertdependentBeans ! =null;
		dependentBeans.forEach(System.out::println);
	}
Copy the code

Test cases that do not contain the specified key

	/** * The map collection contains this key */
	@Test
	public void test_computeIfPresent_contains_no(a){
		Map<String, Set<String>> dependentBeanMap = new ConcurrentHashMap<>(64);
		String canonicalName = "m";
		// Check whether the corresponding val of this key exists. If it does, calculate a new val for this key according to the calculation function defined by us
		Set<String> dependentBeans =
				dependentBeanMap.computeIfPresent(canonicalName, (k, v) -> {
					v.add("hahh");
					return v;
				});
		// Check whether the set contains the key
		System.out.println(dependentBeanMap.containsKey(canonicalName));// false
		// NullPointerException is reported when executing this line
		Set<String> strings = dependentBeanMap.get(canonicalName);
		assertstrings ! =null;
		strings.forEach(System.out::println);
	}
Copy the code