{"id":736,"date":"2016-02-17T06:37:30","date_gmt":"2016-02-17T06:37:30","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?page_id=736"},"modified":"2018-06-05T06:35:46","modified_gmt":"2018-06-05T06:35:46","slug":"list","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/java\/list","title":{"rendered":"List Interface Tutorial In Java With Example"},"content":{"rendered":"<p>List is the sequence of elements, or we can say collection of elements, on which user has precise control over where an elements are inserted. These elements can be accessed by their index value (Starting from 0) which is of Integer type and with the help of this index, user can search any element easily.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> List allows duplicate values as well as null values in it.<\/p>\n<p>List is a child of Collection Interface, In addition to adding all methods implemented in Collection Interface, List adds its own methods to add or delete elements on the bases of index that are discussed below.<\/p>\n<p>Some of these methods can throw exceptions, (like UnsupportedOperationException) if the given collection cannot be modified and Exception ClassCastException is generated if given object is not compatible with other.<\/p>\n<hr \/>\n<h4><strong>Hierarchy of List Interface<\/strong><\/h4>\n<p>List Interface extends Collection Interface which further extends Iterable Interface, as shown in figure 1.<\/p>\n<p><center><a href=\"\/java\/wp-content\/uploads\/2016\/02\/hierarchy-of-List-Interface.png\" rel=\"attachment wp-att-737\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-737\" src=\"\/java\/wp-content\/uploads\/2016\/02\/hierarchy-of-List-Interface-193x300.png\" alt=\"hierarchy of List Interface\" width=\"193\" height=\"300\" srcset=\"https:\/\/abhiandroid.com\/java\/wp-content\/uploads\/2016\/02\/hierarchy-of-List-Interface-193x300.png 193w, https:\/\/abhiandroid.com\/java\/wp-content\/uploads\/2016\/02\/hierarchy-of-List-Interface.png 256w\" sizes=\"auto, (max-width: 193px) 100vw, 193px\" \/><\/a><\/center><\/p>\n<hr \/>\n<h4><strong>List Implementations:<\/strong><\/h4>\n<p><span style=\"color: #008000;\"><strong>ArrayList\u00a0<\/strong><\/span><\/p>\n<p>ArrayList is a Class that is implementation of List Interface, that allows elements to be added and removed dynamically from it. If in case more elements are added than its capacity, Arraylist automatically increases its size. Elements in it can be accessed directly using get() and set() methods using index value, but add() and remove() operations has very low performance as all the elements are shifted by this operation leading to change in index values.<\/p>\n<p><span style=\"color: #008000;\"><strong>LinkedList<\/strong><\/span><\/p>\n<p>LinkedList is a class that is also the implementation of List Interface which is implemented using Doubly Linked List, It allows elements to be added dynamically in it, if in case more elements are added than its capacity, linkedlist\u00a0 automatically increases its size. Operations such as add() and remove() performs better than arrayList whereas operations such as get() and set() performs very poor as compared to arrayList.<\/p>\n<p><strong><span style=\"color: #008000;\">Vectors<\/span><\/strong><\/p>\n<p>Vector is also the implementation of List Interface. Points that distinguishes vector from arraylist and linkedlist are vectors are Synchronized and also vector is a legacy class that is, it is introduced before JDK 1.2. and it do not implement most of the collection methods.<\/p>\n<hr \/>\n<h4><strong>Example of List Interface In JAVA:<\/strong><\/h4>\n<p>Let us discuss List Interface with the help of program. In this program objects of all the List implementations (Vector, ArrayList, LinkedList) are created and data of String type is added and then displayed, we have divided this program into 3 Steps that we will discuss one by one.<\/p>\n<pre>import java.util.*;\r\npublic class ListInterfaceDemo{\r\npublic static void main(String args[]){\r\n\r\n\/\/Step 1: Create Objects\r\nList&lt;String&gt; arrayList = new ArrayList&lt;String&gt;();\u00a0\r\nList&lt;String&gt; linkedList= new LinkedList&lt;String&gt;();\r\nList&lt;String&gt; vectorObject= new Vector&lt;String&gt;();\r\n\r\n\/\/Step 2: Add values\r\narrayList.add(\"This\");\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\narrayList.add(\"is\");\r\narrayList.add(\"ArrayList\");\r\n\r\nlinkedList.add(\"This\");\r\nlinkedList.add(\"is\");\r\nlinkedList.add(\"LinkedList\");\r\n\r\nvectorObject.add(\"This\");\r\nvectorObject.add(\"is\");\r\nvectorObject.add(\"vector\");\r\n\r\n\/\/Step 3:Display values\r\nSystem.<em>out<\/em>.println(\"ArrayList: \"+arrayList);\r\nSystem.<em>out<\/em>.println(\"LinkedList: \"+linkedList);\r\nSystem.<em>out<\/em>.println(\"Vector : \"+vectorObject);\r\n}\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>ArrayList: [This, is, ArrayList]\r\nLinkedList: [This, is, LinkedList]\r\nVector : [This, is, vector]\r\n<\/pre>\n<p><strong>Description<\/strong>:<\/p>\n<ul>\n<li>In Step 1, we have created objects of ArrayList, LinkedList and Vector class, we have defined these objects to store value of String type.<\/li>\n<\/ul>\n<ul>\n<li>In Step 2, we have used add method to store values in the data structures that we have created in step 1.<\/li>\n<\/ul>\n<ul>\n<li>In Step 3, we have displayed values of these objects.<\/li>\n<\/ul>\n<hr \/>\n<h4><strong>Important Points Of List Interface:<\/strong><\/h4>\n<ul>\n<li>List has three implementation classes Vector, LinkedList and ArrayList. If we have more requirement of just retrieving and setting the elements than ArrayList should be preferred, or if we have more requirement of adding and removing the elements (or we can say modification) than LinkedList should be preferred.<\/li>\n<li>List allows duplicate values in it.<\/li>\n<li>List allows null values to be stored.<\/li>\n<li>Some of the methods throw UnsupportedOperationException if the given collection cannot be modified.<\/li>\n<li>Some of the methods throw ClassCastException if the given object is not compatible with other.<\/li>\n<\/ul>\n<hr \/>\n<h4><strong>Importance of List Interface:<\/strong><\/h4>\n<ul>\n<li>As List is dynamic in nature, it grows and shrinks according to the requirement. So it is very helpful when we don\u2019t know capacity of our data structure in advance, we can just declare a List with some initial capacity that automatically increases or decreases its size when elements are added or removed respectively.<\/li>\n<li>Similar to Arrays we have various methods to access elements very smoothly using index, we have three implementations of List Interface that are Vectors, LinkedList and ArrayList, which can be used according to the requirement.<\/li>\n<\/ul>\n<hr \/>\n<h4><strong>List Quick Revision Points:<\/strong><\/h4>\n<ul>\n<li>Size increases dynamically.<\/li>\n<li>Duplicate values are allowed.<\/li>\n<li>Null value is allowed.<\/li>\n<li>Arraylist is preferred when getting and setting elements is required in access.<\/li>\n<li>LinkedList is preferred when modifications are required in access.<\/li>\n<li>Vector is a legacy class that is, it is introduced before JDK 1.2.<\/li>\n<\/ul>\n<hr \/>\n<h4><strong>List Methods:<\/strong><\/h4>\n<p><span style=\"color: #008000;\"><strong>1. void add(int index, Object obj)<\/strong><\/span><\/p>\n<p>This method adds specified element at the specified index, moving the entire elements one step up, avoiding any type of overlapping of element.<\/p>\n<p><span style=\"color: #008000;\"><strong>2. boolean addAll(int index, Collection c)<\/strong><\/span><\/p>\n<p>This method adds all\u00a0 elements of Collection c after the specified index, moving the entire elements up, avoiding any type of overlapping, This method returns true if the operation is successful or else false.<\/p>\n<p><span style=\"color: #008000;\"><strong>3. Object get(int index)<\/strong><\/span><\/p>\n<p>This method retrieves element at the specified index.<\/p>\n<p><span style=\"color: #008000;\"><strong>4. int indexOf(Object obj)<\/strong><\/span><\/p>\n<p>This method retrieves the first occurrence of the given element in the List or it will return -1 if there is no such elements.<\/p>\n<p><span style=\"color: #008000;\"><strong>5. int lastIndexOf(Object obj)<\/strong><\/span><\/p>\n<p>This method retrieves the last occurrence of the given element in the List or it will return -1 if there is no such elements.<\/p>\n<p><span style=\"color: #008000;\"><strong>6. ListIterator listIterator( )<\/strong><\/span><\/p>\n<p>This method gives object of ListIterator from the index 0 of the list, to traverse all the elements from the start.<\/p>\n<p><span style=\"color: #008000;\"><strong>7. ListIterator listIterator(int index)<\/strong><\/span><\/p>\n<p>This method gives object of ListIterator starting from the specified index of the list to traverse all the elements from the start of that index.<\/p>\n<p><span style=\"color: #008000;\"><strong>8. Object remove(int index)<\/strong><\/span><\/p>\n<p>This method removes an element from the list at specified index, In addition to removing this method also returns removed element. After deleting that element indexes of subsequent elements in the list are decreased by 1.<\/p>\n<p><span style=\"color: #008000;\"><strong>9. Object set(int index, Object obj)<\/strong><\/span><\/p>\n<p>This method sets the element at the specified index, if there is already any element present than that element is replaced by the new one.<\/p>\n<p><span style=\"color: #008000;\"><strong>10. List subList(int start, int end)<\/strong><\/span><\/p>\n<p>This method returns the sub-list from the index \u201cstart\u201d to index \u201cend\u201d.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>List is the sequence of elements, or we can say collection of elements, on which user has precise control over where an elements are inserted. These elements can be accessed by their index value (Starting from 0) which is of Integer type and with the help of this index, user can search any element easily. &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/list\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">List Interface Tutorial In Java With Example<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"footnotes":""},"class_list":["post-736","page","type-page","status-publish","hentry"],"psp_head":"<title>List Interface Tutorial In Java With Example \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Tutorial on List Interface which is collection of elements with example and code in JAVA. Also find details of List methods, implementation and its importance.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/list\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/736","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/comments?post=736"}],"version-history":[{"count":1,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/736\/revisions"}],"predecessor-version":[{"id":1484,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/736\/revisions\/1484"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}