<?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 Harshal Parekh on Medium]]></title>
        <description><![CDATA[Stories by Harshal Parekh on Medium]]></description>
        <link>https://medium.com/@harshalparekh20696?source=rss-2a6e065054e5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*nx7XI-qLND2PLAq7.jpg</url>
            <title>Stories by Harshal Parekh on Medium</title>
            <link>https://medium.com/@harshalparekh20696?source=rss-2a6e065054e5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 22:32:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@harshalparekh20696/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[What is “yield” or “generator” in Python?]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/swlh/what-is-yield-or-generator-in-python-4484a11362d0?source=rss-2a6e065054e5------2"><img src="https://cdn-images-1.medium.com/max/2000/1*2Yq3jCEYAzztVSuy_r-hkw.jpeg" width="2000"></a></p><p class="medium-feed-snippet">What is a coroutine in Python?</p><p class="medium-feed-link"><a href="https://medium.com/swlh/what-is-yield-or-generator-in-python-4484a11362d0?source=rss-2a6e065054e5------2">Continue reading on The Startup »</a></p></div>]]></description>
            <link>https://medium.com/swlh/what-is-yield-or-generator-in-python-4484a11362d0?source=rss-2a6e065054e5------2</link>
            <guid isPermaLink="false">https://medium.com/p/4484a11362d0</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[python3]]></category>
            <category><![CDATA[beginner]]></category>
            <category><![CDATA[knowledge-sharing]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Harshal Parekh]]></dc:creator>
            <pubDate>Sun, 12 Apr 2020 09:26:17 GMT</pubDate>
            <atom:updated>2020-04-12T10:53:53.668Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Hidden Features of Java]]></title>
            <link>https://medium.com/swlh/hidden-features-of-java-f6f6b46b0e42?source=rss-2a6e065054e5------2</link>
            <guid isPermaLink="false">https://medium.com/p/f6f6b46b0e42</guid>
            <category><![CDATA[knowledge-sharing]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[hidden-feature]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[cool-stuff]]></category>
            <dc:creator><![CDATA[Harshal Parekh]]></dc:creator>
            <pubDate>Fri, 19 Jul 2019 04:18:57 GMT</pubDate>
            <atom:updated>2019-08-06T20:06:15.753Z</atom:updated>
            <content:encoded><![CDATA[<p>A powerful language with features you might not know about.</p><h3>Joint Type Parameters:</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ffccee3f07e3d5edb23a6b6bb6e1c6a4/href">https://medium.com/media/ffccee3f07e3d5edb23a6b6bb6e1c6a4/href</a></iframe><p>You can have more than one parameter extended like the example above.</p><p>The above contrived method returns true if the two given collections are equal or if either one of them contains the given element, otherwise false. The point to notice is that you can invoke methods of both Comparable and Collection on the arguments b1 and b2.</p><h3>Labeled blocks</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3fe16518460641c60bebcb797108b900/href">https://medium.com/media/3fe16518460641c60bebcb797108b900/href</a></iframe><p>What’s especially unknown to many programmers is that you can actually label and break out of <strong>any</strong> old block. It doesn’t have to be a loop — you can just define some arbitrary block, give it a label, and use break with it.</p><h3>instanceof vs null</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a42d15e4bae0959094b5b0ec3b9544e/href">https://medium.com/media/3a42d15e4bae0959094b5b0ec3b9544e/href</a></iframe><p>That’s because Java has a special (<em>hidden</em>) null type that you do not see, nor can reference.</p><h3>Transfer of control in a finally block</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0aab1ce5b96f5a26be4c8c0e5c3eb116/href">https://medium.com/media/0aab1ce5b96f5a26be4c8c0e5c3eb116/href</a></iframe><p>The try/catch/finally flow control does what it’s intended to do, but only within certain limits. Similarly, you have to be careful not to cause an exception inside a catch/finally block, or you’ll also throw away the original exception. And if you do a System.exit() inside the try block, the finally block won’t be called. If you break the normal flow, you break the normal flow.</p><h3>Variable method parameters</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c055ab7ae60033f3fb9714f9b9c48f64/href">https://medium.com/media/c055ab7ae60033f3fb9714f9b9c48f64/href</a></iframe><p>Bars is automatically converted to array of the specified type.</p><h3>Calling methods directly from anonymous subclass</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b059a8f9e533ff94976b8c134009aa59/href">https://medium.com/media/b059a8f9e533ff94976b8c134009aa59/href</a></iframe><p>Sometimes I wonder how this is useful at all, but it works.</p><h3>Throwing null?</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b30cd04a9dcc14daf9a6e18dadb447b9/href">https://medium.com/media/b30cd04a9dcc14daf9a6e18dadb447b9/href</a></iframe><h3>0.equals(0) is false?</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c190b376a24bd5d57bda76398e7bf81/href">https://medium.com/media/2c190b376a24bd5d57bda76398e7bf81/href</a></iframe><h3>Class without main?</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2e9b626aee5c49e0dafdbb782be3a6d4/href">https://medium.com/media/2e9b626aee5c49e0dafdbb782be3a6d4/href</a></iframe><h3>Class inside a method?</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f23945daabc3b6b7aab72013850f19a8/href">https://medium.com/media/f23945daabc3b6b7aab72013850f19a8/href</a></iframe><p>Although, anonymous classes are overrated. Try debugging one sometime, or supporting one in the field, and you’ll see what I mean. Once you lose line numbers in a release build, they’re <em>very</em> hard to track.</p><h3>Static imports</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f9ad039ac8d6c0cdedf71c18744b365b/href">https://medium.com/media/f9ad039ac8d6c0cdedf71c18744b365b/href</a></iframe><h3>Love C?</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bd29a587c4f866a0001cd6f5360e0e9b/href">https://medium.com/media/bd29a587c4f866a0001cd6f5360e0e9b/href</a></iframe><p>Similar to C#, if ‘2’ is not found in the array, it returns a negative value but if you take the 1’s Complement of the returned value you actually get the position where ‘2’ can be inserted.<br>In the above example, position = -2, ~position = 1 which is the position where 2 should be inserted… it also lets you find the “closest” match in the array.</p><h3>Multiple instance initializers</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d5584005900b2d68de1ed9072d82e0da/href">https://medium.com/media/d5584005900b2d68de1ed9072d82e0da/href</a></iframe><h3>And finally, Double Brace Initializers</h3><p>Is this as <strong>inefficient</strong> as it sounds? Should its use be limited to one-off initializations?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f7c5a08456b7ce5a6d456a9177054aa2/href">https://medium.com/media/f7c5a08456b7ce5a6d456a9177054aa2/href</a></iframe><p>The “double brace initialization”, as already mentioned, is an anonymous inner class with a instance initialization block, which means that a new class is created for each “initialization”, all for the purpose of usually making a single object.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1c65e5c6d27b76021c6fc137726ada86/href">https://medium.com/media/1c65e5c6d27b76021c6fc137726ada86/href</a></iframe><p>Unfortunately, this code never made it to Java 7 or 8.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d3dc9915ea9f9cec4547a8cdc6a2daf9/href">https://medium.com/media/d3dc9915ea9f9cec4547a8cdc6a2daf9/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6a89f539ea55f03bbefc6580220b9351/href">https://medium.com/media/6a89f539ea55f03bbefc6580220b9351/href</a></iframe><pre>Output:</pre><pre>Test1 Times (ms)           Test2 Times (ms)<br>----------------           ----------------<br>           187                          0<br>           203                          0<br>           203                          0<br>           188                          0<br>           188                          0<br>           187                          0<br>           203                          0<br>           188                          0<br>           188                          0<br>           203                          0</pre><p>As can be seen, the double brace initialization has a noticeable execution time of around 190 ms.<br>Meanwhile, the ArrayList initialization execution time came out to be 0 ms. Of course, the timer resolution should be taken into account, but it is likely to be under 15 ms.<br>And yes, there were 1000 .class files generated by compiling the Test1 double brace initialization test program.</p><p>For anyone interested in looking at the class files generated by the all the anonymous classes:</p><pre>2019/05/27  16:35             1,602 DemoApp2$1.class<br>2019/05/27  16:35             1,976 DemoApp2$10.class<br>2019/05/27  16:35             1,919 DemoApp2$11.class<br>2019/05/27  16:35             2,404 DemoApp2$12.class<br>2019/05/27  16:35             1,197 DemoApp2$13.class</pre><pre>/* snip */</pre><pre>2019/05/27  16:35             1,953 DemoApp2$30.class<br>2019/05/27  16:35             1,910 DemoApp2$31.class<br>2019/05/27  16:35             2,007 DemoApp2$32.class<br>2019/05/27  16:35               926 DemoApp2$33$1$1.class<br>2019/05/27  16:35             4,104 DemoApp2$33$1.class<br>2019/05/27  16:35             2,849 DemoApp2$33.class<br>2019/05/27  16:35               926 DemoApp2$34$1$1.class<br>2019/05/27  16:35             4,234 DemoApp2$34$1.class<br>2019/05/27  16:35             2,849 DemoApp2$34.class</pre><pre>/* snip */</pre><pre>2019/05/27  16:35               614 DemoApp2$40.class<br>2019/05/27  16:35             2,344 DemoApp2$5.class<br>2019/05/27  16:35             1,551 DemoApp2$6.class<br>2019/05/27  16:35             1,604 DemoApp2$7.class<br>2019/05/27  16:35             1,809 DemoApp2$8.class<br>2019/05/27  16:35             2,022 DemoApp2$9.class</pre><p>I hope you enjoyed the article! Find more in my profile. Visit my portfolio here: <a href="https://harshal.one/?source=post_page---------------------------">https://harshal.one</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f6f6b46b0e42" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/hidden-features-of-java-f6f6b46b0e42">Hidden Features of Java</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Modifying org.json to handle duplicate keys]]></title>
            <link>https://medium.com/swlh/modifying-org-json-to-handle-duplicate-keys-975bfffe2f5a?source=rss-2a6e065054e5------2</link>
            <guid isPermaLink="false">https://medium.com/p/975bfffe2f5a</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[json]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Harshal Parekh]]></dc:creator>
            <pubDate>Tue, 25 Jun 2019 05:31:15 GMT</pubDate>
            <atom:updated>2019-06-25T05:31:15.595Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qCo2_V_ZbirLTUL8ceWliA.jpeg" /></figure><p>A JSONObject is an unordered collection of name/value pairs. Its external form is a string wrapped in curly braces with colons between the names and values, and commas between the values and names. The internal form is an object having get and opt methods for accessing the values by name, and put methods for adding or replacing values by name.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/598/1*aFIPfq6zTi5mNs3E-VcMsA.png" /><figcaption>JSON Object Representation</figcaption></figure><p>A sample JSON Object looks something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/67e57be4f91d722d7fd0dc20a35e8289/href">https://medium.com/media/67e57be4f91d722d7fd0dc20a35e8289/href</a></iframe><blockquote>This article is written with the assumption that you are already familiar with JSON and org.json library in Java.</blockquote><p>As per the documentation of <a href="http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf">JSON</a>: An object structure is represented as a pair of curly brackets surrounding zero or more name/value pairs (or members). A name is a string. A single colon comes after each name, separating the name from the value. A single comma separates a value from a following name. <strong>The names within an object SHOULD be unique.</strong></p><p>But, let’s get past this definition and assume that you have a string with duplicate keys and you have to parse that key into a JSONObject.</p><p>Sometimes you have an API response which is flawed and has duplicate keys. Instead of waiting for someone to fix the API, you can write a couple of classes to handle duplicate keys.</p><h4>How can we parse a string into a JSONObject with duplicate keys?</h4><p>Parse the string using wither regex or tokens, add each key-value pair to a hashmap, and in the end recreate your JSON document with the duplicates removed. In this case though I would only remove key-value pairs that are exactly the same. <em>This is not an ideal solution.</em></p><p><em>Or</em></p><p>Download the source code for org.json.JSONObject, and make a slight modification to the code to automatically leave out duplicates. This is a bit dangerous though. Another option is to create a modified version that simply validates and modifies.</p><p>The below code allows you to create a JSONObject with a string containing duplicate keys. Exceptions are thrown only when you have two key-values that have the same key, but different values. This was because I think it would be a problem to choose at random which of the two should be assigned (e.g. the latter value?). Of course this can be changed to work as you wish (e.g. keep last value for multiple keys).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1ac07dd244b45e6dceca823302906a8a/href">https://medium.com/media/1ac07dd244b45e6dceca823302906a8a/href</a></iframe><p>The code above only works for the keys at the first level of JSONObject. To extend the functionality to avoid duplicates in the inner objects as well, the code has to be modified as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/74aa678c46e2560962fd2bff46baad75/href">https://medium.com/media/74aa678c46e2560962fd2bff46baad75/href</a></iframe><p>The above code can be used as:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/36d4ed16b3ce8a6382f089ed355db740/href">https://medium.com/media/36d4ed16b3ce8a6382f089ed355db740/href</a></iframe><p>This solves your problem of handling duplicate keys.</p><p>I hope you enjoyed the article! Find more in my profile. Visit my portfolio here: <a href="https://harshal.one/">https://harshal.one</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=975bfffe2f5a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/modifying-org-json-to-handle-duplicate-keys-975bfffe2f5a">Modifying org.json to handle duplicate keys</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Branch Prediction — Everything you need to know.]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/swlh/branch-prediction-everything-you-need-to-know-da13ce05787e?source=rss-2a6e065054e5------2"><img src="https://cdn-images-1.medium.com/max/1300/1*sWtS-QQbrCSUYoH4jhWGeg.jpeg" width="1300"></a></p><p class="medium-feed-snippet">In computer science, predication is an architectural feature that provides an alternative to conditional transfer of control, implemented&#x2026;</p><p class="medium-feed-link"><a href="https://medium.com/swlh/branch-prediction-everything-you-need-to-know-da13ce05787e?source=rss-2a6e065054e5------2">Continue reading on The Startup »</a></p></div>]]></description>
            <link>https://medium.com/swlh/branch-prediction-everything-you-need-to-know-da13ce05787e?source=rss-2a6e065054e5------2</link>
            <guid isPermaLink="false">https://medium.com/p/da13ce05787e</guid>
            <category><![CDATA[optimization]]></category>
            <category><![CDATA[c]]></category>
            <category><![CDATA[branch-prediction]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Harshal Parekh]]></dc:creator>
            <pubDate>Sun, 23 Jun 2019 22:59:14 GMT</pubDate>
            <atom:updated>2019-06-25T07:47:04.829Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[A simple program to freeze your computer]]></title>
            <link>https://medium.com/@harshalparekh20696/a-simple-program-to-freeze-your-computer-b31a57aefc83?source=rss-2a6e065054e5------2</link>
            <guid isPermaLink="false">https://medium.com/p/b31a57aefc83</guid>
            <category><![CDATA[c]]></category>
            <category><![CDATA[shell]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Harshal Parekh]]></dc:creator>
            <pubDate>Sat, 16 Mar 2019 23:00:13 GMT</pubDate>
            <atom:updated>2019-03-16T23:00:13.333Z</atom:updated>
            <cc:license>https://creativecommons.org/publicdomain/mark/1.0/</cc:license>
            <content:encoded><![CDATA[<p>Ever wondered how does a piece of code look like that can lock up a traditional computer?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LFiJenS3MWyUQ6zElDdv9Q.jpeg" /></figure><p>I have always had this curiosity and finally now I have explored and written enough to come up with a some really dangerous pieces of code that will do the job.</p><p><strong>Fork Bomb: </strong>Very simple, yet effective. I came across the shell fork bomb years ago when I had to reboot the system. It looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1764762c9046851c49859edeb85cefc8/href">https://medium.com/media/1764762c9046851c49859edeb85cefc8/href</a></iframe><p>Just a shell function that calls itself twice and never terminates.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6b02cc449e20d44d278f5bab0df59139/href">https://medium.com/media/6b02cc449e20d44d278f5bab0df59139/href</a></iframe><p>This is how it look like in C. Well, this will get the job done, but nothing amazing about it.</p><p>How about allocating and leaking 1 KiB in an infinite loop? Let’s have a look at the code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c61d6f0ed9a76252a6af37db50360fd/href">https://medium.com/media/9c61d6f0ed9a76252a6af37db50360fd/href</a></iframe><p>If you run this on a x64 machine, it is capable of locking up your RAM in just a few seconds. But this does not accomplish the job in all the environments, like if you run x86 on a x64 architecture, it can only lock up about 4GB of your RAM. Also, the processes that are already running, will continue running. Sometimes the mouse pointer will move around smoothly, but not respond to inputs.</p><p>How about generating huge pages and writing them to the memory?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dbf1175389fa65cd343f43ce67971eb5/href">https://medium.com/media/dbf1175389fa65cd343f43ce67971eb5/href</a></iframe><p><em>This program will eventually finish. </em>First, it will give itself the maximum priority. Then it starts generating huge memory pages with a random number generator so the OS allocates these pages.</p><p><strong>Note:</strong> if you do not have the admin rights or sudo privileges, the priority setting code will fail.</p><p>I was able to kill the process, but the keyboard responded once in 5 seconds.</p><p>Write a comment if you have any more ideas!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b31a57aefc83" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>