Java 8 - Why Java8 doesn't provide the putIfPresent() function for Map

Because we already have the method replace that does the same thing


Map<String,String> strMap = new HashMap<String,String>();
strMap.put("Key2", "Value2");
strMap.replace("Key3", "Value3");

* Replace method doesn't throw the exception even if the element with specified Key is not already present in the Map and hence exactly replicate the behavior put If Present.

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