Java 8 - Map getOrDefault method

Returns either the value of the specified Key in Map or the Default Value if the Key is not available.

Sample Code 1

Map<String,String> intMap = new HashMap<String,String>();
strMap.put("Key1","Value1");
strMap.put("Key2", "Value2");
System.out.println(strMap.getOrDefault("Key1", "Value5")); // prints Value1

Sample Code 2

Map<String,String> strMap = new HashMap<String,String>();
strMap.put("Key1","Value1");
strMap.put("Key2", "Value2");
System.out.println(strMap.getOrDefault("Key5", "Value5")); // prints Value5


Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner
comments powered by Disqus