<?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 KarthigaKK on Medium]]></title>
        <description><![CDATA[Stories by KarthigaKK on Medium]]></description>
        <link>https://medium.com/@karthi.kcube2006?source=rss-bc1a34e8f791------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*bqbt8cU8UkJUsRso</url>
            <title>Stories by KarthigaKK on Medium</title>
            <link>https://medium.com/@karthi.kcube2006?source=rss-bc1a34e8f791------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 11:59:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@karthi.kcube2006/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[Collections in Java]]></title>
            <link>https://medium.com/@karthi.kcube2006/collections-in-java-1f3b49da7b9d?source=rss-bc1a34e8f791------2</link>
            <guid isPermaLink="false">https://medium.com/p/1f3b49da7b9d</guid>
            <dc:creator><![CDATA[KarthigaKK]]></dc:creator>
            <pubDate>Thu, 30 Jun 2022 21:51:49 GMT</pubDate>
            <atom:updated>2022-06-30T21:55:13.209Z</atom:updated>
            <content:encoded><![CDATA[<p>Any group of individual objects which are represented as a single unit is known as the collection of the objects. In Java, a separate framework named the <em>“Collection Framework”</em> has been defined in JDK 1.2 which holds all the collection classes and interface in it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/192/1*AIi2MO_K60_-p5y0LNE6vg.png" /></figure><p>The Collection interface ( <strong>java.util.Collection</strong>) and Map interface ( <strong>java.util.Map</strong>) are the two main “root” interfaces of Java collection classes.</p><p><strong>What is a Framework?</strong></p><p>A framework is a set of classes and interfaces which provide a ready-made architecture. In order to implement a new feature or a class, there is no need to define a framework. However, an optimal object-oriented design always includes a framework with a collection of classes such that all the classes perform the same kind of task.</p><p><strong>Need for a Separate Collection Framework</strong></p><p>In Java to store 10 integer variables we store in 10 separate variables its complex programming.<br><strong>Ex:</strong> int[] aArray = new int[10];</p><p>To overcome this we got an Array List(To store multiple values of the same type in a single variable). Array lists hold homogenous values and are fixed in size. Also Array doesn’t provide logic for Data Structure or Algorithm in any separate API’s. To overcome these limitations in Array we got Collection Framework.</p><p><strong>Advantages of the Collection Framework:</strong> Since the lack of a collection framework gave rise to the above set of disadvantages, the following are the advantages of the collection framework.</p><ol><li><strong>Consistent API:</strong> The API has a basic set of interfaces like <em>Collection</em>, <em>Set</em>, <em>List</em>, or <em>Map</em>, all the classes (ArrayList, LinkedList , Vector, etc) that implement these interfaces have <em>some</em> common set of methods.</li><li><strong>Reduces programming effort:</strong> A programmer doesn’t have to worry about the design of the Collection but rather he can focus on its best use in his program. Therefore, the basic concept of Object-oriented programming (i.e.) abstraction has been successfully implemented.</li><li><strong>Increases program speed and quality:</strong> Increases performance by providing high-performance implementations of useful data structures and algorithms because in this case, the programmer need not think of the best implementation of a specific data structure. He can simply use the best implementation to drastically boost the performance of his algorithm/program.</li></ol><h3>Hierarchy of the Collection Framework</h3><p>The utility package, (java.util) contains all the classes and interfaces that are required by the collection framework. The collection framework contains an interface named an iterable interface which provides the iterator to iterate through all the collections. This interface is extended by the main collection interface which acts as a root for the collection framework. All the collections extend this collection interface thereby extending the properties of the iterator and the methods of this interface. The following figure illustrates the hierarchy of the collection framework.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/0*xyXlBRpvRpuDj2D_.png" /></figure><p>Before understanding the different components in the above framework, let’s first understand a class and an interface.</p><ul><li><strong>Class:</strong> A class is a user-defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type.</li><li><strong>Interface:</strong> Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract (only method signature, no body). Interfaces specify what a class must do and not how. It is the blueprint of the class.</li></ul><h3>Methods of the Collection Interface</h3><blockquote><em>This interface contains various methods which can be directly used by all the collections which implement this interface. They are:</em></blockquote><blockquote><strong><em>add(Object)-</em></strong><em>This method is used to add an object to the collection.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*tYgwDtEV5uByi7gIlBsxWA.png" /></figure><blockquote><strong><em>contains(object o)-T</em></strong><em>his method returns true if the collection contains the specified element.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*y-J5mYj1iWQLnPurkgDzww.png" /></figure><blockquote><strong><em>size()-</em></strong><em>This method is used to return the number of elements in the collection.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*onbH3uju7V_RFtmbe6z67A.png" /></figure><blockquote><strong><em>addAll(Collection c)-</em></strong><em>This method adds all the elements in the given collection to this collection.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*sTOvFylk_w8hpRr6SC3omw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*p8Pu0j6ejueYEh5WsSBlmg.png" /></figure><blockquote><strong><em>isEmpty()-</em></strong><em>This method returns true if this collection contains no elements.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*SeFna7r9wr90koKsriv2xw.png" /></figure><blockquote><strong><em>max()-</em></strong><em>This method is used to return the maximum value present in the collection.</em></blockquote><blockquote><strong><em>remove()-</em></strong><em>This method is used to remove the element inside a collection</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*tn0DHnc_CiskEwO6XI5ujQ.png" /></figure><p>remove method is overload we can remove element based on given index another by just pass element to remove that particular element.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*Fi2RmtLP4lJvtBX3NzvPnQ.png" /></figure><p><strong>get()-</strong>This method used to get elements from ArrayList based on index.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*Lky3QGchmf4FyZ3pby-2LQ.png" /></figure><p><strong>set()</strong>-This method used to modify element at given index.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/808/1*6zt3hr8SqiTc0TMDkEvn5A.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1f3b49da7b9d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Different Ways To Iterate through List in Java:]]></title>
            <link>https://medium.com/@karthi.kcube2006/different-ways-to-iterate-through-list-in-java-53d8e509b055?source=rss-bc1a34e8f791------2</link>
            <guid isPermaLink="false">https://medium.com/p/53d8e509b055</guid>
            <dc:creator><![CDATA[KarthigaKK]]></dc:creator>
            <pubDate>Tue, 12 Jan 2021 01:34:52 GMT</pubDate>
            <atom:updated>2022-07-02T19:48:15.860Z</atom:updated>
            <content:encoded><![CDATA[<p>The List interface is a part of <strong>java.util</strong> package and it inherits the Collection interface. An object of list interface allows us to organize both unique and duplicate values also maintains the insertion order .ie whatever the order we insert in same order values will be displayed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/240/1*gCd5kPtAcGgv9g2fQJdiYg.png" /></figure><p>There are several ways to iterate over List in Java. They are discussed below:</p><p><strong>Method 1: Simple for loop</strong></p><p>Each element can be accessed by iteration using a simple for loop. The index can be accessed using the index as a loop variable.</p><p><strong>Syntax:</strong></p><p><em>Example 1:</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/412/1*pyUgxX_fPRaFf3PNn3ijaA.png" /></figure><p><em>Example 2:</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/839/1*RUP0eqhnNHR86m21fExiYQ.png" /></figure><p><strong>Method 2: Enhanced for loop</strong></p><p>Each element can be accessed by iteration using an enhanced for loop. This loop was introduced in J2SE 5.0. It is an alternative approach to traverse the for a loop. It makes the code more readable.</p><p><strong>Syntax:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/368/1*SdPZcMA6FrRnPQ2QIORREA.png" /></figure><p><strong>Method 3: Using a while loop</strong></p><p>Iterating over a list can also be achieved using a while loop. The block of code inside the loop executes until the condition is true. A loop variable can be used as <strong>an </strong>index to access each element.</p><p><strong>Syntax:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/448/1*Tdg9MBmSNuZ9lcELTe3DkQ.png" /></figure><p>An iterator is an object in Java that allows iterating over elements of a collection. Each element in the list can be accessed using iterator with a while loop.</p><p><strong>Syntax:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/578/1*5PfQA_AY24-9J-lHY1_iVw.png" /></figure><p><strong>Method 4: Using List iterator</strong></p><p>ListIterator is an iterator in java which is available since the 1.2 version. It allows us to iterate elements one-by-one from a List implemented object. It is used to iterator over a list using while loop.</p><p><strong>Syntax</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/457/1*GDKAMbadhU81qyhYp--dMw.png" /></figure><p><strong>Method 5: Using Iterable.forEach()</strong></p><p>This feature is available since Java 8. It can also be used to iterate over a List. Iteration can be done using <strong>a </strong>lambda expression.</p><p><strong>Syntax:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/478/1*PecV7rAtCbOZvSVuo-pkwg.png" /></figure><p><strong>Method 6: Using Stream.forEach()</strong></p><p>The processing order of stream().forEach() is undefined while in case of forEach(), it is defined. Both can be used to iterate over a List.</p><p><strong>Syntax:</strong></p><p>First we convert list into stream and used forEach() method to iterate over list. We can also directly iterate from list elements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/654/1*9kN7HxS9bktGKjoPvS5uog.png" /></figure><blockquote>Tip: Use ArrayList anywhere no removal and insertions in the middle.</blockquote><ol><li><strong><em>ArrayList</em></strong> is implemented on behavior to add the elements or values at the end of the list.</li><li>In case of read-only collections or collections which are rarely modified, ArrayList is suitable.</li></ol><p>For example if you are working with huge volume data and after adding the values to <strong><em>ArrayList</em></strong> if you do not want to remove the values or add new values in the between the exiting values then you are good to use the <strong><em>ArrayList</em></strong> in such scenario’s.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=53d8e509b055" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>