Properties putIfAbsent(Object, Object) in Java

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

Syntax:

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

This method takes two arguments. If the specified key is not already associated with a value associates it with the given value and returns null, else returns the current value.

Parameters: Two parameters are required for this method.

key: key with which the specified value is to be 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 PropertiesputIfAbsent {
    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";
        System.out.println(properties.putIfAbsent(key,
value));

    }
}

Output:

World


No comments:

Post a Comment