<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Chaitanya Chunduri</title>
    <description>The latest articles on DEV Community by Chaitanya Chunduri (@cchunduri).</description>
    <link>https://dev.to/cchunduri</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F37792%2F8b464e20-6fd1-48ca-b038-bda3ed9e07dc.jpeg</url>
      <title>DEV Community: Chaitanya Chunduri</title>
      <link>https://dev.to/cchunduri</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cchunduri"/>
    <language>en</language>
    <item>
      <title>About Spring Boot Native Images</title>
      <dc:creator>Chaitanya Chunduri</dc:creator>
      <pubDate>Sat, 29 Oct 2022 15:26:28 +0000</pubDate>
      <link>https://dev.to/cchunduri/spring-boot-native-images-4mfm</link>
      <guid>https://dev.to/cchunduri/spring-boot-native-images-4mfm</guid>
      <description>&lt;p&gt;I was recently impressed by Spring Boot's native images feature. I considered sharing some information about native images.&lt;/p&gt;

&lt;p&gt;I created two simple Spring Boot projects&lt;br&gt;
1) spring-with-native-plugin&lt;br&gt;
2) spring-without-native-plugin&lt;/p&gt;

&lt;p&gt;Apart from the Main class, I used the same controller in both projects.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yVZ5MgFx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mdiliu6koh6qpqdbt8er.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yVZ5MgFx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mdiliu6koh6qpqdbt8er.png" alt="Hello World Controller" width="880" height="455"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I used the following command to create docker images.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;mvn spring-boot:build-image 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(Both projects are based on maven)&lt;/p&gt;

&lt;p&gt;When I ran both docker images, I was surprised by the application startup time.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pB6mrYIH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9cn3yicwk6cru6rx0kqs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pB6mrYIH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9cn3yicwk6cru6rx0kqs.png" alt="Spring-Without-native" width="880" height="56"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WUPvHmlU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/emb7o2ks243xvem3wept.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WUPvHmlU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/emb7o2ks243xvem3wept.png" alt="Spring-with-native" width="880" height="71"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There's also a significant difference in the sizes of the two Docker images.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YU4MNBAg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t3ksxmmsmvojq5huxgy7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YU4MNBAg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t3ksxmmsmvojq5huxgy7.png" alt="memory-deference" width="880" height="44"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, What happened after we installed the native plugin?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Thanks to Spring AOT Process, it performed a code analysis to identify unnecessary code.&lt;/li&gt;
&lt;li&gt;After the code analysis, all unnecessary code was removed, and a native image was created.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, This native image is &lt;strong&gt;no longer dependent on the JVM&lt;/strong&gt; and can run without the JVM.&lt;/p&gt;

&lt;p&gt;We must thank @graalvmfor making this all possible.&lt;/p&gt;

</description>
      <category>java</category>
      <category>native</category>
      <category>spirngboot</category>
      <category>docker</category>
    </item>
    <item>
      <title>Handle Null like a pro using Optional </title>
      <dc:creator>Chaitanya Chunduri</dc:creator>
      <pubDate>Mon, 16 Aug 2021 08:19:09 +0000</pubDate>
      <link>https://dev.to/cchunduri/handle-null-like-a-pro-using-optional-5aff</link>
      <guid>https://dev.to/cchunduri/handle-null-like-a-pro-using-optional-5aff</guid>
      <description>&lt;p&gt;Many times we think that our code is perfect, works like a charm in the given scenarios and we proceed for deployment in production on a fine Friday. But when the code hits production due to unexpected data or due to some failure, when the code throws &lt;strong&gt;NullPointerExcpetion&lt;/strong&gt;, the feeling comes like damn this weekend also ha... &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/t9ctG5MZhyyU8/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/t9ctG5MZhyyU8/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But Thank God, we have a rescuer to fight with &lt;strong&gt;NPE(NullPointerException)&lt;/strong&gt; who came into the existence since Java 8 which is Optional class. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/mQeafe0Aev1PW/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/mQeafe0Aev1PW/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this article let us find out how we can use Optional in day to day jobs.&lt;/p&gt;

&lt;p&gt;To create Optional object we have two methods,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;using of method&lt;/li&gt;
&lt;li&gt;using ofNullable&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here are the examples&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;optional&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;of&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Chaitanya"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;opNullable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ofNullable&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Chaitanya"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you think that there is a possibility of null coming because of the calling function then you can use &lt;strong&gt;ofNullable&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;of&lt;/strong&gt; method throws NPE if the value is null, so, if you're certain that you don't get a null value you can use &lt;strong&gt;of&lt;/strong&gt; method. &lt;/p&gt;

