<?xml version="1.0" encoding="utf-8"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Collections · MangoDriod</title><link>https://md.eknath.dev/tags/collections/</link><description>Collections updates from MangoDriod</description><language>en-us</language><atom:link href="https://md.eknath.dev/tags/collections/index.xml" rel="self" type="application/rss+xml"/><lastBuildDate>Wed, 25 Nov 2026 09:45:04 +0530</lastBuildDate><item><title>Linked List in Java - Collections Framework- DSA</title><link>https://md.eknath.dev/posts/dsa/linked-list-in-java-collections/</link><guid isPermaLink="true">https://md.eknath.dev/posts/dsa/linked-list-in-java-collections/</guid><pubDate>Wed, 23 Feb 2022 13:02:00 +0530</pubDate><category>java</category><category>dsa</category><category>collections</category><category>linkedlist</category><description>Gist: A Linked list is dynamic array where a group of objects can be stored while following the insertion order and allow duplicates, the 2 types of linked list are singly and doubly linked lists, the default one is singly linked list where the elements can be traversed only in forward direction this is because unlike array here the elements are stored at random locations and only the node of each element knows the address of the next element. A simple example would be our circuit board here a point X may be connected to Y and Y connected to point Z here the insertion order is preserved and we can know the next point only by following the signals from the previous point.</description><content:encoded><![CDATA[<h3 id="gist">Gist:</h3>
<p>A Linked list is dynamic array where a group of objects can be stored while following the insertion order and allow duplicates, the 2 types of linked list are singly and doubly linked lists, the default one is singly linked list where the elements can be traversed only in forward direction this is because unlike array here the elements are stored at random locations and only the node of each element knows the address of the next element. A simple example would be our circuit board here a point X may be connected to Y and Y connected to point Z here the insertion order is preserved and we can know the next point only by following the signals from the previous point.</p>
<p>Same as the example but in Linked list each element has 2 parts in singly linked list where 1 part is the node with the address to the next node and the other which is the actual data of that element like a post letter the envelope has the address and inside the contents. but in doubly linked list there are 3 parts 2 of them are nodes which has the address of previous and next element and the other one is the actual data of that element.</p>
<p>The insertion or deletion are in constant time so compared to ArrayList the mega shifting process will be avoided.</p>
<h3 id="hierarchical-order">Hierarchical order</h3>
<p><img loading="lazy" src="https://md.eknath.dev/img/blogger/linked-list-in-java-collections-1.png"></p>
<h3 id="type-of-constructors">Type of constructors</h3>
<ul>
<li>Empty argument constructor or the default constructor is the same as invoking any other object here a new linked list object will be created, do note that there is no scope for reserving memory for the number of elements as we add new element the size is maintained dynamically a new LinkedList is an empty list.</li>
</ul>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>LinkedList list <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> LinkedList();
</span></span></code></pre></div><ul>
<li>Conversion of other Collections like ArrayList, Vector, etc&hellip;to a LinkedList is possible with the following constructor.</li>
</ul>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>LinkedList arraysFromVector <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> LinkedList( VariableNameOfTheCollection );
</span></span><span style="display:flex;"><span><span style="color:#75715e">//here any collection will be converted into a LinkedList</span>
</span></span></code></pre></div><h3 id="important-points">Important Points</h3>
<ul>
<li>By default an empty LinkedList is created.</li>
<li>Is Dynamically Growable and Shrinkable.</li>
<li>Heterogeneous Objects are allowed.</li>
<li>There is no initial capacity</li>
<li>No load factor is applicable here.</li>
<li>Order of insertion is preserved</li>
<li>We can also use generic data-type such that the list acts like a homogenous collection.</li>
<li>Random access interface is not implemented.</li>
<li>Ideal if the operations are Insertion or deletion.</li>
</ul>
<h3 id="implementing-interfaces">Implementing Interfaces</h3>
<ul>
<li>Serializable</li>
<li>Cloneable</li>
<li>Iterable &lt;E&gt;</li>
<li>Collection &lt;E&gt;</li>
</ul>
<h3 id="methods-available-to-only-the-linkedlist-object">Methods available to only the LinkedList object.</h3>
<ul>
<li>.addFirst(Element) -&gt; adds the element in the front of the LinkedList.</li>
<li>.addLast(Element) -&gt; add an element in the last part of the LinkedList.</li>
<li>getFirst() &ndash;&gt; returns the Object at the front of the Linked List.</li>
<li>getLast() &ndash;&gt; returns the last object in the linked list.</li>
<li>removeFirst() -&gt; returns the object in the first and removes it from the linked list</li>
<li>removeLast() &ndash;&gt; returns the last objects and removes the object from the linked list.</li>
</ul>
<table>
  <thead>
      <tr>
          <th>Pros</th>
          <th>Cons</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Ideal if insertion or deletion operations are more.</td>
          <td>Worst if there are lots of search operations as it needs to iterate through all the other elements.</td>
      </tr>
      <tr>
          <td>By default the elements can be traversed through forward direction.</td>
          <td>Java LinkedList class is non-synchronized.</td>
      </tr>
      <tr>
          <td>Can hold duplicate elements.</td>
          <td>Does not implement random access interface.</td>
      </tr>
      <tr>
          <td>Can hold null values.</td>
          <td></td>
      </tr>
      <tr>
          <td>The process of insertion or deletion is simple and fast.</td>
          <td></td>
      </tr>
  </tbody>
