<?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 Java Spring Decoded on Medium]]></title>
        <description><![CDATA[Stories by Java Spring Decoded on Medium]]></description>
        <link>https://medium.com/@java-Spring-Decoded?source=rss-943a61b631c6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*8QpJ-VrX5o3TrzE_fMnSqg.png</url>
            <title>Stories by Java Spring Decoded on Medium</title>
            <link>https://medium.com/@java-Spring-Decoded?source=rss-943a61b631c6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 20:36:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@java-Spring-Decoded/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[Generating Unique Random Number Upto A Limit In Java]]></title>
            <link>https://medium.com/javarevisited/generating-unique-random-number-upto-a-limit-in-java-1c8967eb90be?source=rss-943a61b631c6------2</link>
            <guid isPermaLink="false">https://medium.com/p/1c8967eb90be</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Java Spring Decoded]]></dc:creator>
            <pubDate>Mon, 03 Jul 2023 17:32:12 GMT</pubDate>
            <atom:updated>2023-07-06T15:54:38.701Z</atom:updated>
            <content:encoded><![CDATA[<p>Here we will try to figure out a way to generate unique random number within a upper limit .</p><p>The problem with general random number generation is there is a chance of collision but we want to return unique random numbers each time a client call us , this is one of the solution which is easy to understand .</p><pre>import java.util.*;<br><br>public class GenerateRandom {<br>    public static int MAX = 100;<br><br>    Set&lt;Integer&gt; numberSet = new HashSet&lt;&gt;();<br><br><br><br><br>    public Integer generateRandomNumber(){<br>        Random random = new Random();<br>        if(numberSet.size() == 100){<br>            return -1;<br>        }<br>        else{<br>            int temp = random.nextInt(MAX) ;<br>            while(numberSet.contains(temp)){<br>                temp = random.nextInt(MAX);<br>            }<br>            numberSet.add(temp);<br>            return temp;<br>        }<br>    }<br>}</pre><p>here , we are maintaining a hash set of the numbers we have generated and checking each time for the generated number if it is present in the set or not , and we keep on doing it till we get the unique number .</p><p>here , we have implemented with a upper limit of 100 .</p><p>P.S :- There is a drawback to this method as there will be less numbers left , there will be more collisions , hence high response time .</p><p>I will update with a better algorithm to do so.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1c8967eb90be" width="1" height="1" alt=""><hr><p><a href="https://medium.com/javarevisited/generating-unique-random-number-upto-a-limit-in-java-1c8967eb90be">Generating Unique Random Number Upto A Limit In Java</a> was originally published in <a href="https://medium.com/javarevisited">Javarevisited</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Here we are trying to print odd and even numbers in two seperate threads sequentially using the…]]></title>
            <link>https://medium.com/javarevisited/here-we-are-trying-to-print-odd-and-even-numbers-in-two-seperate-threads-sequentially-using-the-d950948e65f?source=rss-943a61b631c6------2</link>
            <guid isPermaLink="false">https://medium.com/p/d950948e65f</guid>
            <category><![CDATA[multithreading]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Java Spring Decoded]]></dc:creator>
            <pubDate>Sat, 01 Jul 2023 18:15:51 GMT</pubDate>
            <atom:updated>2023-07-02T14:42:01.812Z</atom:updated>
            <content:encoded><![CDATA[<h3>Java <strong>program</strong> to print odd and even number in two seperate threads.</h3><p>Here we are trying to <a href="https://javarevisited.blogspot.com/2013/04/how-to-check-if-number-is-even-or-odd.html">print odd and even numbers</a> in two seperate threads sequentially using the concepts of multithreading in java.</p><p>First we will create two methods , one to print odd numbers and one to print even numbers till we reach the last number .</p><p>In both the methods we will use the <a href="https://javarevisited.blogspot.com/2017/02/10-java-wait-notify-locking-and-synchronization-Interview-Questions-Answers.html">object level lock</a> to awake the waiting threads to notify the other thread.</p><p>First when we enter the first method , we will check if the number is odd or even , basis on which we will print the number and increment the counter so one thread will always be waiting and one thread will be awaken by the other thread once it prints the corresponding odd/even number .</p><p>Since , we have taken the lock on the object , <a href="https://javarevisited.blogspot.com/2015/07/how-to-use-wait-notify-and-notifyall-in.html">all the threads waiting on that object lock will be notified </a>and hence the other thread will start executing once it gets notified by the other thread .</p><pre>public class MyClass{<br> <br>    int count = 1;<br> <br>    static int N;<br> <br>    public void printOddNumber()<br>    {<br>        synchronized (this)<br>        {<br>            while (count &lt; N) {<br> <br>             <br>                while (count % 2 == 0) {<br>                    try {<br>                        wait();<br>                    }<br>                    catch (InterruptedException e) {<br>                        e.printStackTrace();<br>                    }<br>                }<br>                System.out.print(count + &quot; &quot;);<br>                count++;<br>                notify();<br>            }<br>        }<br>    }<br><br>    public void printEvenNumber()<br>    {<br>        synchronized (this)<br>        {<br>            while (count &lt; N) {<br>                while (count % 2 == 1) {<br>                    try {<br>                        wait();<br>                    }<br>                    catch (InterruptedException e) {<br>                        e.printStackTrace();<br>                    }<br>                }<br>                System.out.print(count + &quot; &quot;);<br>                count++;<br>                notify();<br>            }<br>        }<br>    }<br>    public static void main(String[] args)<br>    {<br>        N = 10;<br>        MyClass myClass = new MyClass();<br>        Thread t1 = new Thread(new Runnable() {<br>            public void run()<br>            {<br>                myClass.printEvenNumber();<br>            }<br>        });<br>        Thread t2 = new Thread(new Runnable() {<br>            public void run()<br>            {<br>                myClass.printOddNumber();<br>            }<br>        });<br>        t1.start();<br>        t2.start();<br>    }<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d950948e65f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/javarevisited/here-we-are-trying-to-print-odd-and-even-numbers-in-two-seperate-threads-sequentially-using-the-d950948e65f">Here we are trying to print odd and even numbers in two seperate threads sequentially using the…</a> was originally published in <a href="https://medium.com/javarevisited">Javarevisited</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[String vs StringBuilder Explained]]></title>
            <link>https://medium.com/javarevisited/string-vs-stringbuilder-explained-82f7bdad5bd2?source=rss-943a61b631c6------2</link>
            <guid isPermaLink="false">https://medium.com/p/82f7bdad5bd2</guid>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[java8]]></category>
            <dc:creator><![CDATA[Java Spring Decoded]]></dc:creator>
            <pubDate>Wed, 12 Apr 2023 13:59:19 GMT</pubDate>
            <atom:updated>2023-06-23T15:33:52.602Z</atom:updated>
            <content:encoded><![CDATA[<p>Lets see this through a very interesting example :-</p><p>Suppose we have a string and a StringBuilder as given below :-</p><p>When i append the string with the “new” string , the string reference “s” ,</p><p>still points to the original string , while the StringBuilder reference sb is showing the modification done in the magic method why is such case ??</p><h3><strong>1.Because Strings are Immutable</strong> .</h3><p>what does that mean?</p><p>Once a String Object is being in heap , we can’t change the state of the string object , there will be only one state of a String Object .<br>Its state can’t mutate.</p><p>On the Other Hand , For StringBuilder :-</p><p>It can mutate its object state as many times as it wants , it will be happening on the Same Object .</p><pre><br>        String s = &quot;I am a String &quot;;<br>        StringBuilder sb = new StringBuilder(&quot;I am a StringBuilder &quot;);<br>        System.out.println(s);<br>        System.out.println(sb);<br>        magic(s,sb);<br>        System.out.println(s);<br>        System.out.println(sb);</pre><pre>public static void magic(String s, StringBuilder sb){<br>        s =  s + &quot;new&quot; ;<br>        sb = sb.append(&quot;new&quot;);<br>    }</pre><pre>I am a String<br>I am a StringBuilder <br>I am a String<br>I am a StringBuilder new</pre><p>In this cases , when we wrote s = s + “new” , Here String will create a new String Object and s will be pointing to that object in the method .</p><h3><strong>2. Strings Override the equals Method.</strong></h3><pre>class Main<br>{<br>    private static final String STR = &quot;COMMON&quot;;<br> <br>    public static void main(String[] args)<br>    {<br>        String s1 = new String(STR);<br>        String s2 = new String(STR);<br>        System.out.println(s1.equals(s2));<br> <br>        StringBuilder sb1 = new StringBuilder(STR);<br>        StringBuilder sb2 = new StringBuilder(STR);<br>        System.out.println(sb1.equals(sb2));<br>    }<br>}</pre><p>Output 1st -&gt; true</p><p>Output 2nd -&gt; false</p><p>Here , StringBuilder doesn’t have the implementation of equals method , so it uses the Objects Equals Method , which basically compares the memory reference , which is not same as two different StringBuilder objects are created .</p><h3><strong>3.Strings implements the comparable</strong></h3><p>The String class implements the Comparable interface, while StringBuilder doesn’t. To illustrate, consider the following code, which throws ClassCastException since StringBuilder doesn’t provide any mechanism for string comparison.</p><pre>import java.util.Set;<br>import java.util.TreeSet;<br> <br>class Main<br>{<br>    public static void main(String[] args)<br>    {<br>        Set&lt;String&gt; colors = new TreeSet&lt;&gt;();<br>        colors.add(new String(&quot;Red&quot;));<br>        colors.add(new String(&quot;Blue&quot;));<br>        colors.add(new String(&quot;green&quot;));<br> <br>        Set&lt;StringBuilder&gt; codes = new TreeSet&lt;&gt;();<br> <br>        try {<br>            codes.add(new StringBuilder(&quot;#008200&quot;));<br>            codes.add(new StringBuilder(&quot;#ff0000&quot;));<br>            codes.add(new StringBuilder(&quot;#006400&quot;));<br>        }<br>        catch (ClassCastException ex) {<br>            System.out.println(ex.getMessage());<br>        }<br>    }<br>}</pre><p>OUTPUT :- java.lang.StringBuilder cannot be cast to java.lang.Comparable</p><h3>5. Performance</h3><p>StringBuilder is speedy and consumes less memory than a string while performing concatenations. This is because string is immutable in Java, and concatenation of two string objects involves creating a new object.</p><p>To illustrate, consider the following code, which logs the time taken by both string and StringBuilder objects on multiple concatenations.</p><pre>class Main<br>{<br>    private static final String STR = &quot;TEST&quot;;<br> <br>    public static String stringConcatenation()<br>    {<br>        String s = STR;<br>        for (int i = 0; i &lt; Short.MAX_VALUE; i++) {<br>            s = s + STR;<br>        }<br>        return s;<br>    }<br> <br>    public static String stringBuilderConcatenation()<br>    {<br>        StringBuilder sb = new StringBuilder(STR);<br>        for (int i = 0; i &lt; Short.MAX_VALUE; i++) {<br>            sb.append(STR);<br>        }<br>        return sb.toString();<br>    }<br> <br>    public static void main(String[] args)<br>    {<br>        long start = System.nanoTime();<br>        stringConcatenation();<br>        long end = System.nanoTime();<br> <br>        System.out.println(&quot;The time taken by string concatenation: &quot;<br>                        + (end - start) + &quot;ns&quot;);<br> <br>        start = System.nanoTime();<br>        stringBuilderConcatenation();<br>        end = System.nanoTime();<br> <br>        System.out.println(&quot;The time taken by StringBuilder concatenation: &quot;<br>                        + (end - start) + &quot;ns&quot;);<br>    }<br>}</pre><p>The time taken by string concatenation: 504774386ns<br>The time taken by StringBuilder concatenation: 1081315ns</p><h3><strong>CONCLUSION</strong></h3><p>— — — — — — — — — —</p><p>A String can be used when immutability is required, or concatenation operation is not required. A StringBuilder can be used when a mutable string is needed without the performance overhead of constructing lots of strings along the way.</p><p>Final Keyword Explained Here :-</p><p><a href="https://medium.com/@java-Spring-Decoded/final-keyword-in-java-c197c6a28345">https://medium.com/@java-Spring-Decoded/final-keyword-in-java-c197c6a28345</a></p><p>MultiThreaded Singelton Explained Here :-</p><p><a href="https://medium.com/@java-Spring-Decoded/multithreaded-singleton-java-class-f3b7d633752b">https://medium.com/@java-Spring-Decoded/multithreaded-singleton-java-class-f3b7d633752b</a></p><p>Immutable Classes Explained Here :-</p><p><a href="https://medium.com/@java-Spring-Decoded/immutable-classes-in-java-6268ff84bdd">Immutable Classes In Java</a></p><p>Equals Method Explained Here :-</p><p><a href="https://medium.com/@java-Spring-Decoded/equals-method-in-java-34ff82f1180">https://medium.com/@java-Spring-Decoded/equals-method-in-java-34ff82f1180</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=82f7bdad5bd2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/javarevisited/string-vs-stringbuilder-explained-82f7bdad5bd2">String vs StringBuilder Explained</a> was originally published in <a href="https://medium.com/javarevisited">Javarevisited</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[System.out.println() Explained]]></title>
            <link>https://medium.com/javarevisited/system-out-println-explained-d1c59229d664?source=rss-943a61b631c6------2</link>
            <guid isPermaLink="false">https://medium.com/p/d1c59229d664</guid>
            <category><![CDATA[java8]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[object-oriented]]></category>
            <dc:creator><![CDATA[Java Spring Decoded]]></dc:creator>
            <pubDate>Wed, 12 Apr 2023 06:41:03 GMT</pubDate>
            <atom:updated>2023-06-23T15:36:01.375Z</atom:updated>
            <content:encoded><![CDATA[<p>System — Concrete Class</p><p>Out — Static Reference of Print Stream Class Present in System Class that’s why it is accessible via Class reference.</p><p>println — It is a public method present in PrintStream class , which is responsible for printing output , we can override this method also by extending the PrintStream Class.</p><p><strong>Performance Analysis of System.out.println()</strong></p><p><strong>println()</strong> is a method that helps display output on a console. This might be dependent on various factors that drives the performance of this method. The message passed using println() is passed to the server’s console where <strong>kernel time</strong> is required to execute the task. Kernel time refers to the <strong>CPU time</strong>. Since println() is a synchronized method, so when multiple threads are passed could lead to the <strong>low-performance issue</strong>. System.out.println() is a <strong>slow operation</strong> as it incurs heavy overhead on the machine compared to most IO operations.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d1c59229d664" width="1" height="1" alt=""><hr><p><a href="https://medium.com/javarevisited/system-out-println-explained-d1c59229d664">System.out.println() Explained</a> was originally published in <a href="https://medium.com/javarevisited">Javarevisited</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Comparator Vs Comparable In Java]]></title>
            <link>https://medium.com/javarevisited/comparator-vs-comparable-in-java-4568a9fda62f?source=rss-943a61b631c6------2</link>
            <guid isPermaLink="false">https://medium.com/p/4568a9fda62f</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[collection]]></category>
            <category><![CDATA[object-oriented]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[java8]]></category>
            <dc:creator><![CDATA[Java Spring Decoded]]></dc:creator>
            <pubDate>Tue, 11 Apr 2023 10:42:21 GMT</pubDate>
            <atom:updated>2023-06-29T07:24:12.590Z</atom:updated>
            <content:encoded><![CDATA[<p>As a Java Developer we always come Across these two Interfaces when we need to sort any custom Object ,</p><p>lets discuss its use cases:-</p><h3><strong>COMPARABLE INTERFACE</strong></h3><ol><li>Comparable Interface provides us compareTo method to be overriden , It becomes the property of a class and their can exist only one comparable overriding for Class .</li><li>We can only use Comparable Interface , when the class is our internal class and we want to define a definite natural ordering for it.</li></ol><pre>public class Comparing implements Comparable&lt;Comparing&gt; {<br>    private int x;<br>    private String y;<br><br>    public int getX() {<br>        return x;<br>    }<br><br>    public void setX(int x) {<br>        this.x = x;<br>    }<br><br>    public String getY() {<br>        return y;<br>    }<br><br>    public void setY(String y) {<br>        this.y = y;<br>    }<br><br>    public Comparing(int x, String y) {<br>        this.x = x;<br>        this.y = y;<br>    }<br><br>    @Override<br>    public String toString() {<br>        return &quot;Comparing{&quot; +<br>                &quot;x=&quot; + x +<br>                &quot;, y=&#39;&quot; + y + &#39;\&#39;&#39; +<br>                &#39;}&#39;;<br>    }<br><br>    @Override<br>    public int compareTo(Comparing o) {<br>        if(this.getX() == o.getX()){<br>            return this.getY().compareTo(o.getY());<br>        }<br>        else {<br>            return this.getX() &gt; o.getX() ? 1 : -1 ;<br>        }<br>    }<br>}</pre><p>Here In this example we can see , we have overriden the compareTo method in our custom Class Comparing .</p><pre>Comparing comparing1 = new Comparing(1,&quot;a&quot;);<br>Comparing comparing2 = new Comparing(2,&quot;c&quot;);<br>Comparing comparing3 = new Comparing(4,&quot;b&quot;);<br><br>List&lt;Comparing&gt; list = new ArrayList&lt;&gt;();<br>list.add(comparing1);<br>list.add(comparing2);<br>list.add(comparing3);<br>Collections.sort(list);</pre><p><strong>Note :- If we dont override the compareTo method in our class , Collections.sort(list) will give compile time error .</strong></p><h3><strong>COMPARATOR INTERFACE</strong></h3><p>Suppose the Class Objects we want to sort is an external class , In that case we dont have control over source code of that class , how can we sort the list in such cases .</p><p>Suppose we want to have multiple sorting logic for a class , which can be used in different scenarios .</p><p>These two cases are not addressed by Comparable Interface , Here Comparator Interface come to rescue .</p><ol><li>Comparator interface provides a compare method , which we can override to create multiple sorting logic for a class.</li><li>It doesn’t required a change in source code in client class which we want to sort.</li></ol><pre>public class Comparing  {<br>    private int x;<br>    private String y;<br><br>    public int getX() {<br>        return x;<br>    }<br><br>    public void setX(int x) {<br>        this.x = x;<br>    }<br><br>    public String getY() {<br>        return y;<br>    }<br><br>    public void setY(String y) {<br>        this.y = y;<br>    }<br><br>    public Comparing(int x, String y) {<br>        this.x = x;<br>        this.y = y;<br>    }<br><br>    @Override<br>    public String toString() {<br>        return &quot;Comparing{&quot; +<br>                &quot;x=&quot; + x +<br>                &quot;, y=&#39;&quot; + y + &#39;\&#39;&#39; +<br>                &#39;}&#39;;<br>    }<br>}</pre><pre>        Comparing comparing1 = new Comparing(1,&quot;a&quot;);<br>        Comparing comparing2 = new Comparing(2,&quot;c&quot;);<br>        Comparing comparing3 = new Comparing(3,&quot;b&quot;);<br><br>        List&lt;Comparing&gt; list = new ArrayList&lt;&gt;();<br>        list.add(comparing1);<br>        list.add(comparing2);<br>        list.add(comparing3);<br>        Collections.sort(list,Comparator.comparing(Comparing::getX));<br>        Collections.sort(list,Comparator.comparing(Comparing::getY));<br>  </pre><p>Here , We can see we can create any comparator and pass it to the Collections.sort(list, customComparator) and we have a new sorting logic.</p><p>Related Articles: -<br><a href="https://medium.com/@javabackend175/final-keyword-in-java-c197c6a28345">https://medium.com/@javabackend175/final-keyword-in-java-c197c6a28345</a><br><a href="https://medium.com/@javabackend175/multithreaded-singleton-java-class-f3b7d633752b">https://medium.com/@javabackend175/multithreaded-singleton-java-class-f3b7d633752b</a></p><p><a href="https://medium.com/@javabackend175/immutable-classes-in-java-6268ff84bdd">Immutable Classes In Java</a></p><p><a href="https://medium.com/@javabackend175/hashcode-method-in-object-class-36f6abefa299">https://medium.com/@javabackend175/hashcode-method-in-object-class-36f6abefa299</a></p><p>Please follow on medium for more such inquisitive and detailed articles.<br><a href="https://twitter.com/JavaUnlocked">https://twitter.com/JavaUnlocked</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4568a9fda62f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/javarevisited/comparator-vs-comparable-in-java-4568a9fda62f">Comparator Vs Comparable In Java</a> was originally published in <a href="https://medium.com/javarevisited">Javarevisited</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[HashCode Method In Object Class]]></title>
            <link>https://medium.com/javarevisited/hashcode-method-in-object-class-36f6abefa299?source=rss-943a61b631c6------2</link>
            <guid isPermaLink="false">https://medium.com/p/36f6abefa299</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Java Spring Decoded]]></dc:creator>
            <pubDate>Mon, 10 Apr 2023 23:53:45 GMT</pubDate>
            <atom:updated>2023-06-29T07:24:10.607Z</atom:updated>
            <content:encoded><![CDATA[<p>Often we think why java has the <strong>hashcode</strong> method associated with it in the Object Class , where do we use it as developers and why we need to overwrite it when we are overriding the equals method ?</p><p>There is a contract between hashcode and equals method also :-</p><ul><li><em>If two objects are equal according to the equals(Object)<br>method, then calling the hashCode method on each of<br>the two objects must produce the same integer result.</em></li></ul><p>Lets Consider a scenario , where the contract violates.</p><p>Consider below example:-</p><pre>class MyObject{<br>    String value;<br>   <br>    @override<br>    public boolen equals(MyObject obj){<br>      return this.value.equals(obj.value);<br>    }<br>    @override<br>    publis int hashCode(MyObject obj){<br>      Random rand = new Random()<br>      return rand.nextInt(10);<br>    }<br>     <br>}<br><br><br>HashSet&lt;MyObject&gt; set = new HashSet&lt;&gt;();<br>MyObject obj1 = new MyObject();<br>obj1.setValue(&quot;similar&quot;);<br>MyObject obj2 = new MyObject();<br>obj1.setValue(&quot;similar&quot;);<br>set.add(obj1);<br>set.add(obj2);<br><br><br></pre><p>lets closely look the above code , here when we try to add the object to the hashset , it will check first the hashcode for both the object , even though these two objects are equal , both of these will get stored in HashSet as two different keys , resulting in duplicacy of Hashset , As we can see here , Not providing correct hashCode implementation leads to Incorrect behaviour in HashMaps , HashTables , HashSet .</p><p><strong>The general contract of hashCode is:</strong></p><ul><li>During the execution of the application, if hashCode() is invoked more than once on the same Object then it must consistently return the same Integer value, provided no information used in <strong>equals(Object)</strong> comparison on the Object is modified. It is not necessary that this Integer value to be remained same from one execution of the application to another execution of the same application.</li><li>If two Objects are equal, according to the <strong>equals(Object)</strong> method, then hashCode() method must produce the same Integer on each of the two Objects.</li><li>If two Objects are unequal, according to the <strong>equals(Object)</strong> method, It is not necessary the Integer value produced by hashCode() method on each of the two Objects will be distinct. It can be same but producing the distinct Integer on each of the two Objects is better for improving the performance of hashing based Collections like HashMap, HashTable…etc.</li></ul><p>Related Articles :-</p><p><a href="https://medium.com/@javabackend175/equals-method-in-java-34ff82f1180">https://medium.com/@javabackend175/equals-method-in-java-34ff82f1180</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=36f6abefa299" width="1" height="1" alt=""><hr><p><a href="https://medium.com/javarevisited/hashcode-method-in-object-class-36f6abefa299">HashCode Method In Object Class</a> was originally published in <a href="https://medium.com/javarevisited">Javarevisited</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Equals Method In Java]]></title>
            <link>https://medium.com/javarevisited/equals-method-in-java-34ff82f1180?source=rss-943a61b631c6------2</link>
            <guid isPermaLink="false">https://medium.com/p/34ff82f1180</guid>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[object-oriented]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Java Spring Decoded]]></dc:creator>
            <pubDate>Mon, 10 Apr 2023 23:23:26 GMT</pubDate>
            <atom:updated>2023-06-23T15:36:02.837Z</atom:updated>
            <content:encoded><![CDATA[<p>In Java equals() method is used to compare equality of two Objects. The equality can be compared in two ways:</p><p><strong>Shallow comparison: </strong>The default implementation of equals method is defined in Java.lang.Object class which simply checks if two Object references (say x and y) refer to the same Object. i.e. It checks if x == y. Since Object class has no data members that define its state, it is also known as shallow comparison. In This case , we are just comparing the memory location of the two objects , Even if the Objects in this Case contains the same state , it can return false.</p><pre>Integer i1 = new Integer(10);<br>Integer i2 = new Integer(10);<br></pre><p>Here the two references point to two different objects in heap , resulting false in == operator in java .</p><p><strong>Deep Comparison:</strong> Suppose a class provides its own implementation of equals() method in order to compare the Objects of that class w.r.t state of the Objects. That means data members (i.e. fields) of Objects are to be compared with one another. Such Comparison based on data members is known as deep comparison. Here we compare the states between the two objects and check if they possess same properties.</p><pre>Integer i1 = new Integer(10);<br>Integer i2 = new Integer(10);<br><br><br>@Override<br>public boolean equals(Integer i1 ,Integer i2){<br>  return i1.intValue() == i2.intValue(); </pre><p>As We can see here , the true equality between two objects of Integer class can be compared with the intValue they associate with , hence by overriding it we Enabled the true equals behaviour.</p><p><strong>Some principles of equals() method of Object class :</strong> If some other object is equal to a given object, then it follows these rules:</p><ul><li><strong>Reflexive :</strong> for any reference value a, a.equals(a) should return true.</li><li><strong>Symmetric :</strong> for any reference values a and b, if a.equals(b) should return true then b.equals(a) must return true.</li><li><strong>Transitive :</strong> for any reference values a, b, and c, if a.equals(b) returns true and b.equals(c) returns true, then a.equals(c) should return true.</li><li><strong>Consistent :</strong> for any reference values a and b, multiple invocations of a.equals(b) consistently return true or consistently return false, provided no information used in equals comparisons on the object is modified.</li></ul><p>Follow me On Twitter to be Updated on my new Article EveryDay ,</p><p><a href="https://twitter.com/JavaUnlocked">https://twitter.com/JavaUnlocked</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=34ff82f1180" width="1" height="1" alt=""><hr><p><a href="https://medium.com/javarevisited/equals-method-in-java-34ff82f1180">Equals Method In Java</a> was originally published in <a href="https://medium.com/javarevisited">Javarevisited</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Singelton Bean Serving Concurrent Requests In Spring Boot]]></title>
            <link>https://medium.com/javarevisited/singelton-bean-serving-concurrent-requests-in-spring-boot-945de1a38f3f?source=rss-943a61b631c6------2</link>
            <guid isPermaLink="false">https://medium.com/p/945de1a38f3f</guid>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[multithreading]]></category>
            <dc:creator><![CDATA[Java Spring Decoded]]></dc:creator>
            <pubDate>Mon, 10 Apr 2023 17:35:30 GMT</pubDate>
            <atom:updated>2023-06-23T15:35:56.190Z</atom:updated>
            <content:encoded><![CDATA[<p>We have wonder many a time , how a single spring bean can serve so many concurrent request without corrupting data of Individual Requests .</p><p>Lets See this Service Class :-</p><pre>@Service<br>class MyService {<br>  <br>  @Autowired<br>  MyFacade myFacade;<br><br>  public void doRun(){<br>    myFacade.doSomething()<br>  }<br>}</pre><p>Here MyService Class bean is using the dependency of MyFacade and calling a method of that Class , But we can see that MyService class has no state of itself , whenever the bean of MyService Class will be created , No State will be there in it .</p><p>lets See another case :-</p><pre>@Service<br>class MyService {<br>  <br>  int x = 10;<br>  <br>  @Autowired<br>  MyFacade myFacade;<br><br>  public void doRun(){<br>    myFacade.doSomething();<br>    x++;<br>  }<br>}</pre><p>Here we can see our bean has some property which will be shared across threads which will be using its <strong>doRun method.</strong></p><h3><strong>In Conclusion , To make our Spring Application serve concurrent requests in a multithreaded environment , we should always make Stateless Beans.</strong></h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=945de1a38f3f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/javarevisited/singelton-bean-serving-concurrent-requests-in-spring-boot-945de1a38f3f">Singelton Bean Serving Concurrent Requests In Spring Boot</a> was originally published in <a href="https://medium.com/javarevisited">Javarevisited</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Accessing Spring Bean Inside A Pojo]]></title>
            <link>https://medium.com/javarevisited/accessing-spring-bean-inside-a-pojo-c7a0d3da3ef8?source=rss-943a61b631c6------2</link>
            <guid isPermaLink="false">https://medium.com/p/c7a0d3da3ef8</guid>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[backend-development]]></category>
            <dc:creator><![CDATA[Java Spring Decoded]]></dc:creator>
            <pubDate>Mon, 10 Apr 2023 17:20:29 GMT</pubDate>
            <atom:updated>2023-06-23T15:33:50.593Z</atom:updated>
            <content:encoded><![CDATA[<p>Suppose we have a Pojo class MyPojo , which want to access an object from Spring Bean , How can we do that ?</p><p>A Simple Approach would be to use ApplicationContextAware Interface of Spring , We can implement this class to return Bean of desired class in Our Pojo.</p><p>As We can see Below , we want a Bean of MyBean Class in MyPojo Class , MyBean Class is managed by Spring while MyPojo is managed by developer, So we are using the <strong>getBean() method of ApplicationContextAware Interface .</strong></p><p>we just need to Pass the class name here and just like that we will get the spring managed object in our Pojo .</p><pre>class MyPojo{<br>  MyBean myBean = SpringContext.getBean(MyBeanClass.class);<br>}</pre><pre><br>     @Component<br>public class SpringContext implements ApplicationContextAware {<br>     <br>    private static ApplicationContext context;<br>     <br>    public static &lt;T extends Object&gt; T getBean(Class&lt;T&gt; beanClass) {<br>        return context.getBean(beanClass);<br>    }<br> <br>    @Override<br>    public void setApplicationContext(ApplicationContext context) throws BeansException {<br>        setContext(context);<br>    }     <br> <br>    <br>    private static synchronized void setContext(ApplicationContext context) {<br>        SpringContext.context = context;<br>    }<br>}</pre><p>P.S :-</p><p><strong>Some of you can think why can’t we directly use Autowired Annotation in the pojo and inject the bean , doing so can give a NPE as Our Pojo is not managed by Spring , its dependencies will not be injected .</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c7a0d3da3ef8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/javarevisited/accessing-spring-bean-inside-a-pojo-c7a0d3da3ef8">Accessing Spring Bean Inside A Pojo</a> was originally published in <a href="https://medium.com/javarevisited">Javarevisited</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Final Keyword In Java]]></title>
            <link>https://medium.com/javarevisited/final-keyword-in-java-c197c6a28345?source=rss-943a61b631c6------2</link>
            <guid isPermaLink="false">https://medium.com/p/c197c6a28345</guid>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[java8]]></category>
            <dc:creator><![CDATA[Java Spring Decoded]]></dc:creator>
            <pubDate>Mon, 10 Apr 2023 14:17:37 GMT</pubDate>
            <atom:updated>2023-06-23T15:33:48.641Z</atom:updated>
            <content:encoded><![CDATA[<p>We use Final keyword in three context in Java :-</p><ol><li>variable</li><li>method</li><li>class</li><li>Final Variable In Java</li></ol><ul><li>Once Initialized a final variable can’t be reassigned a different value or object.</li><li>The value of object referenced by final variable can change but the object being referenced by final variable can’t change.</li><li>A Blank Final Variable Can be Initialized only in Constructor.</li><li>A Static Blank Final Variable Can only be initialized in a static block.</li><li>Method Final Arguments can’t be reassigned a new value .</li></ul><pre>final List&lt;String&gt; list = new ArrayList&lt;&gt;();<br><br></pre><p>Here the list variable will always hold the same object but the object can be modified.</p><h3>2) Java final method</h3><p>If you make any method as final, you cannot override it.</p><p>The Subclasses won’t be able to use those method to demonstrate method overriding feature.</p><p>You Can Inherit Final Method , but you can’t override it.</p><h3>3) Java final class</h3><p>If you make any class as final, you cannot extend it.</p><p>No Class Can extend Final Class.<br>If Any Class tried to do so , it will fail at Compile Time Only.</p><p>Follow me On Twitter to be Updated on my new Article EveryDay ,</p><p><a href="https://twitter.com/JavaUnlocked">https://twitter.com/JavaUnlocked</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c197c6a28345" width="1" height="1" alt=""><hr><p><a href="https://medium.com/javarevisited/final-keyword-in-java-c197c6a28345">Final Keyword In Java</a> was originally published in <a href="https://medium.com/javarevisited">Javarevisited</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>