getOrDefault(Object, V): This method is available in java.util.Hashtable class of Java.
Syntax:
V java.util.Hashtable.getOrDefault(Object key, V 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.Hashtable;public class HashtablegetOrDefault {public static void main(String[] args) {Hashtable<String, Integer> hashtable =new Hashtable<String, Integer>();hashtable.put("Hello", 1);hashtable.put("World", 2);Object key = "Java";System.out.println(hashtable.getOrDefault(key, 10));}}
Output:
10
No comments:
Post a Comment