<?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 Onkar Sarfare on Medium]]></title>
        <description><![CDATA[Stories by Onkar Sarfare on Medium]]></description>
        <link>https://medium.com/@onkarsarfare?source=rss-e54cafb78a3b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*zAtHvX6_KY7UKUlPK6SwhQ.jpeg</url>
            <title>Stories by Onkar Sarfare on Medium</title>
            <link>https://medium.com/@onkarsarfare?source=rss-e54cafb78a3b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 14:51:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@onkarsarfare/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[Understanding ArrayList: What It Is and When to Use It]]></title>
            <link>https://onkarsarfare.medium.com/what-isarraylist-and-why-is-arraylist-924a2edf09f0?source=rss-e54cafb78a3b------2</link>
            <guid isPermaLink="false">https://medium.com/p/924a2edf09f0</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[collection]]></category>
            <category><![CDATA[arrays]]></category>
            <category><![CDATA[arraylist-in-java]]></category>
            <category><![CDATA[java-arraylist]]></category>
            <dc:creator><![CDATA[Onkar Sarfare]]></dc:creator>
            <pubDate>Thu, 16 Apr 2026 05:16:35 GMT</pubDate>
            <atom:updated>2026-04-18T10:44:28.705Z</atom:updated>
            <content:encoded><![CDATA[<p>The topic of the Collection Framework has been a consistent favorite in interviews for a long time. Since it is widely used across most applications, understanding its internal workings has become essential rather than optional.</p><p>One of the most commonly used — and often misused — collections is the ArrayList. It has become a go-to data structure for many developers when it comes to storing data. Its flexibility and ease of use make it a staple in everyday development. However, ArrayList is frequently discussed in interviews not just because of its popularity, but because developers tend to rely on it blindly without fully understanding how it works.</p><p>This lack of understanding can lead to inefficient or inappropriate usage. Many developers use ArrayList without questioning when and why it should be used. This article breaks down Array List from scratch → internal working → performance → pitfalls → interview insights.</p><h3>Why Array List is Important in Interviews?</h3><p>The Java Collection Framework is one of the most frequently asked topics in interviews.</p><p>And among all collections, <strong>Array List is the most commonly used — and most misunderstood.</strong></p><p>Developers use it everywhere:</p><ul><li>storing data</li><li>API responses</li><li>temporary buffers</li><li>processing lists</li></ul><p>But most people never ask:</p><blockquote>How does Array List actually manage memory and resizing?</blockquote><p>Let’s understand that.</p><h3>So, what is an Array List?</h3><p>In simple terms:</p><blockquote>Array List is a resizable array implementation in Java.</blockquote><p>It is part of the Java Collection Framework and internally uses an array to store elements.</p><p>A standard array in Java requires you to define its capacity at the time of initialization. So its ,</p><ul><li>Fixed size</li><li>Cannot grow dynamically</li></ul><pre>int [] arr = new int[10];</pre><p>But Array List is resizable data structure built on top of Arrays that doesn’t need to define capacity upfront (in most cases)</p><pre>ArrayList&lt;Object&gt; list = new ArrayList&lt;Object&gt;();</pre><p>But here’s the real question:</p><blockquote>If Array List is built on arrays, how does it grow?</blockquote><p>Let’s go inside.</p><h3>Internal Structure of Array List</h3><pre>class ArrayList&lt;E&gt; {<br><br>    private static final int DEFAULT_CAPACITY = 10;<br><br>    private transient Object[] elementData;<br><br>    private int size;<br><br>    private static final Object[] EMPTY_ELEMENTDATA = {};<br><br>}</pre><h4>Key Components</h4><p><strong>1. </strong><strong>elementData (core storage)</strong></p><p>This is where elements are actually stored.This is the backbone of ArrayList.</p><pre>Object[] elementData;</pre><p><strong>2. size</strong></p><p>Tracks number of elements currently stored.</p><ul><li>it not capacity</li><li>its actual elements present</li></ul><p><strong>3. EMPTY_ELEMENTDATA</strong></p><p>Used for <strong>lazy initialization</strong></p><p>When you create:</p><pre>ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;();</pre><p>Here, no array is created immediately.</p><p><strong>So, what is Lazy Initialization in Array List?</strong></p><p>At creation:</p><pre>elementData = EMPTY_ELEMENTDATA;</pre><p>No memory allocated yet. When first element is added the real array is created.</p><h4>How add() Works Internally?</h4><pre>public boolean add(E e) {<br>    ensureCapacityInternal(size + 1);<br>    elementData[size++] = e;<br>    return true;<br>}</pre><p>Lets take step by step approach to understand it better</p><p><strong>Step 1: Ensure capacity</strong></p><p>When adding element to the list, first it checks if array needs resizing.</p><pre>ensureCapacityInternal(size + 1);</pre><p><strong>Step 1: Insert Element</strong></p><p>Element is placed in an array and size increases as shown below</p><pre>elementData[size++] = e;</pre><p><strong>First insertion behavior</strong></p><p>When first element is added:</p><ul><li>Array is empty</li><li>Capacity becomes 10 (default)</li></ul><pre>list.add(12);</pre><p>Internal state becomes:</p><pre>elementData → [12, null, null, null, null, null, null, null, null, null]<br>size = 1<br>capacity = 10</pre><h4><strong>Capacity Management Logic</strong></h4><p><strong>Step 1: ensureCapacityInternal()</strong></p><p>So, what happens here is that, after first insertion the capacity becomes 10.</p><pre>if (elementData == EMPTY_ELEMENTDATA) {<br>    minCapacity = Math.max(10, minCapacity);<br>}</pre><p><strong>Step 2: ensureExplicitCapacity()</strong></p><p>if Array size is full, then resize happens. We call grow method for that.</p><pre>if (minCapacity - elementData.length &gt; 0) {<br>    grow(minCapacity);<br>}</pre><h4><strong>Growth Strategy/Resizing</strong></h4><p>There is a formula applied to grow array size as follows:</p><pre>int newCapacity = oldCapacity + (oldCapacity &gt;&gt; 1);</pre><p>Here we are using bitwise operator that shifts the digits to right. This is equivalent to division by 2. In other words, oldCapacity &gt;&gt; 1 means half of oldCapacity.</p><p><strong>What happens during resize?</strong></p><pre>elementData = Arrays.copyOf(elementData, newCapacity);</pre><p>A new array is created and old elements are copied. The old array becomes garbage collectible.</p><h4>How get() Works?</h4><pre>public E get(int index) {<br>    rangeCheck(index);<br>    return (E) elementData[index];<br>}</pre><p>Here, element is fetched by index. So the time complexity is O(1). RangeCheck method is called to check if index is within allowed size.</p><h4>How remove() Works?</h4><pre>public E remove(int index) {<br>    rangeCheck(index);<br><br>    E oldValue = (E) elementData[index];<br><br>    int numMoved = size - index - 1;<br><br>    System.arraycopy(elementData, index + 1,<br>                     elementData, index,<br>                     numMoved);<br><br>    elementData[--size] = null;<br><br>    return oldValue;<br>}</pre><p>What happens internally?</p><pre>Example:<br><br>arr = [0, 2, 3, 4, 5]<br><br>--&gt; Remove index 2 i.e value 3 from array<br><br>Result:<br><br>[0, 2, 4, 5, null]<br><br>Elements are shifted left</pre><p>Elements are shifted left so time complexity is O(n). System.arraycopy method is used because it is fast compared to normal java loop. There reason being is,</p><ul><li>Implemented in native C/C++</li><li>Uses optimized memory copying</li><li>Faster than Java loops</li><li>CPU cache friendly</li></ul><h3><strong>Why Array List is Fast for Reads?</strong></h3><p>ArrayList is fast because:</p><ul><li>Contiguous memory allocation</li><li>Direct indexing → O(1)</li><li>CPU cache locality (important concept). When CPU loads data, it loads nearby memory too so, it means faster access!</li></ul><h3>ConcurrentModificationException</h3><p>This happens when:</p><blockquote>We modify Array List while iterating using for-each loop</blockquote><pre>for (Integer num : list) {<br>    if (num == 20) {<br>        list.remove(num);<br>    }<br>}</pre><p><strong>Why it happens?</strong></p><p>Array List maintains modCount and iterator maintains expectedModCount If there is a mimatch, there will be an exception.</p><p><strong>Safe way — use iterator.remove()</strong></p><pre>Iterator&lt;Integer&gt; it = list.iterator();<br><br>while (it.hasNext()) {<br>    if (it.next() == 20) {<br>        it.remove(); // safe<br>    }<br>}</pre><h3>When to Use Array List?</h3><ul><li>Fast random access needed (O(1))</li><li>Order matters</li><li>Frequent reads &gt; writes</li></ul><h3>When NOT to use Array List?</h3><p>Avoid Array List when:</p><ul><li>Frequent insertions/deletions in middle</li><li>Large shifting operations required</li><li>Linked structure is better (use LinkedList)</li></ul><h3>Summary (Interview Ready)</h3><ul><li>ArrayList is a dynamic array</li><li>Uses lazy initialization</li><li>Default capacity = 10 (on first add)</li><li>Grows by 1.5x when full</li><li>get() → O(1)</li><li>remove() → O(n)</li><li>Uses System.arraycopy for shifting</li><li>Fail-fast via modCount</li><li>Best for read-heavy operations</li></ul><h3>Final Thoughts</h3><p>ArrayList is simple on the surface.</p><p>But internally, it is a carefully optimized balance of:</p><ul><li>memory</li><li>performance</li><li>CPU cache usage</li><li>resizing strategy</li></ul><p>And that’s exactly what interviewers expect you to understand. Good luck!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=924a2edf09f0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Are You Living on Autopilot?]]></title>
            <link>https://onkarsarfare.medium.com/are-you-living-on-autopilot-0d20d8da4e9d?source=rss-e54cafb78a3b------2</link>
            <guid isPermaLink="false">https://medium.com/p/0d20d8da4e9d</guid>
            <category><![CDATA[mindfulness]]></category>
            <category><![CDATA[self-improvement]]></category>
            <category><![CDATA[spirituality]]></category>
            <category><![CDATA[success]]></category>
            <category><![CDATA[self-help]]></category>
            <dc:creator><![CDATA[Onkar Sarfare]]></dc:creator>
            <pubDate>Sat, 17 Jan 2026 18:51:21 GMT</pubDate>
            <atom:updated>2026-01-17T18:51:21.087Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hblgE2nqAhWJfQr2SJJv1w.png" /><figcaption>hamster on the wheel</figcaption></figure><p>When it comes to living life, we often evaluate it in terms of financial stability, relationships, health, and other factors that contribute to a happy and fulfilling existence. But as we move through each moment of our lives, how often do we truly question the choices we make? Do we regularly introspect about the consequences of our actions? Do we pause to reflect on the outcomes of specific situations, or do we simply move forward without awareness? Do we really have geniuine affinity for what we strive for?</p><p>Deep within, we desire many things — success, freedom, peace, abundance. Yet when it comes to manifesting these desires, do we make conscious and consistent efforts toward achieving them? Why is it that some people succeed, while others continue striving but never seem to reach their destination? Why do some individuals repeat the same mistakes despite multiple attempts, or eventually give up on their dreams altogether?</p><p>At times, the people we least expect suddenly transform their lives and become completely different versions of themselves. Someone who once had no interest in money may turn into a disciplined investor, carefully planning their finances and building significant wealth. Such transformations astonish us and compel us to ask: how does a person truly turn their life around? How can someone fundamentally change their nature and become someone they have never been before? What is the strategy behind such a shift?</p><h3>Everything Is Memory</h3><p>Who we are today is largely an accumulation of our past experiences and the information we have absorbed over time. The environment we grow up in, the situations we are exposed to, and the people who influence us all play a crucial role in shaping our identity. In many ways, our sense of self is borrowed from our surroundings.</p><p>You may have observed that some people react to certain situations without giving them a second thought. In most cases, these automatic reactions surface unconsciously because they are deeply rooted in the subconscious mind, which has been conditioned since childhood.</p><p>This inherited identity often determines the trajectory of a person’s life. Someone whose identity is rooted in fear, anxiety, or scarcity will inevitably view the world through a lens of limitation and lack. For such a person, life appears filled with obstacles, hostility, and constant friction — the world seems cynical and unforgiving. Conversely, someone whose identity is shaped by abundance, fearlessness, and love during childhood is more likely to discover opportunities even amidst chaos.</p><p>This leads to an important question: Do our likes and dislikes, our perspectives on life, and our inclinations arise from our own conscious choices, or are they inherited from our upbringing? Are we truly living our lives consciously, or are we operating on autopilot?</p><h3>Choosing Who You Become</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*naKIuSg_2IXNvz1H_A5fkw.png" /></figure><p>We have all encountered individuals — public figures or people around us — who have broken free from their status quo, deviated from their conditioning, and carved out a unique identity for themselves. These people did not achieve success solely through hard work; they also made conscious decisions by cultivating awareness in every moment of their lives.</p><p>Asking a simple “why” before acting mechanically can be immensely powerful. Similarly, a brief session of introspection at the end of the day can bring unconscious, autopilot behaviors into awareness. Once seen clearly, these patterns can be replaced with healthier, more intentional habits.</p><p>Students who achieve top ranks in examinations do more than study hard and practice consistently. The real effort lies in breaking old patterns and embracing new ones that foster growth. By identifying habits and thought processes that keep us stagnant and confined to an outdated identity, we gain the ability to transform ourselves repeatedly. A miser can become generous, an irresponsible or lazy person can become disciplined. It is only a matter of realization, coupled with a small amount of daily introspection, that dismantles the barriers of the old self.</p><p>There are already enough machines in the sky flying on autopilot. As a human being, you are meant to navigate life consciously. You deserve every bit of happiness and joy — so why not create your life deliberately and with awareness?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0d20d8da4e9d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Midnight library]]></title>
            <link>https://onkarsarfare.medium.com/the-midnight-library-22d4f0f2ff86?source=rss-e54cafb78a3b------2</link>
            <guid isPermaLink="false">https://medium.com/p/22d4f0f2ff86</guid>
            <category><![CDATA[spiritual-growth]]></category>
            <category><![CDATA[positive-attitude]]></category>
            <category><![CDATA[fiction]]></category>
            <category><![CDATA[books]]></category>
            <category><![CDATA[reading]]></category>
            <dc:creator><![CDATA[Onkar Sarfare]]></dc:creator>
            <pubDate>Sun, 12 Dec 2021 07:42:42 GMT</pubDate>
            <atom:updated>2021-12-12T07:42:42.549Z</atom:updated>
            <content:encoded><![CDATA[<h4>An eccentric fantasy Novel</h4><blockquote>“There is always a library for those who have managed to convince themselves that there is no way out of the suffering”</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TYD0aFwFJrpLvGKKA3_lYA.jpeg" /></figure><p>To begin with, unlike many others, I am not a fan of self help category books. Whatever books that I came across in that particular category or anything similar to it, I could only survive first few pages because most of them seemed over elaborated to the point that it felt bland to me.</p><p>When it comes to fantasy novels, I cannot resist the allure of magical elements, mysticism and non analytical base which teleports you into the world far away from the current god forsaken reality! A land of possibilities, adventure and utopian dreams.</p><p>Anyways, a friend of mine suggested me to read this fantasy novel “<strong>The Midnight Library</strong>” by<strong> Matt Haig</strong>. Immediately after looking at its enticing front cover, I decided to buy my own copy.(Never judge a Book by its cover……they say…..But that didn&#39;t work this time!)</p><p>Finally, started reading the book, and after reading first few pages, I didn&#39;t realise that it became a page turner. I developed a Netflix-like addiction for this book after reading every page as it kept fuelling my curiosity. and eventually it turned out to be an eye opener!</p><p>I wont go much into details and its not my intention to review this book or endorse the Author in any way. I just want to share what I learned and how this book changed my perspective towards “Life”.</p><p>Few things this book taught me which you might relate if you have read this masterpiece.</p><h3><strong>1. Acceptance</strong></h3><p>Come on! you already know Life doesn’t always happen our way. Things we expect might or might not happen. We can always create a master plan to reach our goals, but Life may throw some unexpected milestones that can throw us off the track. Disappointment is a part and parcel of life.</p><p>What comes our way is never in our hand, but what we make out of it, definitely is. Only after total surrender and total acceptance to this grand scheme of life, we start to believe that it is not full of Obstacles but new possibilities in every way and on every milestone. No decision is right or wrong, every decision is an opportunity that we have to accept and stick to it to gain a new experience.</p><h3>2. <strong>Happiness, Success, Peace is lame</strong></h3><p>Its funny to see that 90% of the world have similar goals and expectation. We are not truly living the life of our dreams, we are only living someone else’s dream. Thanks to social media which has defined the guidelines for a successful life.</p><p>Yearning for “Success”, is like asking for more empty calories. It contributes to nothing but only provides a temporary “kick” that only last for sometime and bloats our ego for lifetime, and then, we are back in the “rat race”.</p><p>True <strong>success</strong> lies in our mindset and not the metrics that are defined by society. Real sense of victory lies in the outcomes of every little tasks we do throughout the day.</p><p>True <strong>happiness</strong> lies in the process, in other terms, the “Journey ” rather than “destination”. Eventually we are going to reach somewhere, but it wont matter if we are already enjoying the our expedition.</p><p>Finally, taking into consideration all of the above things, being realistic about life brings certain amount of balance which fosters <strong>peace</strong>.</p><h3>Conclusion</h3><p>It doesn&#39;t matter how many different types of lives you live , the basic essence of <strong>”you”</strong> is same everywhere. If you are broken from within, then it doesn&#39;t matter what opportunity you are given, you will always blow it away.</p><p>Its just like a tree. Doesn&#39;t matter what branch or twig, if there isn&#39;t proper nourishment then the whole tree is going to be rotted and will fall down one day and……C’est la vie!</p><p>You are the nourishment of your life. the essence that has power to create or destroy. If you are constructive, then any kind of life you live will always be rich of experiences and full of happiness, peace and success. If you are Destructive, well….you need to meet Nora Seeds!</p><p><em>Finally, I would like to thank you all for reading this. I hope what I learned from this book will also benefit you all in some way.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=22d4f0f2ff86" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>