Properties getOrDefault(Object, Object) in Java

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

Syntax:

Object java.util.Properties.getOrDefault(Object key, Object defaultValue)

This method takes two arguments. This method returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.

Parameters: Two parameters are required for this method.

key: the key whose associated value is to be returned.

defaultValue: the default mapping of the key.

Returns: the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.

Exceptions: NA

Approach

Java

import java.util.Properties;

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

        System.out.println(properties.getOrDefault(key,
defaultValue));
    }
}

Output:

WORLD


No comments:

Post a Comment