</table>
<hr>
<p><em>Note: This article was originally published on Blogger on February 23, 2022 and imported to MangoDriod.</em></p>
]]></content:encoded></item><item><title>Collection Interface - Java Collections Framework - DSA</title><link>https://md.eknath.dev/posts/dsa/collection-interface-in-java-dsa/</link><guid isPermaLink="true">https://md.eknath.dev/posts/dsa/collection-interface-in-java-dsa/</guid><pubDate>Fri, 11 Feb 2022 17:59:00 +0530</pubDate><category>java</category><category>dsa</category><category>collections</category><description>Most people consider the collection as the root interface of Collections Framework and it is true to a great extent but another part of Collections Framework is Map Interface, we will see that later, Most Common methods which are applicable to all collections are defined in this interface for example add() to add an element, size() to get the size and much more, below is a table of most common methods.</description><content:encoded><![CDATA[<p>Most people consider the collection as the root interface of Collections Framework and it is true to a great extent but another part of Collections Framework is Map Interface, we will see that later, Most Common methods which are applicable to all collections are defined in this interface for example <code>add()</code> to add an element, <code>size()</code> to get the size and much more, below is a table of most common methods.</p>
<h4 id="hierarchy-of-the-collection-interface">Hierarchy of the Collection Interface.</h4>
<p><img loading="lazy" src="https://md.eknath.dev/img/blogger/collection-interface-in-java-dsa-1.jpg"></p>
<p>The Parent of Collection Interface is <strong>Iterable</strong> Interface and the Collection is the base interface for List Interface, Set Interface and Queue Interface, the respective classes which implement either of the sub interfaces will also implement the defined methods from the Collection Interface, below are some of the commonly used methods.</p>
<h4 id="defined-methods">Defined Methods:</h4>
<table>
  <thead>
      <tr>
          <th>Method</th>
          <th>Description</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>add()</td>
          <td>This method returns a Boolean value true if it inserts the specified element in this collection.</td>
      </tr>
      <tr>
          <td>addAll()</td>
          <td>This method returns a Boolean value true if all elements were added successfully.</td>
      </tr>
      <tr>
          <td>clear()</td>
          <td>Removes all the elements of the collection.</td>
      </tr>
      <tr>
          <td>contains()</td>
          <td>returns a Boolean value if the specified object exists true else false.</td>
      </tr>
      <tr>
          <td>containsAll()</td>
          <td>returns a Boolean value if the specified Objects from the argument of collection and if all the elements exist inside the collection then it returns true else false.</td>
      </tr>
      <tr>
          <td>equals()</td>
          <td>returns true if the specified Object is equal to this current Collection.</td>
      </tr>
      <tr>
          <td>hashCode()</td>
          <td>returns the hash code value of this collection.</td>
      </tr>
      <tr>
          <td>isEmpty()</td>
          <td>returns true if this collection has no elements otherwise returns false.</td>
      </tr>
      <tr>
          <td>iterator()</td>
          <td>returns an Iterator over the elements in proper sequence.</td>
      </tr>
      <tr>
          <td>remove()</td>
          <td>Removes the first element where the specified element is found, but only the 1st element of the specified object will be removed.</td>
      </tr>
      <tr>
          <td>removeAll()</td>
          <td>same like remove() but all objects matching will be removed from this collection.</td>
      </tr>
      <tr>
          <td>removeIf()</td>
          <td>It removes all the elements of this collection that satisfy the given predicate filter.</td>
      </tr>
      <tr>
          <td>retainAll()</td>
          <td>The Objects inside the arguments collection will be kept and all other Objects will be removed from this collection.</td>
      </tr>
      <tr>
          <td>size()</td>
          <td>This returns the number of objects present inside this collection.</td>
      </tr>
      <tr>
          <td>spliterator()</td>
          <td>It returns a spliterator over the elements in this collection.</td>
      </tr>
      <tr>
          <td>toArray()</td>
          <td>It returns an array containing all the elements of this collection which are in proper sequence.</td>
      </tr>
  </tbody>
