<?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[Learnings with Java programming by shradha yewale - Medium]]></title>
        <description><![CDATA[This publication contains articles from basics to latest features of Java - Medium]]></description>
        <link>https://medium.com/java-programming-learnings?source=rss----e59a7a5f3358---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Learnings with Java programming by shradha yewale - Medium</title>
            <link>https://medium.com/java-programming-learnings?source=rss----e59a7a5f3358---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 05:01:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/java-programming-learnings" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Java: Map Interface]]></title>
            <link>https://medium.com/java-programming-learnings/java-map-interface-2fb8157cfee6?source=rss----e59a7a5f3358---4</link>
            <guid isPermaLink="false">https://medium.com/p/2fb8157cfee6</guid>
            <category><![CDATA[linkedhashmap]]></category>
            <category><![CDATA[java-collection]]></category>
            <category><![CDATA[hashmap]]></category>
            <category><![CDATA[hash-table]]></category>
            <category><![CDATA[treemap]]></category>
            <dc:creator><![CDATA[Shradha Yewale]]></dc:creator>
            <pubDate>Tue, 11 May 2021 11:56:48 GMT</pubDate>
            <atom:updated>2021-05-11T11:54:20.399Z</atom:updated>
            <content:encoded><![CDATA[<p>Map interface provides the mapping of key and value. Map contains a unique key which mapped to at most one value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tMxdU-KaEkI4anhrOXsXVQ.jpeg" /></figure><ul><li><strong>Creating objects with Map:</strong></li></ul><pre><strong>Map&lt;Integer, String&gt; map = new HashMap&lt;Integer, String&gt;();</strong></pre><h4>Implementations of Map interface:</h4><ol><li><strong>HashMap</strong>:</li></ol><p>HashMap class extends <em>Map </em>interface. It stores the value in form of key-value pair, and you can access the values by using the keys. It does not allow duplicate keys. If we try to insert, then it will replace the existing value corresponding to that key. It is the same as HashTable, but it is not synchronized.</p><p>HashMap can be synchronized by calling:</p><pre><strong><em>Map map = Collections.synchronizedMap(hashMap);</em></strong></pre><blockquote>In HashTable, Key is hashed using <em>hashCode()</em> and then resulting hash value is used as index to retrieve value stored at the index in HashTable.</blockquote><ul><li><strong>Difference between HashMap and HashTable:</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/753/1*uL2p7iQd_0c5y7SN12VTdQ.jpeg" /></figure><p><strong>2. LinkedHashMap:</strong></p><p>LinkedHashMap extends the <em>HashMap </em>class and it maintains the order in which elements are inserted. It is the same as HashMap but HashMap does not maintain the order of elements.</p><p><em>HashMap is quicker than LinkedHashMap for insertion, retrieval, and search of elements.</em></p><pre><strong>LinkedHashMap&lt;Integer, String&gt; linkedMap = new LinkedHashMap&lt;Integer, String&gt;();</strong></pre><p><strong>3. TreeMap:</strong></p><p>TreeMap implements the <em>NavigableMap </em>interface and extends <em>AbstractMap </em>class. TreeMap is sorted according to the natural ordering(ascending) of its keys. It uses Comparator at the time of map creation to sort and store key-value pairs. TreeMap is not synchronized. If multiple threads are accessing it then it must be synchronized externally.</p><pre><strong>TreeMap&lt;Integer, String&gt; treeMap = new TreeMap&lt;Integer, String&gt;();</strong></pre><ul><li><strong>Code implementation</strong></li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/983d95aa2d70282147f75de3bef13eb4/href">https://medium.com/media/983d95aa2d70282147f75de3bef13eb4/href</a></iframe><h4>Applications of Maps:</h4><ol><li>To map classes to students. Class(Key) -&gt; Students(list of values)</li><li>To map manager and employees.</li><li>To map States to cities.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2fb8157cfee6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/java-programming-learnings/java-map-interface-2fb8157cfee6">Java: Map Interface</a> was originally published in <a href="https://medium.com/java-programming-learnings">Learnings with Java programming by shradha yewale</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Java Collections: Set Interface]]></title>
            <link>https://medium.com/java-programming-learnings/java-collections-set-interface-6644cb4a83ca?source=rss----e59a7a5f3358---4</link>
            <guid isPermaLink="false">https://medium.com/p/6644cb4a83ca</guid>
            <category><![CDATA[java-collection]]></category>
            <category><![CDATA[hashset]]></category>
            <category><![CDATA[set]]></category>
            <category><![CDATA[tree-set]]></category>
            <category><![CDATA[linkedhashset]]></category>
            <dc:creator><![CDATA[Shradha Yewale]]></dc:creator>
            <pubDate>Thu, 06 May 2021 16:27:41 GMT</pubDate>
            <atom:updated>2021-05-06T10:05:23.431Z</atom:updated>
            <content:encoded><![CDATA[<p>Set interface extends Collection interface. It is an unordered collection that does not stores duplicate values.</p><blockquote><em>Java Collection: Queue interface → </em><a href="https://medium.com/java-programming-learnings/java-collections-queue-interface-7502028f1542">https://medium.com/java-programming-learnings/java-collections-queue-interface-7502028f1542</a></blockquote><h4>Creating objects with Set:</h4><pre><strong>Set&lt;Obj&gt; set = new </strong><strong>HashSet&lt;Obj&gt;()</strong></pre><p><em>Since it is an interface, we cannot create an object of it. It uses an implementation of the HashSet class.</em></p><h4>Classes that implements Set interface:</h4><ol><li><strong>HashSet</strong>: HashSet class implements Set interface. It uses the <strong><em>Hash table</em></strong> data structure for data storage.</li></ol><pre><strong>HashSet&lt;String&gt; hSet = new HashSet&lt;&gt;();</strong></pre><p><strong>2. LinkedHashSet:</strong> It is an ordered version of HashSet. It implements the Set interface and extends the HashSet class. It uses a <strong><em>Hash table</em></strong> and <strong><em>linked list</em></strong> data structure for data storage.</p><pre><strong>LinkedHashSet&lt;String&gt; lhSet = new LinkedHashSet&lt;&gt;();</strong></pre><p><strong>3. Treeset:</strong> It implements the SortedSet interface. It uses a <strong><em>Tree </em></strong>data structure for storing the elements. It stores objects in sorted and ascending order. Elements are sorted by keys. It can accept only generic types which can comparable.</p><pre><strong>TreeSet&lt;String&gt; tSet = new TreeSet&lt;&gt;();                          </strong></pre><ul><li><strong>Difference between HashSet, LinkedHashSet, and TreeSet.</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/871/1*bbYiGKw_WK9D56wA5jwGxg.jpeg" /></figure><p><strong>Code Implementation</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/09050949ed681f9845dc3878bf5cb9b0/href">https://medium.com/media/09050949ed681f9845dc3878bf5cb9b0/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6644cb4a83ca" width="1" height="1" alt=""><hr><p><a href="https://medium.com/java-programming-learnings/java-collections-set-interface-6644cb4a83ca">Java Collections: Set Interface</a> was originally published in <a href="https://medium.com/java-programming-learnings">Learnings with Java programming by shradha yewale</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Java Collections: List Interface]]></title>
            <link>https://medium.com/java-programming-learnings/java-collections-list-interface-a6fa0376581b?source=rss----e59a7a5f3358---4</link>
            <guid isPermaLink="false">https://medium.com/p/a6fa0376581b</guid>
            <category><![CDATA[java-coll]]></category>
            <category><![CDATA[vector]]></category>
            <category><![CDATA[stack]]></category>
            <category><![CDATA[arraylist]]></category>
            <category><![CDATA[linked-lists]]></category>
            <dc:creator><![CDATA[Shradha Yewale]]></dc:creator>
            <pubDate>Mon, 03 May 2021 14:31:02 GMT</pubDate>
            <atom:updated>2021-05-03T12:09:18.481Z</atom:updated>
            <content:encoded><![CDATA[<p>List interface extends the Collection interface. It is an ordered collection and can contain duplicate elements.</p><blockquote><strong>Iterable and Collection interface in Java Collection framework</strong> <a href="https://medium.com/java-programming-learnings/iterable-and-collection-interface-in-java-collection-framework-f4af3965b355">https://medium.com/java-programming-learnings/iterable-and-collection-interface-in-java-collection-framework-f4af3965b355</a></blockquote><p><strong>Create List Objects: </strong>List is an interface hence we cannot create an object of it.</p><pre>List&lt;object&gt; list = new ArrayList&lt;&gt;();</pre><h4>Classes in List Interface:</h4><p><strong>1. ArrayList</strong></p><p>ArrayList is a class that implements List interface provides a dynamic array in java. That means you can add and remove elements to/from the array whenever you want. Data access is quite easy with ArrayList Since elements are stored in contiguous memory locations.</p><p><strong>List&lt;String&gt; list = new </strong><strong>ArrayList&lt;&gt;();</strong></p><p><strong>2. LinkedList</strong></p><p>LinkedList is a class that also implements a List interface and inherently implements a linked list data structure. In this, every element is an object that contains <strong><em>data </em></strong>and <strong><em>address </em></strong>the next object. Each object is called a <strong><em>Node</em></strong>. LinkedList is preferred over ArrayList for data modification (Insertion and Deletion).</p><p><strong>List&lt;Integer&gt; ll = new </strong><strong>LinkedList&lt;Integer&gt;();</strong></p><p><strong><em>Difference between ArrayList and LinkedList</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/791/1*VEQo-I91TUXYmz3TJtF5nQ.jpeg" /></figure><p><strong>Code implementation:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fc2d729b435cd9db834c55171b6e58b1/href">https://medium.com/media/fc2d729b435cd9db834c55171b6e58b1/href</a></iframe><p><strong>3. Vector</strong></p><p>Vector implements a dynamic array that means it can grow and shrink as required. They are like ArrayList.</p><p><strong>List&lt;Integer&gt; vector = new </strong><strong>Vector&lt;Integer&gt;();</strong></p><p><strong><em>Difference between Vector and ArrayList</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/792/1*FrqSbpyztvziOakp88fyDw.jpeg" /></figure><p><strong>4. Stack</strong></p><p>Stack class extends Vector class and it is an implementation of Stack data structure. It works in the “Last In First Out(LIFO)” fashion.</p><p><strong>List&lt;String&gt; stack = new </strong><strong>Stack&lt;String&gt;();</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2b00816aa4741f890abbcace68e1ce47/href">https://medium.com/media/2b00816aa4741f890abbcace68e1ce47/href</a></iframe><p><strong><em>Java Collection: Queue interface:</em></strong> <a href="https://medium.com/java-programming-learnings/java-collections-queue-interface-7502028f1542">https://medium.com/java-programming-learnings/java-collections-queue-interface-7502028f1542</a> ]</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a6fa0376581b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/java-programming-learnings/java-collections-list-interface-a6fa0376581b">Java Collections: List Interface</a> was originally published in <a href="https://medium.com/java-programming-learnings">Learnings with Java programming by shradha yewale</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Java Collections: Queue Interface]]></title>
            <link>https://medium.com/java-programming-learnings/java-collections-queue-interface-7502028f1542?source=rss----e59a7a5f3358---4</link>
            <guid isPermaLink="false">https://medium.com/p/7502028f1542</guid>
            <category><![CDATA[deque]]></category>
            <category><![CDATA[queue]]></category>
            <category><![CDATA[java-collection]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[priority-queue]]></category>
            <dc:creator><![CDATA[Shradha Yewale]]></dc:creator>
            <pubDate>Mon, 03 May 2021 05:39:56 GMT</pubDate>
            <atom:updated>2021-05-03T10:31:50.479Z</atom:updated>
            <content:encoded><![CDATA[<p>The Queue interface extends the Collection interface. It stores and processes objects in <strong>FIFO (First In First Out) </strong>fashion.</p><blockquote>Java Collection: List interface → <a href="https://shradhayewale.medium.com/java-collections-list-interface-a6fa0376581b">https://shradhayewale.medium.com/java-collections-list-interface-a6fa0376581b</a></blockquote><h4>Creating objects with Queue:</h4><pre><strong>Queue&lt;T&gt; queue = new PriorityQueue&lt;T&gt;();</strong></pre><blockquote>Since Queue is an interface, we cannot create an object of it. The most commonly used implementations are LinkedList and PriorityQueue.</blockquote><p><strong>PriorityQueue</strong>: It processed objects based on the priority on First In First Out(FIFO) fashion. It worked the same as Queue, but <strong><em>the difference is that PriorityQueue processed objects based on their priority.</em></strong></p><h4>Methods of Queue interface:</h4><p>1. <strong><em>add(obj)</em></strong> : Insert the element(obj) at the end of queue.</p><p>2. <strong><em>remove(obj)</em></strong>: It removes and returns the element at the head of the queue.</p><p>3. <strong><em>poll()</em></strong>: It removes and returns the element at the head of the queue. It returns null if the queue is empty.</p><p>4. <strong><em>element()</em></strong>: It retrieves the element at the head of the queue but does not remove the element.</p><p>5. <strong><em>peek()</em></strong>: It retrieves the element at the head of the queue but does not remove the element. It returns null if the queue is empty.</p><ul><li><strong>Code implementation:</strong></li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa0a83ca77c02b9996b38de2d9a81313/href">https://medium.com/media/fa0a83ca77c02b9996b38de2d9a81313/href</a></iframe><h3>Deque:</h3><p>Deque interface extends Queue interface. It is a <strong><em>double ended queue</em></strong> where elements can be added or removed from the either end of the queue. That means it can be used as a queue (<strong><em>First In First Out</em></strong>) or as a stack (<strong><em>Last In Last Out</em></strong>).</p><h4>Creating object with Deque :</h4><pre><strong>Deque&lt;Obj&gt; deque = new ArrayDeque&lt;Obj&gt;();</strong></pre><p><strong>Code implementation of Deque :</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d120133c219cdc7385098ba353cdc4cd/href">https://medium.com/media/d120133c219cdc7385098ba353cdc4cd/href</a></iframe><ul><li><strong>Applications of Deque:</strong></li></ul><ol><li>Maintain internet browsing history of user : Deque can be used to maintain the list of recently visited URL’s with recently visited added at the start of deque and removing the URL’s at the end of the queue when visited URL’s range increased.</li><li>Deque can be used to check if string is palindrome or not.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7502028f1542" width="1" height="1" alt=""><hr><p><a href="https://medium.com/java-programming-learnings/java-collections-queue-interface-7502028f1542">Java Collections: Queue Interface</a> was originally published in <a href="https://medium.com/java-programming-learnings">Learnings with Java programming by shradha yewale</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Iterable and Collection interface in Java Collection framework]]></title>
            <link>https://medium.com/java-programming-learnings/iterable-and-collection-interface-in-java-collection-framework-f4af3965b355?source=rss----e59a7a5f3358---4</link>
            <guid isPermaLink="false">https://medium.com/p/f4af3965b355</guid>
            <category><![CDATA[iterable-interface]]></category>
            <category><![CDATA[java-collection]]></category>
            <dc:creator><![CDATA[Shradha Yewale]]></dc:creator>
            <pubDate>Tue, 27 Apr 2021 15:15:50 GMT</pubDate>
            <atom:updated>2021-03-15T12:18:55.734Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article, we are going to see the Iterable and Collection Interface in detail.</p><blockquote><strong>Hierarchy and flow diagram of Java Collection framework:</strong> <a href="https://shradhayewale.medium.com/collection-framework-in-java-7c413ac094e5">https://shradhayewale.medium.com/collection-framework-in-java-7c413ac094e5</a></blockquote><h4>Iterable Interface</h4><p>Iterable is the main interface in the java collection framework. It is used to iterate over the elements of the collection type.</p><p>Method : <strong>public</strong> Iterator&lt;T&gt; <strong>iterator</strong>();</p><pre>//<strong>Iterate through the list using iterator</strong><br>Iterator&lt;String&gt; iterator = list.iterator();<br>while (iterator.hasNext()) {<br>  String element = iterator.next();<br>  System.out.println(element);<br>}</pre><pre>//<strong>Iterate through the list using for loop</strong><br>for (String element : list ) {<br>    System.out.println(element);<br>}</pre><pre>//<strong>Iterate through the list using for each loop</strong><br>list.forEach((element) -&gt; { System.out.println(element); });</pre><h4>Collection Interface</h4><p>The collection represents a group of objects. Some allow duplicate elements, some do not allow duplicate elements, some are ordered elements, others are unordered elements. It also provides methods to perform basic operations such as read, write, remove, etc.</p><p>This interface is implemented by all other collection classes.</p><ul><li><strong>Few methods of Collection interface:</strong></li></ul><pre><strong>int </strong>size();</pre><pre><strong>boolean </strong>isEmpty();</pre><pre><strong>boolean </strong>contains(Object o);</pre><pre><strong>boolean </strong>add(E e);</pre><pre><strong>boolean </strong>remove(Object o);</pre><pre><strong>boolean </strong>containsAll(Collection&lt;?&gt; c);</pre><pre><strong>boolean </strong>addAll(Collection&lt;? <strong>extends </strong>E&gt; c);</pre><pre><strong>boolean </strong>removeAll(Collection&lt;?&gt; c);</pre><p><strong>Code implementation for Iterable and Collection interface:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bade4887e910dcfc335449afa7349775/href">https://medium.com/media/bade4887e910dcfc335449afa7349775/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f4af3965b355" width="1" height="1" alt=""><hr><p><a href="https://medium.com/java-programming-learnings/iterable-and-collection-interface-in-java-collection-framework-f4af3965b355">Iterable and Collection interface in Java Collection framework</a> was originally published in <a href="https://medium.com/java-programming-learnings">Learnings with Java programming by shradha yewale</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Collection Framework in Java]]></title>
            <link>https://medium.com/java-programming-learnings/collection-framework-in-java-7c413ac094e5?source=rss----e59a7a5f3358---4</link>
            <guid isPermaLink="false">https://medium.com/p/7c413ac094e5</guid>
            <category><![CDATA[java-collection]]></category>
            <category><![CDATA[collections-framework]]></category>
            <category><![CDATA[collection-hierarchy]]></category>
            <category><![CDATA[map-interface]]></category>
            <category><![CDATA[collection-interface]]></category>
            <dc:creator><![CDATA[Shradha Yewale]]></dc:creator>
            <pubDate>Tue, 27 Apr 2021 15:15:43 GMT</pubDate>
            <atom:updated>2021-03-09T14:06:33.906Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently, I have started reading the Java Collection framework and I will be writing series of articles of my learnings from scratch.</p><p><strong>What is the Collection framework:</strong></p><p>The collection<strong> </strong>framework in java provides many interfaces and classes which gives the programmer access to prepackaged data structures as well as algorithms for manipulating them.</p><blockquote>Collection interface and Map interface are two main interfaces of java collection framework.</blockquote><h4>Collection Interface :</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gTQvVsAU7xC0Q8UNlNJGPA.jpeg" /><figcaption>Collection interface hierarchy</figcaption></figure><blockquote>One of the advantages of the Collection framework is that it provides consistent API → [The classes that implements common interfaces share the common methods] e.g. Since then classes <strong>ArrayList</strong>, <strong>LinkedList</strong>, <strong>Vector</strong>, and <strong>Stack</strong> implements common interface <strong>List</strong>, They share all the methods of <strong>List</strong>, <strong>Collection </strong>and <strong>Iterable </strong>interface.</blockquote><h4>Map Interface :</h4><p>Map interface provides interface and classes which represents a mapping between key and value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Lt4fV-Wf677Tc1CHI2BedQ.jpeg" /><figcaption>Map interface hierarchy</figcaption></figure><p>Every interface and class in the Collection interface as well as in the Map interface have some significance. Let’s see their significance in form of a flow diagram. The following flow diagram depicts the significance of which class or interface to be used based on the respective requirements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-DCWHmBy-gvITGi19FWXsw.png" /></figure><p>Stay tuned for an upcoming article: <strong><em>Iterable and Collection interface in java collection framework :)</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7c413ac094e5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/java-programming-learnings/collection-framework-in-java-7c413ac094e5">Collection Framework in Java</a> was originally published in <a href="https://medium.com/java-programming-learnings">Learnings with Java programming by shradha yewale</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Generics in Java]]></title>
            <link>https://medium.com/java-programming-learnings/generics-in-java-e43a9a1a49fc?source=rss----e59a7a5f3358---4</link>
            <guid isPermaLink="false">https://medium.com/p/e43a9a1a49fc</guid>
            <category><![CDATA[abstract-class]]></category>
            <category><![CDATA[java8]]></category>
            <category><![CDATA[inheritance]]></category>
            <category><![CDATA[java-generic]]></category>
            <dc:creator><![CDATA[Shradha Yewale]]></dc:creator>
            <pubDate>Fri, 19 Feb 2021 17:07:49 GMT</pubDate>
            <atom:updated>2020-12-06T14:50:00.425Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*RBN_WbJ0MfmdtHHdxFrSBQ.png" /><figcaption><strong>JAVA GENERICS</strong></figcaption></figure><p>Generics in java means parameterized types to deal with type-safe objects. It allows type like Integer, String, …etc., and user-defined types to be a parameter to methods, classes, and interfaces.</p><p>Class, method, or interface which operates on this parameterized type is called a Generic entity.</p><blockquote><strong>Note: Generics cannot work with primitive types.</strong></blockquote><p><em>The following example illustrates the use of generics wherein the </em><strong><em>Player </em></strong><em>class is an abstract class. </em><strong><em>FootballPlayer </em></strong><em>and </em><strong><em>BasketballPlayer </em></strong><em>are the classes that manage the players of the football team and basketball team respectively. Both of these classes extend Player class which contain property shared by both of the class. Class </em><strong><em>Team </em></strong><em>contains the functionality to manage the activities of all teams.</em></p><p><strong>Player.java</strong></p><pre><strong>public abstract class </strong>Player {<br>    <strong>private </strong>String <strong>name</strong>;<br><br>    <strong>public </strong>Player(String name) {<br>        <strong>this</strong>.<strong>name </strong>= name;<br>    }<br><br>    <strong>public </strong>String getName() {<br>        <strong>return name</strong>;<br>    }<br>}</pre><p><strong>FootballPlayer.java</strong></p><pre><strong>public class </strong>FootballPlayer <strong>extends </strong>Player {<br>    <strong>public </strong>FootballPlayer(String name) {<br>        <strong>super</strong>(name);<br>    }<br>}</pre><p><strong>BaseballPlayer.java</strong></p><pre><strong>package </strong>com.example.java.generics.model;<br><br><strong>public class </strong>BaseballPlayer <strong>extends </strong>Player {<br>    <strong>public </strong>BaseballPlayer(String name) {<br>        <strong>super</strong>(name);<br>    }<br>}</pre><p><strong>Team.java</strong></p><p>Here Team class accepts the generic type as a parameter:</p><pre><strong>public class </strong>Team&lt;T <strong>extends </strong>Player&gt; {<br><br>    <strong>private </strong>String <strong>name</strong>;<br>    <strong>int played </strong>= 0;<br>    <strong>int won </strong>= 0;<br>    <strong>int lost </strong>= 0;<br>    <strong>int tied </strong>= 0;<br><br>    <strong>private </strong>ArrayList&lt;T&gt; <strong>members </strong>= <strong>new </strong>ArrayList&lt;&gt;();<br><br>    <strong>public </strong>Team(String name) {<br>        <strong>this</strong>.<strong>name </strong>= name;<br>    }<br><br>    <strong>public </strong>String getName() {<br>        <strong>return name</strong>;<br>    }<br><br>    <strong>public boolean </strong>addPlayer(T player) {<br>        <strong>if </strong>(<strong>members</strong>.contains(player)) {<br>            <em>//No need to typecast here as &lt;T extends Player&gt;.<br>            </em>System.<strong><em>out</em></strong>.println(player.getName() + <strong>&quot; is already in this team&quot;</strong>);<br>            <strong>return false</strong>;<br>        } <strong>else </strong>{<br>            <strong>members</strong>.add(player);<br>            System.<strong><em>out</em></strong>.println(player.getName()+ <strong>&quot;Picked for this team&quot;</strong>);<br>            <strong>return true</strong>;<br>        }<br>    }<br><br>    <strong>public int </strong>numPlayers() {<br>        <strong>return this</strong>.<strong>members</strong>.size();<br>    }<br><br>    <strong>public void </strong>matchResult(Team&lt;T&gt; opponent, <strong>int </strong>ourScore, <strong>int </strong>theirScore) {<br>        String message;<br>        <strong>if</strong>(ourScore &gt; theirScore) {<br>            <strong>won</strong>++;<br>            message = <strong>&quot; Beat &quot;</strong>;<br>        } <strong>else if </strong>(ourScore == theirScore) {<br>            <strong>tied</strong>++;<br>            message = <strong>&quot; Drew with &quot;</strong>;<br>        } <strong>else </strong>{<br>            <strong>lost</strong>++;<br>            message = <strong>&quot; Lost to &quot;</strong>;<br>        }<br>        <strong>played</strong>++;<br>        <strong>if </strong>(opponent != <strong>null</strong>) {<br>            System.<strong><em>out</em></strong>.println(<strong>this</strong>.getName() + message + opponent.getName());<br>            opponent.matchResult(<strong>null</strong>, theirScore, ourScore);<br>        }<br>    }<br><br>    <strong>public int </strong>ranking() {<br>        <strong>return </strong>(<strong>won </strong>* 2) + <strong>tied</strong>;<br>    }<br>}</pre><blockquote><strong><em>Team&lt;T extends Player&gt; </em></strong><em>implies Team class accepts parameter of type Player and the classes that extends Player class. In our case both FootballPlayer and BaseballPlayer.</em></blockquote><blockquote><strong><em>Team&lt;T&gt; </em></strong><em>implies class accepts the generic parameter but we need to type cast to respective type when using the respective type.</em></blockquote><p><strong>Generics.java (main class)</strong></p><pre><strong>public class </strong>Generics {<br>    <strong>public static void </strong>main(String args[]) {<br><br>        Team&lt;FootballPlayer&gt; starKins = <strong>new </strong>Team&lt;&gt;(<strong>&quot;StarKins&quot;</strong>);<br>        FootballPlayer fb = <strong>new </strong>FootballPlayer(<strong>&quot;FB&quot;</strong>);<br>        starKins.addPlayer(fb);<br><br>        Team&lt;FootballPlayer&gt; wideWings = <strong>new </strong>Team&lt;&gt;(<strong>&quot;WideWings&quot;</strong>);<br>        FootballPlayer ob = <strong>new </strong>FootballPlayer(<strong>&quot;OB&quot;</strong>);<br>        wideWings.addPlayer(ob);<br><br>        Team&lt;BaseballPlayer&gt; baseBallTeam = <strong>new </strong>Team&lt;&gt;(<strong>&quot;Sunrisers&quot;</strong>);<br>        BaseballPlayer bb = <strong>new </strong>BaseballPlayer(<strong>&quot;BB&quot;</strong>);<br>        baseBallTeam.addPlayer(bb);</pre><pre><em>        //Comparing Football teams &quot;starKins&quot; and &quot;Sunrisers&quot;.<br>        </em>starKins.matchResult(wideWings, 2, 1);<br>        System.<strong><em>out</em></strong>.println(<strong>&quot;Rankings&quot;</strong>);<br>        System.<strong><em>out</em></strong>.println(starKins.getName() + <strong>&quot; : &quot; </strong>+ starKins.ranking());<br>        System.<strong><em>out</em></strong>.println(wideWings.getName() + <strong>&quot; : &quot; </strong>+ wideWings.ranking());<br><br>    }<br>}</pre><ul><li>We can specify multiple classes in the generic type declaration like this :</li></ul><pre><strong>public class </strong>Team&lt;T <strong>extends </strong>Player &amp; Coach &amp; Manager&gt;</pre><h3>Why use Generics:</h3><ol><li><em>Individual typecasting is not needed.</em></li><li><em>It allows code reusability.</em></li><li>It provides compile-time safety.</li></ol><p><strong>Complete code for this available at : </strong><a href="https://github.com/SHRADHA-YEWALE/Java-features/tree/Generics">https://github.com/SHRADHA-YEWALE/Java-features/tree/Generics</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e43a9a1a49fc" width="1" height="1" alt=""><hr><p><a href="https://medium.com/java-programming-learnings/generics-in-java-e43a9a1a49fc">Generics in Java</a> was originally published in <a href="https://medium.com/java-programming-learnings">Learnings with Java programming by shradha yewale</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>