Properties replace(Object, Object) in Java

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

Syntax:

Object java.util.Properties.replace(Object key, Object value)

This method takes two arguments. This method replaces the entry for the specified key only if it is currently mapped to some value.

Parameters: Two parameters are required for this method.

key: key with which the specified value is associated.

value: value to be associated with the specified key.

Returns: the previous value associated with the specified key, or null if there was no mapping for the key.

Exceptions: NA

Approach

Java

import java.util.Properties;

public class Propertiesreplace {
    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", value = "WORLD";

        properties.replace(key, value);

        System.out.println(properties);
    }
}

Output:

{Java=Program, C++=Program, Hello=WORLD}


No comments:

Post a Comment