</table>
<h4 id="classes-that-implements-collection">Classes that implements Collection</h4>
<p><code>ArrayList</code> , <code>LinkedList</code> , <code>Vector</code> , <code>Stack</code> , <code>PriorityQueue</code> , <code>ArrayDeque</code> , <code>TreeSet</code> , <code>HashSet</code> , <code>LinkedHashSet</code></p>
<p>Which means all the above methods in the table can be used from the objects of these classes which implement Collection interface, along with these different collections have their own interfaces they implement depending on the underlying collections.</p>
<h4 id="sample-code">Sample Code:</h4>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#f92672">import</span> java.util.*;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">TestJavaCollection1</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">static</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">main</span>(String args<span style="color:#f92672">[]</span>) {
</span></span><span style="display:flex;"><span>        ArrayList<span style="color:#f92672">&lt;</span>String<span style="color:#f92672">&gt;</span> list <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> ArrayList<span style="color:#f92672">&lt;</span>String<span style="color:#f92672">&gt;</span>(); <span style="color:#75715e">// Creating arraylist</span>
</span></span><span style="display:flex;"><span>        list.<span style="color:#a6e22e">add</span>(<span style="color:#e6db74">&#34;Ravi&#34;</span>); <span style="color:#75715e">// Adding object in arraylist</span>
</span></span><span style="display:flex;"><span>        list.<span style="color:#a6e22e">add</span>(<span style="color:#e6db74">&#34;Vijay&#34;</span>);
</span></span><span style="display:flex;"><span>        list.<span style="color:#a6e22e">add</span>(<span style="color:#e6db74">&#34;Ravi&#34;</span>);
</span></span><span style="display:flex;"><span>        list.<span style="color:#a6e22e">add</span>(<span style="color:#e6db74">&#34;Ajay&#34;</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">// Traversing list through Iterator</span>
</span></span><span style="display:flex;"><span>        Iterator<span style="color:#f92672">&lt;</span>String<span style="color:#f92672">&gt;</span> itr <span style="color:#f92672">=</span> list.<span style="color:#a6e22e">iterator</span>();
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">while</span> (itr.<span style="color:#a6e22e">hasNext</span>()) {
</span></span><span style="display:flex;"><span>            System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(itr.<span style="color:#a6e22e">next</span>());
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// output</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// Ravi</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// Vijay</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// Ravi</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// Ajay</span>
</span></span></code></pre></div><hr>
<p><em>Note: This article was originally published on Blogger on February 11, 2022 and imported to MangoDriod.</em></p>
]]></content:encoded></item><item><title>Array List - Collections Framework in Java - DSA</title><link>https://md.eknath.dev/posts/dsa/array-list-of-collections-in-java-dsa/</link><guid isPermaLink="true">https://md.eknath.dev/posts/dsa/array-list-of-collections-in-java-dsa/</guid><pubDate>Fri, 11 Feb 2022 10:15:00 +0530</pubDate><category>java</category><category>dsa</category><category>collections</category><category>arraylist</category><description>Gist: An array list can store individual objects by following insertion order, here the initial capacity is 10 by default but can be modified as per the requirement, once the array list reaches its load factor then internal all the elements of the current array is copied to a new array with the new capacity and the reference variable will now be referring to this new array list and the old array will be dealt by the garbage collector.</description><content:encoded><![CDATA[<h3 id="gist">Gist:</h3>
<p>An array list can store individual objects by following insertion order, here the initial capacity is 10 by default but can be modified as per the requirement, once the array list reaches its load factor then internal all the elements of the current array is copied to a new array with the new capacity and the reference variable will now be referring to this new array list and the old array will be dealt by the garbage collector.</p>
<h3 id="hierarchical-order">Hierarchical order</h3>
<p><img loading="lazy" src="https://md.eknath.dev/img/blogger/array-list-of-collections-in-java-dsa-1.jpg"></p>
<h3 id="type-of-constructors">Type of constructors</h3>
<ul>
<li>Empty argument constructor or the default constructor is the same as invoking any other object here a new ArrayList is created with a default size of 10. Below is the most commonly used constructor by beginners and others alike.</li>
</ul>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>ArrayList array <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> ArrayList();
</span></span><span style="display:flex;"><span><span style="color:#75715e">//array has a capacity of 10</span>
</span></span></code></pre></div><ul>
<li>The default constructor above will allot only 10 slots but if you want the initial size to be 20 or 1000 you can do so with the following constructor this is ideal when you know the number of entries expected, compared to the default here you will save a lot of performance. since this part is important I will stretch it with an example.</li>
</ul>
<p>If you are preparing to insert 100 inputs by calling the default constructor, then every time the ArrayList reaches its capacity it will clone itself and grow to a certain capacity and this internal task will take a toll on your performance so instead if you already set the capacity while creating the array list this internal process of growing can be avoided thus saving a lot of performance and time.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>ArrayList arrayOfPages <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> ArrayList(380);
</span></span><span style="display:flex;"><span><span style="color:#75715e">//array has a capacity of 380</span>
</span></span></code></pre></div><ul>
<li>Conversion of other Collections like LinkedList, Vector, etc&hellip;to an ArrayList is possible with the following constructor.</li>
</ul>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>ArrayList arraysFromVector <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> ArrayList( VariableNameOfTheCollection );
</span></span><span style="display:flex;"><span><span style="color:#75715e">//here any collection will be converted into an ArrayList</span>
</span></span></code></pre></div><h3 id="important-points">Important Points</h3>
<ul>
<li>Is Dynamically Growable and Shrinkable.</li>
<li>Heterogeneous Objects are allowed.</li>
<li>The initial capacity is 10</li>
<li>Load Factor is 0.75</li>
<li>Order of insertion is preserved</li>
<li>We can also use generic data-type such that the array acts like a homogenous.</li>
</ul>
<h3 id="implementing-interfaces">Implementing Interfaces</h3>
<ul>
<li>Serializable</li>
<li>Cloneable</li>
<li>RandomAccess</li>
<li>Iterable &lt;E&gt;</li>
<li>Collection &lt;E&gt;</li>
<li>List &lt;E&gt;</li>
</ul>
<h3 id="commonly-used-methods-from-list-and-the-respective-base-classes">Commonly Used methods from List and the respective base classes.</h3>
<ul>
<li>.add(element) -&gt; add an element in the last.</li>
<li>.add(Index, element) -&gt; add an element at the specified index.</li>
<li>.addAll(Collection x) -&gt; adds all the elements of the collection x</li>
<li>.addAll(Index , Collection x) -&gt; adds all the elements of the collection x from the specified index.</li>
<li>.set(index,element) -&gt; change an element at the specified index.</li>
<li>.get(index) -&gt; Retrieve the element from the requested index.</li>
<li>.contains(object) -&gt; Retrieves a boolean value if the element is available..</li>
<li>.indexOf(object) -&gt; Retrieves index value if the element is available or -1.</li>
<li>.remove(object) -&gt; to remove an element at the requested position.</li>
<li>.sort() -&gt; Sorts the collection.</li>
<li>.subList(startIndex, endIndex) -&gt; returns an array list with values from the range of index.</li>
<li>.trimToSize() -&gt; The capacity of the array will be set to the size of the array to save memory.</li>
</ul>
<table>
  <thead>
      <tr>
          <th>Pros</th>
          <th>Cons</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Ideal if a lot of index-based retrieval or update operations will be performed.</td>
          <td>If elements need to be <strong>added</strong> or <strong>removed</strong> at an index the entire array elements will be shifted internally.</td>
      </tr>
      <tr>
          <td>Can be traversed through both directions.</td>
          <td>Java ArrayList class is non-synchronized.</td>
      </tr>
      <tr>
          <td>Can hold duplicate elements.</td>
          <td></td>
      </tr>
      <tr>
          <td>Can take hold of null.</td>
          <td></td>
      </tr>
      <tr>
          <td>Once the load factor is met then the capacity will be increased by (current capacity *3/2)+1</td>
          <td></td>
      </tr>
  </tbody>
</table>
<hr>
<p><em>Note: This article was originally published on Blogger on February 11, 2022 and imported to MangoDriod.</em></p>
]]></content:encoded></item></channel></rss>