{"id":358861,"date":"2024-05-21T00:20:35","date_gmt":"2024-05-21T00:20:35","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=358861"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-05-21T00:20:35","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=358861","title":{"rendered":"<span>List in Java | Interface, Methods, Example<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>In this topic, we will learn about what is a\u00a0<em>List in Java<\/em>. How to create a List in Java? What are the methods of List in Java? The List is an interface in Java. It is extending the Collection interface in Java. This List interface is present in the\u00a0<strong>java.util\u00a0<\/strong>package in Java. A list represents a group of individual objects as a single entity where duplicates are allowed and insertion order is preserved.<\/p>\n<pre><code class=\"java\">public interface List&lt;E> extends Collection&lt;E><\/code><\/pre>\n<p>\u2192 In the above, we mentioned the general declaration of the List in Java.<\/p>\n<h3>How to create a List in Java?<\/h3>\n<p>The list interface has four concrete subclasses(ArrayList, LinkedList, Vector and Stack) in the Collection Framework.<br \/>We can create the list by using its concrete subclasses as follows:<\/p>\n<pre><code class=\"java\">List l1 = new ArrayList(); List l2 = new LinkedList(); List l3 = new Vector(); List l4= new Stack();<\/code><\/pre>\n<h3>How to create a Generic List object in Java?<\/h3>\n<p>We can create the generic list object as follows:<\/p>\n<pre><code class=\"java\">List&lt;data type> list = new ArrayList&lt;data type>(); <\/code><\/pre>\n<p>\u2192 In the above, we mentioned the general syntax of creating a\u00a0<a href=\"https:\/\/springjava.com\/core-java\/generics-in-java\" rel=\"noopener noreferrer nofollow\"><strong>generic<\/strong><\/a>\u00a0List by using the ArrayList class.<\/p>\n<pre><code class=\"java\">List&lt;String> list = new ArrayList&lt;String>();<\/code><\/pre>\n<p>\u2192 In the above create a list of objects of String type by using ArrayList class.<\/p>\n<pre><code class=\"java\">List&lt;Integer> list = new LinkedList&lt;Integer>();<\/code><\/pre>\n<p>\u2192 In the above example, we created a list of objects of Integer type by using the LinkedList class.\u00a0 \u00a0<\/p>\n<pre><code class=\"java\">List&lt;String> list = new LinkedList&lt;String>();<\/code><\/pre>\n<p>\u2192 In the above example, we created a list of objects of String type by using the LinkedList class.<\/p>\n<pre><code class=\"java\">List&lt;User> list=new ArrayList&lt;User>(); <\/code><\/pre>\n<p>\u2192 In the above example, we created a list of objects of type obj by using the ArrayList class. The User\u00a0is the type object.<\/p>\n<pre><code class=\"java\"> List&lt;String> strList = new ArrayList&lt;>();  List&lt;Integer> list = new LinkedList&lt;>();<\/code><\/pre>\n<p>\u2192 Java 1.7 onwards, we can use a diamond operator<\/p>\n<h3>What are the Methods of List?<\/h3>\n<p>The List interface has several methods:\u00a0<br \/>\u00a0\u00a0<strong>\u2022 void add(int index, E element):\u00a0<\/strong>This method is used to insert an object at a particular position in the list.<br \/>\u00a0 \u00a0 \u00a0\u00a0<strong>E<\/strong>&#8212; It is a type of element in the list<br \/>For example, suppose we want to add \u201cPeter\u201d at the 4th position in the list we can use this method as below:<\/p>\n<pre><code class=\"java\">list.add(4,\"Peter\");<\/code><\/pre>\n<p>\u00a0 \u2022\u00a0<strong>boolean addAll(int index, Collection&lt;? extends E> c):<\/strong>\u00a0This method is used to add a Collection object at the particular position in the list and shift the subsequent objects to the right by increasing their indices.<br \/>For example, suppose we want to add a list at the 2nd position in the other list we can use this method as below:<\/p>\n<pre><code class=\"java\">list.add(\"P\"); list.add(\"Q\"); list.add(\"R\");<\/code><\/pre>\n<p>Now, we will create a new list using list1 reference.<\/p>\n<pre><code class=\"java\">list1.add(\"W\"); list1.add(\"Y\");<\/code><\/pre>\n<p>Now, we will add list1 at the 2nd position in the list.<\/p>\n<pre><code class=\"java\">list.add(2,list2);<\/code><\/pre>\n<p>\u00a0\u00a0<strong>\u2022 E remove(int index):<\/strong>\u00a0This method is used to remove an element at a specified position in the list.<br \/>For example, we consider the following:<\/p>\n<pre><code class=\"java\">list.remove(2);<\/code><\/pre>\n<p><strong>\u00a0 \u2022 E get(int index):<\/strong>\u00a0This method is used to return an object stored at a specified position in the list.<br \/>For example, we consider the following:<\/p>\n<pre><code class=\"java\">list.get(1);<\/code><\/pre>\n<p>\u00a0\u00a0<strong>\u2022 E set(int index, E element):\u00a0<\/strong>This method is used to replace an existing element with a new element with a specified position in the list.<br \/>For example, we consider the following:<\/p>\n<pre><code class=\"java\">list.set(1, \"T\");<\/code><\/pre>\n<p>\u00a0\u00a0<strong>\u2022 int indexOf(Object o):<\/strong>\u00a0This method is used to return an index of the particular element of the first occurrence in the list. If the specified element is not present in the list then will return -1.<br \/>For example, we consider the following:<\/p>\n<pre><code class=\"java\">list.indexOf(\"A\");<\/code><\/pre>\n<p>\u00a0\u00a0<strong>\u2022 int lastIndexOf(Object o):\u00a0<\/strong>This method is used to return an index of the particular element of the last occurrence in the list. If the specified element is not present in the list then will return -1.<br \/>For example, we consider the following:<\/p>\n<pre><code class=\"java\">list.lastIndexOf(\"A\");<\/code><\/pre>\n<p>\u00a0\u00a0<strong>\u2022 ListIterator&lt;E> listIterator():<\/strong>\u00a0This method is used to return the listIterator of the elements in the list in a proper sequence.<br \/>\u00a0\u00a0<strong>\u2022 ListIterator&lt;E> listIterator(int index):\u00a0<\/strong>This method is used to return a listIterator of the elements in the list in the proper sequence, starting at the specified position in the list.<\/p>\n<h3>Example of Create a List in Java<\/h3>\n<pre><code class=\"java\">import java.util.ArrayList;  import java.util.List;  public class Test {  public static void main(String[] args){  List&lt;String> al = new ArrayList&lt;String>(); al.add(\"Apple\");  al.add(\"Mango\");  al.add(\"Orange\");  al.add(\"Grapes\");  System.out.println(\"List1 contains: \" +al);  List&lt;String> al2 = new ArrayList&lt;String>();  al2.add(\"10\");  al2.add(\"11\");  al2.add(\"12\");  System.out.println(\"List2 contains : \"+al2);  al.addAll(2, al2);  System.out.println(\"After added List2 at 2nd index in the list: \" +al);    }  }<\/code><\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre><code class=\"bash\">List1 contains: [Apple, Mango, Orange, Grapes] List2 contains : [10, 11, 12] List1 after adding List2 at 2nd index: [Apple, Mango, 10, 11, 12, Orange, Grapes]<\/code><\/pre>\n<h3>How to Iterate List in Java?<\/h3>\n<p>There are different ways to iterate a list in Java. You can refer to the post by\u00a0<a href=\"https:\/\/springjava.com\/core-java\/how-to-iterate-list-in-java\" rel=\"noopener noreferrer nofollow\"><strong>clicking here<\/strong><\/a>.\u00a0<\/p>\n<h3>When to use List in Java?<\/h3>\n<p>There are the following points where we will use List:<br \/>\u00a0 \u2022 When we want to need duplicate elements to store<br \/>\u00a0 \u2022 When we want to allow null elements<br \/>\u00a0 \u2022 When we want to preserve the insertion order<\/p>\n<h4>Conclusion<\/h4>\n<p>In this topic, we learnt about\u00a0<strong>what is a List Interface in Java and its various methods<\/strong>.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/770790\/\"> https:\/\/habr.com\/ru\/articles\/770790\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>In this topic, we will learn about what is a\u00a0<em>List in Java<\/em>. How to create a List in Java? What are the methods of List in Java? The List is an interface in Java. It is extending the Collection interface in Java. This List interface is present in the\u00a0<strong>java.util\u00a0<\/strong>package in Java. A list represents a group of individual objects as a single entity where duplicates are allowed and insertion order is preserved.<\/p>\n<pre><code class=\"java\">public interface List&lt;E> extends Collection&lt;E><\/code><\/pre>\n<p>\u2192 In the above, we mentioned the general declaration of the List in Java.<\/p>\n<h3>How to create a List in Java?<\/h3>\n<p>The list interface has four concrete subclasses(ArrayList, LinkedList, Vector and Stack) in the Collection Framework.<br \/>We can create the list by using its concrete subclasses as follows:<\/p>\n<pre><code class=\"java\">List l1 = new ArrayList(); List l2 = new LinkedList(); List l3 = new Vector(); List l4= new Stack();<\/code><\/pre>\n<h3>How to create a Generic List object in Java?<\/h3>\n<p>We can create the generic list object as follows:<\/p>\n<pre><code class=\"java\">List&lt;data type> list = new ArrayList&lt;data type>(); <\/code><\/pre>\n<p>\u2192 In the above, we mentioned the general syntax of creating a\u00a0<a href=\"https:\/\/springjava.com\/core-java\/generics-in-java\" rel=\"noopener noreferrer nofollow\"><strong>generic<\/strong><\/a>\u00a0List by using the ArrayList class.<\/p>\n<pre><code class=\"java\">List&lt;String> list = new ArrayList&lt;String>();<\/code><\/pre>\n<p>\u2192 In the above create a list of objects of String type by using ArrayList class.<\/p>\n<pre><code class=\"java\">List&lt;Integer> list = new LinkedList&lt;Integer>();<\/code><\/pre>\n<p>\u2192 In the above example, we created a list of objects of Integer type by using the LinkedList class.\u00a0 \u00a0<\/p>\n<pre><code class=\"java\">List&lt;String> list = new LinkedList&lt;String>();<\/code><\/pre>\n<p>\u2192 In the above example, we created a list of objects of String type by using the LinkedList class.<\/p>\n<pre><code class=\"java\">List&lt;User> list=new ArrayList&lt;User>(); <\/code><\/pre>\n<p>\u2192 In the above example, we created a list of objects of type obj by using the ArrayList class. The User\u00a0is the type object.<\/p>\n<pre><code class=\"java\"> List&lt;String> strList = new ArrayList&lt;>();  List&lt;Integer> list = new LinkedList&lt;>();<\/code><\/pre>\n<p>\u2192 Java 1.7 onwards, we can use a diamond operator<\/p>\n<h3>What are the Methods of List?<\/h3>\n<p>The List interface has several methods:\u00a0<br \/>\u00a0\u00a0<strong>\u2022 void add(int index, E element):\u00a0<\/strong>This method is used to insert an object at a particular position in the list.<br \/>\u00a0 \u00a0 \u00a0\u00a0<strong>E<\/strong>&#8212; It is a type of element in the list<br \/>For example, suppose we want to add \u201cPeter\u201d at the 4th position in the list we can use this method as below:<\/p>\n<pre><code class=\"java\">list.add(4,\"Peter\");<\/code><\/pre>\n<p>\u00a0 \u2022\u00a0<strong>boolean addAll(int index, Collection&lt;? extends E> c):<\/strong>\u00a0This method is used to add a Collection object at the particular position in the list and shift the subsequent objects to the right by increasing their indices.<br \/>For example, suppose we want to add a list at the 2nd position in the other list we can use this method as below:<\/p>\n<pre><code class=\"java\">list.add(\"P\"); list.add(\"Q\"); list.add(\"R\");<\/code><\/pre>\n<p>Now, we will create a new list using list1 reference.<\/p>\n<pre><code class=\"java\">list1.add(\"W\"); list1.add(\"Y\");<\/code><\/pre>\n<p>Now, we will add list1 at the 2nd position in the list.<\/p>\n<pre><code class=\"java\">list.add(2,list2);<\/code><\/pre>\n<p>\u00a0\u00a0<strong>\u2022 E remove(int index):<\/strong>\u00a0This method is used to remove an element at a specified position in the list.<br \/>For example, we consider the following:<\/p>\n<pre><code class=\"java\">list.remove(2);<\/code><\/pre>\n<p><strong>\u00a0 \u2022 E get(int index):<\/strong>\u00a0This method is used to return an object stored at a specified position in the list.<br \/>For example, we consider the following:<\/p>\n<pre><code class=\"java\">list.get(1);<\/code><\/pre>\n<p>\u00a0\u00a0<strong>\u2022 E set(int index, E element):\u00a0<\/strong>This method is used to replace an existing element with a new element with a specified position in the list.<br \/>For example, we consider the following:<\/p>\n<pre><code class=\"java\">list.set(1, \"T\");<\/code><\/pre>\n<p>\u00a0\u00a0<strong>\u2022 int indexOf(Object o):<\/strong>\u00a0This method is used to return an index of the particular element of the first occurrence in the list. If the specified element is not present in the list then will return -1.<br \/>For example, we consider the following:<\/p>\n<pre><code class=\"java\">list.indexOf(\"A\");<\/code><\/pre>\n<p>\u00a0\u00a0<strong>\u2022 int lastIndexOf(Object o):\u00a0<\/strong>This method is used to return an index of the particular element of the last occurrence in the list. If the specified element is not present in the list then will return -1.<br \/>For example, we consider the following:<\/p>\n<pre><code class=\"java\">list.lastIndexOf(\"A\");<\/code><\/pre>\n<p>\u00a0\u00a0<strong>\u2022 ListIterator&lt;E> listIterator():<\/strong>\u00a0This method is used to return the listIterator of the elements in the list in a proper sequence.<br \/>\u00a0\u00a0<strong>\u2022 ListIterator&lt;E> listIterator(int index):\u00a0<\/strong>This method is used to return a listIterator of the elements in the list in the proper sequence, starting at the specified position in the list.<\/p>\n<h3>Example of Create a List in Java<\/h3>\n<pre><code class=\"java\">import java.util.ArrayList;  import java.util.List;  public class Test {  public static void main(String[] args){  List&lt;String> al = new ArrayList&lt;String>(); al.add(\"Apple\");  al.add(\"Mango\");  al.add(\"Orange\");  al.add(\"Grapes\");  System.out.println(\"List1 contains: \" +al);  List&lt;String> al2 = new ArrayList&lt;String>();  al2.add(\"10\");  al2.add(\"11\");  al2.add(\"12\");  System.out.println(\"List2 contains : \"+al2);  al.addAll(2, al2);  System.out.println(\"After added List2 at 2nd index in the list: \" +al);    }  }<\/code><\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre><code class=\"bash\">List1 contains: [Apple, Mango, Orange, Grapes] List2 contains : [10, 11, 12] List1 after adding List2 at 2nd index: [Apple, Mango, 10, 11, 12, Orange, Grapes]<\/code><\/pre>\n<h3>How to Iterate List in Java?<\/h3>\n<p>There are different ways to iterate a list in Java. You can refer to the post by\u00a0<a href=\"https:\/\/springjava.com\/core-java\/how-to-iterate-list-in-java\" rel=\"noopener noreferrer nofollow\"><strong>clicking here<\/strong><\/a>.\u00a0<\/p>\n<h3>When to use List in Java?<\/h3>\n<p>There are the following points where we will use List:<br \/>\u00a0 \u2022 When we want to need duplicate elements to store<br \/>\u00a0 \u2022 When we want to allow null elements<br \/>\u00a0 \u2022 When we want to preserve the insertion order<\/p>\n<h4>Conclusion<\/h4>\n<p>In this topic, we learnt about\u00a0<strong>what is a List Interface in Java and its various methods<\/strong>.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/770790\/\"> https:\/\/habr.com\/ru\/articles\/770790\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-358861","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/358861","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=358861"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/358861\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=358861"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=358861"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=358861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}