{"id":26862,"date":"2021-05-03T23:10:05","date_gmt":"2021-05-04T03:10:05","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/recent-posts\/linkedlist-and-iterators-in-java-programming-code-examples-java-j2ee-j2me-j2se\/"},"modified":"2021-05-03T23:10:05","modified_gmt":"2021-05-04T03:10:05","slug":"linkedlist-and-iterators-in-java-programming-code-examples-java-j2ee-j2me-j2se","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=26862","title":{"rendered":"LinkedList and Iterators in Java #Programming Code Examples #Java\/J2EE\/J2ME #J2SE"},"content":{"rendered":"<pre style='padding-left:10px;font-family:verdana;font-size:12px'>\n\/*\n * LinkedList.java\n *\n * Created on January 10, 2008, 8:51 PM\n *\n * To change this template, choose Tools | Template Manager\n * and open the template in the editor.\n *\/\n\npackage linkedlist;\n\nimport java.util.List;\nimport java.util.LinkedList;\nimport java.util.Iterator;\nimport java.util.ListIterator;\nimport java.util.Collections;\nimport java.util.Random;\n\n\/**\n *\n * @author Sayed\n *\/\npublic class LinkedListTest {\n    \n    \/** Creates a new instance of LinkedList *\/\n    public LinkedListTest() {\n    }\n    \n    \/**\n     *Example operations using linked lists\n     *\/\n    \n    public void linkedListOperation(){\n        \n        final int MAX = 10;\n        int counter = 0;\n\n        \/\/create two linked lists\n        List listA = new LinkedList();\n        List listB = new LinkedList();\n\n        \/\/store data in the linked list A\n        for (int i = 0; i &lt; MAX; i++) {\n            System.out.println(&quot;  - Storing Integer(&quot; + i + &quot;)&quot;);\n            listA.add(new Integer(i));\n        }\n       \n        \/\/print data from the linked list using iterator \n        Iterator it = listA.iterator();\n        while (it.hasNext()) {\n            System.out.println(it.next());\n        }\n\n        \/\/print data from the linked list using listIterator. \n        counter = 0;\n        ListIterator liIt = listA.listIterator();\n        while (liIt.hasNext()) {\n            System.out.println(&quot;Element [&quot; + counter + &quot;] = &quot; + liIt.next());\n            System.out.println(&quot;  - hasPrevious    = &quot; + liIt.hasPrevious());\n            System.out.println(&quot;  - hasNext        = &quot; + liIt.hasNext());\n            System.out.println(&quot;  - previousIndex  = &quot; + liIt.previousIndex());\n            System.out.println(&quot;  - nextIndex      = &quot; + liIt.nextIndex());\n            System.out.println();\n            counter++;\n        }\n\n\n        \/\/retrieve data from the linked list using index\n        for (int j=0; j &lt; listA.size(); j++) {\n            System.out.println(&quot;[&quot; + j + &quot;] - &quot; + listA.get(j));\n        }\n\n        \/\/find the location of an element\n        int locationIndex = listA.indexOf(&quot;5&quot;);\n        System.out.println(&quot;Index location of the String &quot;5&quot; is: &quot; + locationIndex);  \n\n        \/\/find the first and the last location of an element\n        System.out.println(&quot;First occurance search for String &quot;5&quot;.  Index =  &quot; + \n\t\tlistA.indexOf(&quot;5&quot;));\n        System.out.println(&quot;Last Index search for String &quot;5&quot;.       Index =  &quot; + \n\t\tlistA.lastIndexOf(&quot;5&quot;));\n\n        \/\/create a sublist from the list \n        List listSub = listA.subList(10, listA.size());\n        System.out.println(&quot;New Sub-List from index 10 to &quot; + listA.size() + &quot;: &quot; + \n\t\tlistSub);\n\n        \/\/sort the sub-list\n        System.out.println(&quot;Original List   : &quot; + listSub);\n        Collections.sort(listSub);\n        System.out.println(&quot;New Sorted List : &quot; + listSub);\n        System.out.println();\n\n        \/\/reverse the new sub-list\n        System.out.println(&quot;Original List     : &quot; + listSub);\n        Collections.reverse(listSub);\n        System.out.println(&quot;New Reversed List : &quot; + listSub);\n        System.out.println();\n\n        \/\/check to see if the lists are empty\n        System.out.println(&quot;Is List A empty?   &quot; + listA.isEmpty());\n        System.out.println(&quot;Is List B empty?   &quot; + listB.isEmpty());\n        System.out.println(&quot;Is Sub-List empty? &quot; + listSub.isEmpty());\n        \n        \/\/compare two lists\n        System.out.println(&quot;A=B? &quot; + listA.equals(listB));        \n        System.out.println();\n\n        \/\/Shuffle the elements around in some Random order for List A\n        Collections.shuffle(listA, new Random());\n\n        \/\/convert a list into an array\n        Object[] objArray = listA.toArray();\n        for (int j=0; j &lt; objArray.length; j++) {\n            System.out.println(&quot;Array Element [&quot; + j + &quot;] = &quot; + objArray[j]);\n        }\n\n        \/\/clear listA\n        System.out.println(&quot;List A   (before) : &quot; + listA);        \n        System.out.println();\n        listA.clear();\n        System.out.println(&quot;List A   (after)  : &quot; + listA);        \n        System.out.println();\n        \n    }\n    \/**\n     * @param args the command line arguments\n     *\/\n    public static void main(String[] args) {\n        \/\/ TODO code application logic here\n        LinkedListTest listExample = new LinkedListTest();\n        listExample.linkedListOperation();\n    }\n    \n}\n<\/pre>\n<p>Note: Brought from our old site: http:\/\/www.salearningschool.com\/example_codes\/ on Jan 2nd, 2017 From: http:\/\/sitestree.com\/?p=10175<br \/> Categories:Programming Code Examples, Java\/J2EE\/J2ME, J2SE<br \/>Tags:Java\/J2EE\/J2MEJ2SE<br \/> Post Data:2017-01-02 16:04:23<\/p>\n<p>\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com\/' target='new' rel=\"noopener\">https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\t(Big Data, Cloud, Security, Machine Learning): Courses: <a href='http:\/\/Training.SitesTree.com' target='new' rel=\"noopener\"> http:\/\/Training.SitesTree.com<\/a><br \/>\n\t\tIn Bengali: <a href='http:\/\/Bangla.SaLearningSchool.com' target='new' rel=\"noopener\">http:\/\/Bangla.SaLearningSchool.com<\/a><br \/>\n\t\t<a href='http:\/\/SitesTree.com' target='new' rel=\"noopener\">http:\/\/SitesTree.com<\/a><br \/>\n\t\t8112223 Canada Inc.\/JustEtc: <a href='http:\/\/JustEtc.net' target='new' rel=\"noopener\">http:\/\/JustEtc.net (Software\/Web\/Mobile\/Big-Data\/Machine Learning) <\/a><br \/>\n\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com'> https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\tMedium: <a href='https:\/\/medium.com\/@SayedAhmedCanada' target='new' rel=\"noopener\"> https:\/\/medium.com\/@SayedAhmedCanada <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/* * LinkedList.java * * Created on January 10, 2008, 8:51 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. *\/ package linkedlist; import java.util.List; import java.util.LinkedList; import java.util.Iterator; import java.util.ListIterator; import java.util.Collections; import java.util.Random; \/** * * @author Sayed *\/ public class LinkedListTest &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=26862\">Continue reading<\/a><\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1917],"tags":[],"class_list":["post-26862","post","type-post","status-publish","format-standard","hentry","category-fromsitestree-com","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":10102,"url":"http:\/\/bangla.sitestree.com\/?p=10102","url_meta":{"origin":26862,"position":0},"title":"J2SE : LinkedList and Iterators in Java","author":"","date":"July 31, 2015","format":false,"excerpt":"\/* \u00a0* LinkedList.java \u00a0* \u00a0* Created on January 10, 2008, 8:51 PM \u00a0* \u00a0* To change this template, choose Tools | Template Manager \u00a0* and open the template in the editor. \u00a0*\/ package linkedlist; import java.util.List; import java.util.LinkedList; import java.util.Iterator; import java.util.ListIterator; import java.util.Collections; import java.util.Random; \/** \u00a0* \u00a0* @author\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":65912,"url":"http:\/\/bangla.sitestree.com\/?p=65912","url_meta":{"origin":26862,"position":1},"title":"Linked List and Iterator in Java #Java Short Notes","author":"Author-Check- Article-or-Video","date":"July 17, 2021","format":false,"excerpt":"\/* * LinkedList.java * * Created on January 10, 2008, 8:51 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. *\/package linkedlist;import java.util.List;import java.util.LinkedList;import java.util.Iterator;import java.util.ListIterator;import java.util.Collections;import java.util.Random;\/** * * @author Sayed *\/public class LinkedListTest { \/** Creates a\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":23218,"url":"http:\/\/bangla.sitestree.com\/?p=23218","url_meta":{"origin":26862,"position":2},"title":"Linked List and Iterator in Java #Root #By Sayed Ahmed","author":"Author-Check- Article-or-Video","date":"March 27, 2021","format":false,"excerpt":"By Sayed: 8 [use Firefox or IE with large font size] \/* * LinkedList.java * * Created on January 10, 2008, 8:51 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. *\/ package linkedlist; import java.util.List; import java.util.LinkedList; import\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":65938,"url":"http:\/\/bangla.sitestree.com\/?p=65938","url_meta":{"origin":26862,"position":3},"title":"EJB 3: Entity Bean Basic #Java Short Notes","author":"Author-Check- Article-or-Video","date":"July 18, 2021","format":false,"excerpt":"Plain Java objects with persistence storage. They are not remotable and must be accessed through the new javax.persistence.EntityManager service. An entity bean implementation is provided in the following three files\/classesBeansOrder.javaLineItem.javaEntity ManagerShoppingCartBean.java\/\/Order.javaimport javax.persistence.CascadeType;import javax.persistence.Entity;import javax.persistence.FetchType;import javax.persistence.GeneratedValue; import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.OneToMany;import javax.persistence.Table;import javax.persistence.Id;import javax.persistence.CascadeType;import javax.persistence.FetchType;import java.util.ArrayList;import java.util.Collection;@Entity@Table(name = \"PURCHASE_ORDER\")public class Order implements\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":65876,"url":"http:\/\/bangla.sitestree.com\/?p=65876","url_meta":{"origin":26862,"position":4},"title":"Set, TreeSet, Iterator in Java #Java Short Notes #Blog","author":"Author-Check- Article-or-Video","date":"July 16, 2021","format":false,"excerpt":"\/* * TreeSetExample.java * *Illustrates mathematical set operations * * Created on January 10, 2008, 9:28 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. *\/package treesetexample;import java.util.Set;import java.util.TreeSet;import java.util.Iterator;\/** * * @author Sayed *\/public class TreeSetExample { \/**\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10473,"url":"http:\/\/bangla.sitestree.com\/?p=10473","url_meta":{"origin":26862,"position":5},"title":"IfTag.java, IfConditionTag.java, IfThenTag.java, and IfElseTag.java, Custom tags that make use of tag nesting","author":"","date":"August 28, 2015","format":false,"excerpt":"IfTag.java, IfConditionTag.java, IfThenTag.java, and IfElseTag.java, Custom tags that make use of tag nesting IfTag.java package cwp.tags; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; import javax.servlet.*; \/** A tag that acts like an if\/then\/else. \u00a0*\u00a0 <P> \u00a0*\u00a0 Taken from Core Web Programming Java 2 Edition \u00a0*\u00a0 from Prentice Hall and Sun Microsystems\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/26862","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=26862"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/26862\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26862"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}