<?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 Amit Toor on Medium]]></title>
        <description><![CDATA[Stories by Amit Toor on Medium]]></description>
        <link>https://medium.com/@amittoor?source=rss-4c9fa91d494d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*qojJjzp1rmctAV6S</url>
            <title>Stories by Amit Toor on Medium</title>
            <link>https://medium.com/@amittoor?source=rss-4c9fa91d494d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 23:07:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@amittoor/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[Fixing Java 8 Optional]]></title>
            <link>https://medium.com/@amittoor/fixing-java-8-optional-f347231b0f4a?source=rss-4c9fa91d494d------2</link>
            <guid isPermaLink="false">https://medium.com/p/f347231b0f4a</guid>
            <category><![CDATA[fix]]></category>
            <category><![CDATA[java8]]></category>
            <category><![CDATA[optionals]]></category>
            <category><![CDATA[better-code]]></category>
            <category><![CDATA[lambda]]></category>
            <dc:creator><![CDATA[Amit Toor]]></dc:creator>
            <pubDate>Sat, 10 Oct 2020 12:37:04 GMT</pubDate>
            <atom:updated>2020-10-11T06:15:55.939Z</atom:updated>
            <content:encoded><![CDATA[<h3>Fixing Java 8 Optional: ifPresent with else</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YbJuXCJTovgNrS5JAzbxmw.jpeg" /></figure><p>Along with many other features, Java 8 came with lambda style of programming, which lets you write code in more concise and expressive way. One of the feature that I liked most about this new syntax was that it allowed you to chain multiple operations, using that you can do something like this:</p><pre>List&lt;String&gt; names<br>        = Arrays.<em>asList</em>(&quot;Amit Toor &quot;, &quot;Armaan Sindhu&quot;, &quot;Akashdeep Munjal&quot;, &quot;Amar Poonia&quot;,&quot;Amit Saini&quot;, &quot;Keanu Reeves&quot;);<br><br>names.stream()<br>        .filter(s -&gt; s.length()&gt;= 10)<br>        .map(s -&gt; s.toUpperCase())<br>        .sorted()<br>        .collect(Collectors.<em>toList</em>());</pre><p>You can see that in each subsequent operation, I have access to modified stream from previous step.</p><p>Other feature I really liked, which was part of lambda construct is <strong>Optionals, </strong>which allowed you to handle null values without writing if statements. Consider a use case where you can get null values. Using Optionals, you can do something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EQyoCBN4YAMg44qqhPXJ-g.png" /></figure><p>Cool, right?</p><p>So I had a use case where I needed to do null check on authorization header, So obviously, I decided use Optionals. So I created Optional of authorization header using following code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iV2zv_7wEV-XCMUl0oS2dw.png" /></figure><p>Now I needed to handle both cases:</p><ul><li>when header is null, throw exception.</li><li>and when it has some value, do something with value.</li></ul><p>That’s where I hit the wall. Java 8 optionals doesn’t provide you a way to handle both conditions in single statement. What I wanted was something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qAXnNPy-Urq-b8mS2uV8nQ.png" /></figure><p>Unfortunately, this doesn’t work. The return type of ifPresent method is void, so next statement won’t work.</p><p>My other option was to use isPresent() method with if else block (isPresent is different method than ifPresent, isPresent returns a boolean, Just clarification), but it beats the whole purpose of optionals. So I decided to something about it and I ended up writing a wrapper on java optionals. Find code for wrapper class below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H32MvatVSsLraesjXBp7xg.png" /></figure><p>Once wrapper is defined, we can write our code in the way we intended. Our code now becomes:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KLVzyUo69lo5vmdRZ4pTaQ.png" /></figure><p>Neat, right? I have implemented orElse and OrElseThrow method for Else logic. orElse takes Runnable as argument, which makes sense because if input is null then we don’t need method arguments anyway. You can also see that I have used inner class in wrapper. This inner class ensures that only valid methods are visible to user.</p><p>I have been following <a href="https://medium.com/@nmillard">Nicklas Millard</a> on medium, who has this awesome series, where he talks about how if else is bad for our code and teaches way using which we can avoid it. Please do check out if you are interested.</p><p>This is my first story on medium, please let me know if I can improve my writing in any way.</p><p>References: <a href="https://stackoverflow.com/questions/23773024/functional-style-of-java-8s-optional-ifpresent-and-if-not-present">https://stackoverflow.com/questions/23773024/functional-style-of-java-8s-optional-ifpresent-and-if-not-present</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f347231b0f4a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>