{"id":1253,"date":"2022-02-26T06:53:39","date_gmt":"2022-02-26T06:53:39","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1253"},"modified":"2022-02-26T06:53:39","modified_gmt":"2022-02-26T06:53:39","slug":"java-list","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/26\/java-list\/","title":{"rendered":"Java List"},"content":{"rendered":"\n<p>In this tutorial, we will learn about the List interface in Java and its methods.<\/p>\n\n\n\n<p id=\"introduction\">In Java, the&nbsp;<code>List<\/code>&nbsp;interface is an ordered collection that allows us to store and access elements sequentially. It extends the&nbsp;<code>Collection<\/code>&nbsp;interface.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"implementation\">Classes that Implement List<\/h2>\n\n\n\n<p>Since&nbsp;<code>List<\/code>&nbsp;is an interface, we cannot create objects from it.<\/p>\n\n\n\n<p>In order to use functionalities of the&nbsp;<code>List<\/code>&nbsp;interface, we can use these classes:<\/p>\n\n\n\n<ul><li>ArrayList<\/li><li>LinkedList<\/li><li>Vector<\/li><li>Stack<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/Java-list-interface.png\" alt=\"Classes implementing the List interface in Java\" title=\"Java List interface\"\/><\/figure>\n\n\n\n<p>These classes are defined in the Collections framework and implement the&nbsp;<code>List<\/code>&nbsp;interface.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"use-list\">How to use List?<\/h2>\n\n\n\n<p>In Java, we must import&nbsp;<code>java.util.List<\/code>&nbsp;package in order to use&nbsp;<code>List<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ ArrayList implementation of List\nList&lt;String&gt; list1 = new ArrayList&lt;&gt;();\n\n\/\/ LinkedList implementation of List\nList&lt;String&gt; list2 = new LinkedList&lt;&gt;();\n<\/code><\/pre>\n\n\n\n<p>Here, we have created objects&nbsp;<var>list1<\/var>&nbsp;and&nbsp;<var>list2<\/var>&nbsp;of classes&nbsp;<code>ArrayList<\/code>&nbsp;and&nbsp;<code>LinkedList<\/code>. These objects can use the functionalities of the&nbsp;<code>List<\/code>&nbsp;interface.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"methods\">Methods of List<\/h2>\n\n\n\n<p>The&nbsp;<code>List<\/code>&nbsp;interface includes all the methods of the&nbsp;<code>Collection<\/code>&nbsp;interface. Its because&nbsp;<code>Collection<\/code>&nbsp;is a super interface of&nbsp;<code>List<\/code>.<\/p>\n\n\n\n<p>Some of the commonly used methods of the&nbsp;<code>Collection<\/code>&nbsp;interface that&#8217;s also available in the&nbsp;<code>List<\/code>&nbsp;interface are:<\/p>\n\n\n\n<ul><li><code>add()<\/code>&nbsp;&#8211; adds an element to a list<\/li><li><code>addAll()<\/code>&nbsp;&#8211; adds all elements of one list to another<\/li><li><code>get()<\/code>&nbsp;&#8211; helps to randomly access elements from lists<\/li><li><code>iterator()<\/code>&nbsp;&#8211; returns iterator object that can be used to sequentially access elements of lists<\/li><li><code>set()<\/code>&nbsp;&#8211; changes elements of lists<\/li><li><code>remove()<\/code>&nbsp;&#8211; removes an element from the list<\/li><li><code>removeAll()<\/code>&nbsp;&#8211; removes all the elements from the list<\/li><li><code>clear()<\/code>&nbsp;&#8211; removes all the elements from the list (more efficient than&nbsp;<code>removeAll()<\/code>)<\/li><li><code>size()<\/code>&nbsp;&#8211; returns the length of lists<\/li><li><code>toArray()<\/code>&nbsp;&#8211; converts a list into an array<\/li><li><code>contains()<\/code>&nbsp;&#8211; returns&nbsp;<code>true<\/code>&nbsp;if a list contains specified element<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"implementation-classes\">Implementation of the List Interface<\/h2>\n\n\n\n<p><strong>1. Implementing the ArrayList Class<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.List;\nimport java.util.ArrayList;\n\nclass Main {\n\n    public static void main(String&#91;] args) {\n        \/\/ Creating list using the ArrayList class\n        List&lt;Integer&gt; numbers = new ArrayList&lt;&gt;();\n\n        \/\/ Add elements to the list\n        numbers.add(1);\n        numbers.add(2);\n        numbers.add(3);\n        System.out.println(\"List: \" + numbers);\n\n        \/\/ Access element from the list\n        int number = numbers.get(2);\n        System.out.println(\"Accessed Element: \" + number);\n\n        \/\/ Remove element from the list\n        int removedNumber = numbers.remove(1);\n        System.out.println(\"Removed Element: \" + removedNumber);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>List: &#91;1, 2, 3]\nAccessed Element: 3\nRemoved Element: 2\n<\/samp><\/code><\/pre>\n\n\n\n<p>To learn more about&nbsp;<code>ArrayList<\/code>, visit&nbsp;<a href=\"https:\/\/www.programiz.com\/java-programming\/arraylist\">Java ArrayList<\/a>.<\/p>\n\n\n\n<p><strong>2. Implementing the LinkedList Class<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.List;\nimport java.util.LinkedList;\n\nclass Main {\n\n    public static void main(String&#91;] args) {\n        \/\/ Creating list using the LinkedList class\n        List&lt;Integer&gt; numbers = new LinkedList&lt;&gt;();\n\n        \/\/ Add elements to the list\n        numbers.add(1);\n        numbers.add(2);\n        numbers.add(3);\n        System.out.println(\"List: \" + numbers);\n\n        \/\/ Access element from the list\n        int number = numbers.get(2);\n        System.out.println(\"Accessed Element: \" + number);\n\n        \/\/ Using the indexOf() method\n        int index = numbers.indexOf(2);\n        System.out.println(\"Position of 3 is \" + index);\n\n        \/\/ Remove element from the list\n        int removedNumber = numbers.remove(1);\n        System.out.println(\"Removed Element: \" + removedNumber);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>List: &#91;1, 2, 3]\nAccessed Element: 3\nPosition of 3 is 1\nRemoved Element: 2\n<\/samp><\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"vs-set\">Java List vs. Set<\/h2>\n\n\n\n<p>Both the&nbsp;<code>List<\/code>&nbsp;interface and the&nbsp;<code>Set<\/code>&nbsp;interface inherits the&nbsp;<code>Collection<\/code>&nbsp;interface. However, there exists some difference between them.<\/p>\n\n\n\n<ul><li>Lists can include duplicate elements. However, sets cannot have duplicate elements.<\/li><li>Elements in lists are stored in some order. However, elements in sets are stored in groups like sets in mathematics.<\/li><\/ul>\n\n\n\n<p>Now that we know what\u00a0<code>List<\/code>\u00a0is, we will see its implementations in\u00a0<code>ArrayList<\/code>\u00a0and\u00a0<code>LinkedList<\/code>\u00a0classes in detail in the next tutorials.<a href=\"https:\/\/www.programiz.com\/java-programming\/collection-interface\"><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about the List interface in Java and its methods. In Java, the&nbsp;List&nbsp;interface is an ordered collection that allows us to store and access elements sequentially. It extends the&nbsp;Collection&nbsp;interface. Classes that Implement List Since&nbsp;List&nbsp;is an interface, we cannot create objects from it. In order to use functionalities of the&nbsp;List&nbsp;interface, we [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[584],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1253"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1253"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1253\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}