&lt;p&gt;IMO, for simple operations like variable initialization or you've simple values and you want to pass them then you can use &lt;strong&gt;of&lt;/strong&gt; method, if you're calling some external function for the result then you can use &lt;strong&gt;ofNullable&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Optional is something like a container that holds a value. To get things out of the container we need to open it, same way to get the value out of the &lt;strong&gt;Optional&lt;/strong&gt;, we've &lt;strong&gt;get&lt;/strong&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;optional&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;of&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Chaitanya"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;optional&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Think that you got a big container, you opened it and found nothing, then you feel disappointed right? To save from these kinds of bad feelings, we have &lt;strong&gt;isPresent&lt;/strong&gt; method, usage is like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;nameOptional&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;getFirstName&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nameOptional&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isPresent&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nameOptional&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"No Name :( "&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to handle in a functional style, here is the way&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;nameOptional&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ifPresent&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or using method references&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;nameOptional&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ifPresent&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;println&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sometimes we don't want to do anything when an exception raises, without &lt;strong&gt;Optional&lt;/strong&gt; one way is we return null in catch block, but from &lt;strong&gt;Optional&lt;/strong&gt; we've one more method &lt;strong&gt;empty()&lt;/strong&gt;, here is the usage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ofNullable&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;getEmpName&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;empty&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sometimes we may need to manipulate the results of certain operations, in such cases, we've &lt;strong&gt;map&lt;/strong&gt; and &lt;strong&gt;flatMap&lt;/strong&gt; methods&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;nameInUpperCaseOptional&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;                            
                       &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ofNullable&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;getEmpName&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
                          &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;String:&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;toUpperCase&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                          &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here map returns one more Optional object with the transformed value. If you want to get direct value instead of Optional object, then you can use &lt;strong&gt;flatMap&lt;/strong&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;nameInUpperCase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ofNullable&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;getEmpName&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
                          &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;flatMap&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;String:&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;toUpperCase&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to return a default value when there is no value present, you can use &lt;strong&gt;orElse&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="no"&gt;EMPTY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ofNullable&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;getEmpName&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
                  &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;orElse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;EMPTY&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to perform some action when there is no value present, you can use &lt;strong&gt;orElseGet&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ofNullable&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;getEmpName&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
                  &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;orElseGet&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"There is no Value :( "&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to throw an exception when there is no value present then we've &lt;strong&gt;orElseThrow&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ofNullable&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;getEmpName&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
                  &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;orElseThrow&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;Exception:&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Java 11, There are some more enhancements in Optional class like &lt;strong&gt;ifPresentElse&lt;/strong&gt;, &lt;strong&gt;or&lt;/strong&gt;, &lt;strong&gt;stream&lt;/strong&gt;, here is the link to documentation for further details. &lt;a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Optional.html"&gt;Java 11#Optional&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That is it for this article :) If you use &lt;strong&gt;Optional&lt;/strong&gt; properly you can pretty much avoid &lt;strong&gt;NPE&lt;/strong&gt; and can avoid stressful weekends too.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/kiBcwEXegBTACmVOnE/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/kiBcwEXegBTACmVOnE/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for spending time. I appreciate your feedback.&lt;/p&gt;

</description>
      <category>java</category>
      <category>backend</category>
      <category>webdev</category>
      <category>functional</category>
    </item>
    <item>
      <title>Developing Fullstack Web Application Using Spring Boot, Kotlin and React JS</title>
      <dc:creator>Chaitanya Chunduri</dc:creator>
      <pubDate>Sun, 24 May 2020 19:15:24 +0000</pubDate>
      <link>https://dev.to/cchunduri/developing-fullstack-web-application-using-spring-boot-kotlin-and-react-js-89d</link>
      <guid>https://dev.to/cchunduri/developing-fullstack-web-application-using-spring-boot-kotlin-and-react-js-89d</guid>
      <description>&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/rxb0IrNH5zs"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>springboot</category>
      <category>react</category>
      <category>webapplications</category>
    </item>
    <item>
      <title>How to Build web applications using Kotlin/JS and React JS </title>
      <dc:creator>Chaitanya Chunduri</dc:creator>
      <pubDate>Wed, 22 Apr 2020 07:19:36 +0000</pubDate>
      <link>https://dev.to/cchunduri/how-to-build-web-applications-using-kotlin-js-and-react-js-2mc1</link>
      <guid>https://dev.to/cchunduri/how-to-build-web-applications-using-kotlin-js-and-react-js-2mc1</guid>
      <description>&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/FjZ5UCnVNpk"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>ES 6: Using Object.entries() to iterate the keys in the Object</title>
      <dc:creator>Chaitanya Chunduri</dc:creator>
      <pubDate>Thu, 25 Jul 2019 01:11:02 +0000</pubDate>
      <link>https://dev.to/cchunduri/es-6-using-object-entries-to-iterate-the-keys-in-the-object-53pd</link>
      <guid>https://dev.to/cchunduri/es-6-using-object-entries-to-iterate-the-keys-in-the-object-53pd</guid>
      <description>&lt;p&gt;You can iterate keys in an object using ES 6 like below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let employe = {
 name: 'Chaitanya',
 eno: '1'
};

for (let [key, value] of Object.entries(employe)) {
  console.log(`${key}: ${value}`);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or there is another way using forEach method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Object.entries(employe).forEach(([key, value]) =&amp;gt; {
  console.log(`${key}: ${value}`); 
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Object.enttries() got standardised in ECMAScript2017. Below is the browser support chart collected from MDN&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fmen2md5m46u0ye0pphzl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fmen2md5m46u0ye0pphzl.png"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>es6</category>
    </item>
  </channel>
</rss>
