<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Muthusankar B on Medium]]></title>
        <description><![CDATA[Stories by Muthusankar B on Medium]]></description>
        <link>https://medium.com/@muthusankar54?source=rss-4df677407af7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*1v86lp2JF5wWLsho.</url>
            <title>Stories by Muthusankar B on Medium</title>
            <link>https://medium.com/@muthusankar54?source=rss-4df677407af7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 09:08:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@muthusankar54/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Java.util.HashMap : get() Vs. getOrDefault()]]></title>
            <link>https://medium.com/@muthusankar54/java-util-hashmap-get-vs-getordefault-c06ec603cdee?source=rss-4df677407af7------2</link>
            <guid isPermaLink="false">https://medium.com/p/c06ec603cdee</guid>
            <category><![CDATA[hashmap]]></category>
            <category><![CDATA[getordefault]]></category>
            <category><![CDATA[java8]]></category>
            <dc:creator><![CDATA[Muthusankar B]]></dc:creator>
            <pubDate>Sun, 03 May 2020 22:57:46 GMT</pubDate>
            <atom:updated>2020-05-03T22:57:46.764Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Java.util.HashMap : get() Vs. getOrDefault()</strong></h3><p>The method <strong>Map.getOrDefault(key, defaultValue)</strong> returns mapped value for the given key or <strong>defaultValue</strong> if there is no such mapping.</p><p>The <strong>getOrDefault(Object key, V defaultValue)</strong> method of Map Interface, implemented by HashMap class is used to get the value mapped with specified key. If no value is mapped with the provided key then the default value is returned.</p><p><strong>Syntax:</strong></p><p><strong>Default V getOrDefault (Object key, V value);</strong></p><p><strong>Parameters:</strong> This method accepts two parameters:</p><ul><li><strong>key:</strong> which is the <strong>key</strong> of the element whose value has to be obtained.</li><li><strong>defaultValue:</strong> which is the <strong>default value</strong> that has to be returned, if no value is mapped with the specified key.</li></ul><p><strong>How Can We Retrieve a Value in HashMap?</strong></p><p><strong>Using the get() Method of HashMap</strong></p><p>In Hashmap, <a href="https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#get-java.lang.Object-">get(Object key) </a>method is used to retrieve the value of a key. So, if we have a key then we easily fetch the value from the hashmap using the get method. Please follow the below snapshot for same:</p><blockquote>Map&lt;String, Integer&gt; map = new HashMap&lt;&gt;();</blockquote><blockquote>map.put(“1”, 500);</blockquote><blockquote>map.put(“2”, 1000);</blockquote><blockquote>// get the value of key 1.</blockquote><blockquote>Integer value = map.get(“1”);</blockquote><blockquote>// check if value is null, then set default value to 0.</blockquote><blockquote>if(null == value){</blockquote><blockquote>price = 0;</blockquote><blockquote>}</blockquote><blockquote>// print the value of map.</blockquote><blockquote>System.out.println(“value of 1 is : “ +value);</blockquote><p>In the above example, we set the default value to 0 if the value cannot be found for the map. Through this, we can easily avoid the null value if the key cannot be found with a default value. But for this, we have to put an additional null check for each value in the product map.</p><p>So, do we have any other way to set the default value for non-existing keys?</p><p>In fact, we do.</p><h3>Using the getOrDefault() Method in HashMap</h3><p>Let see how can we override the above approach without adding a null check. One new out-of-the-box method of Map that was introduced in Java8 will help here. We will provide a default value in that method itself, so if key doesn’t exist, then the default value will be returned instead of null. Check out the solution below:</p><blockquote>Map&lt;String, Integer&gt; map = new HashMap&lt;&gt;();</blockquote><blockquote>map.put(“1”, 500);</blockquote><blockquote>map.put(“2”, 1000);</blockquote><blockquote>// get value of key 1.</blockquote><blockquote>Integer value = map.getOrDefault(“1”, “0”);</blockquote><blockquote>// print the value of map.</blockquote><blockquote>System.out.println(“value of 1 is : “ + value);</blockquote><p><strong>References:</strong> <a href="https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#getOrDefault-java.lang.Object-V-">https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#getOrDefault-java.lang.Object-V-</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c06ec603cdee" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>