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 replace(key, oldValue, newValue)

farenda 2016-07-11 0

java.util.Map.replace(key, oldValue, newValue)

The method Map.replace(key, oldValue, newValue) replaces the value associated with the given key only if it is currently mapped to the oldValue.

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.replace(key, oldValue, newValue) method is as follows:

default boolean replace(K key, V oldValue, V newValue)

The method is available since Java 8.

Parameters

  • K key: key for which replace the value
  • V oldValue: value expected to be associated with the key
  • V newValue: value to associate with the key

Return value

Returns true when the value was replaced.

Exceptions

  • NullPointerException: when the given key or newValue is null and the map doesn’t allow null keys or values (optional)
  • UnsupportedOperationException: if the put operation is not supported by this map (optional)
  • ClassCastException: if the key or value is of not acceptable by this map (optional)
  • IllegalArgumentException: when some property of the given key or value prevents it from being stored in this map

Example usage

In the following code we use java.util.Map.replace(key, oldValue, newValue):

package com.farenda.java.util;

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

public class MapExamples {

    public static void main(String[] args) {
        Map<Integer,String> languages = new HashMap<>();
        languages.put(1, "Java");
        System.out.println("Original map: " + languages);

        boolean replaced = languages.replace(1, "Clojure", "Python");
        System.out.printf("Replaced: %b, the map: %s%n",
                replaced, languages);

        replaced = languages.replace(1, "Java", "Python");
        System.out.printf("Replaced: %b, the map: %s%n",
                replaced, languages);
    }
}

The above code produces the following output:

Original map: {1=Java}
Replaced: false, the map: {1=Java}
Replaced: true, the map: {1=Python}

References:

  • Compare with Java Util Map replace(key, value)
Share with the World!
Categories Java Tags java, java-util, java8
Previous: Java Util Map replace(key, value)
Next: Spring Field Injection

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