Properties replace(Object, Object, Object) in Java

replace(Object, Object, Object): This method is available in java.util.Properties class of Java.

Syntax:

boolean java.util.Properties.replace(Object key, Object oldValue, Object newValue)

This method takes three arguments. This method replaces the entry for the specified key only if currently mapped to the specified value.

Parameters: Three parameters are required for this method.

key: key with which the specified value is associated.

oldValue: value expected to be associated with the specified key.

newValue: value to be associated with the specified key.

Returns: true if the value was replaced.

Exceptions: NA

Approach

Java

import java.util.Properties;

public class Propertiesreplace2 {
    public static void main(String[] args) {

        Properties properties = new Properties();

        properties.put("Hello", "World");
        properties.put("C++", "Program");
        properties.put("Java", "Program");

        Object key = "Hello", oldValue =
"World", newValue = "WORLD";

        System.out.println(properties.replace(key,
oldValue, newValue));
    }
}

Output:

true


No comments:

Post a Comment