Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java Util Map replaceAll(BiFunction)

farenda 2016-07-13 0

java.util.Map.replaceAll(BiFunction)

The method Map.replaceAll(BiFunction) replaces each value with the result of applying the given function on the corresponding entry. Rethrows exceptions thrown by the replacement function.

Note that this default method does not make any guarantees about thread-safety. This is left to implementations and should be clearly documented by them.

Method signature

The signature of the java.util.Map.replaceAll(BiFunction) method is as follows:

default void replaceAll(
        BiFunction<? super K,? super V,? extends V> function)

The method is available since Java 8.

Parameters

  • BiConsumer<? super K, ? super V, ? extends V> action – action to be performed on each entry.

Return value

It replaces values in place and the method returns nothing.

Exceptions

  • ClassCastException: if the replacement class is of type not acceptable by this map
  • ConcurrentModificationException: when an entry is found to be removed during iteration.
  • IllegalArgumentException: when some property of the replacement value prevents it from being stored in this map (optional)
  • NullPointerException: when the given function or newValue is null and the map doesn’t allow null keys or values (optional)
  • UnsupportedOperationException: if the set operation is not supported by this map (optional)

Example usage

In the following code we use java.util.Map.replaceAll(BiFunction):

package com.farenda.java.util;

import java.util.HashMap;
import java.util.Map;

public class MapExamples {

    public static void main(String[] args) {
        Map<Integer,Integer> numbers = new HashMap<>();
        for (int i = 1; i <= 10; ++i) {
            numbers.put(i, i);
        }
        System.out.println("Original: " + numbers);

        numbers.replaceAll((key, oldValue) -> oldValue * oldValue);
        System.out.println("Squared: " + numbers);
    }
}

The above code produces the following output:

Original: {1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9, 10=10}
Squared: {1=1, 2=4, 3=9, 4=16, 5=25, 6=36, 7=49, 8=64, 9=81, 10=100}

References:

  • Compare with Map.forEach(BiConsumer), which is similar, but doesn’t replace
Share with the World!
Categories Java Tags java, java-util, java8
Previous: Spring Field Injection
Next: Java Util Optional.ofNullable(T)

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored