{"id":1249,"date":"2022-02-26T06:47:54","date_gmt":"2022-02-26T06:47:54","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1249"},"modified":"2022-02-26T06:47:54","modified_gmt":"2022-02-26T06:47:54","slug":"collections-framework","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/26\/collections-framework\/","title":{"rendered":"Collections Framework"},"content":{"rendered":"\n<p>In this tutorial, we will learn about different interfaces of the Java collections framework.<\/p>\n\n\n\n<p>The Java&nbsp;<strong>collections<\/strong>&nbsp;framework provides a set of interfaces and classes to implement various data structures and algorithms.<\/p>\n\n\n\n<p>For example, the&nbsp;<code>LinkedList<\/code>&nbsp;class of the collections framework provides the implementation of the doubly-linked list data structure.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"interfaces\">Interfaces of Collections FrameWork<\/h2>\n\n\n\n<p>The Java collections framework provides various interfaces. These interfaces include several methods to perform different operations on collections.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/Java-Collections.png\" alt=\"Interfaces in the Java Collections Framework\"\/><\/figure>\n\n\n\n<p>We will learn about these interfaces, their subinterfaces, and implementation in various classes in detail in the later chapters. Let&#8217;s learn about the commonly used interfaces in brief in this tutorial.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"collection-interface\">Java Collection Interface<\/h2>\n\n\n\n<p>The&nbsp;<code>Collection<\/code>&nbsp;interface is the root interface of the collections framework hierarchy.<\/p>\n\n\n\n<p>Java does not provide direct implementations of the\u00a0<code>Collection<\/code>\u00a0interface but provides implementations of its subinterfaces like\u00a0<code>List<\/code>,\u00a0<code>Set<\/code>, and\u00a0<code>Queue<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Collections Framework Vs. Collection Interface<\/h3>\n\n\n\n<p>People often get confused between the collections framework and&nbsp;<code>Collection<\/code>&nbsp;Interface.<\/p>\n\n\n\n<p>The&nbsp;<code>Collection<\/code>&nbsp;interface is the root interface of the collections framework. The framework includes other interfaces as well:&nbsp;<code>Map<\/code>&nbsp;and&nbsp;<code>Iterator<\/code>. These interfaces may also have subinterfaces.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"subinterfaces\">Subinterfaces of the Collection Interface<\/h2>\n\n\n\n<p>As mentioned earlier, the&nbsp;<code>Collection<\/code>&nbsp;interface includes subinterfaces that are implemented by Java classes.<\/p>\n\n\n\n<p>All the methods of the&nbsp;<code>Collection<\/code>&nbsp;interface are also present in its subinterfaces.<\/p>\n\n\n\n<p>Here are the subinterfaces of the&nbsp;<code>Collection<\/code>&nbsp;Interface:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">List Interface<\/h3>\n\n\n\n<p>The\u00a0<code>List<\/code>\u00a0interface is an ordered collection that allows us to add and remove elements like an array.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Set Interface<\/h3>\n\n\n\n<p>The\u00a0<code>Set<\/code>\u00a0interface allows us to store elements in different sets similar to the set in mathematics. It cannot have duplicate elements.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Queue Interface<\/h3>\n\n\n\n<p>The\u00a0<code>Queue<\/code>\u00a0interface is used when we want to store and access elements in\u00a0<strong>First In, First Out<\/strong>\u00a0manner.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"map\">Java Map Interface<\/h2>\n\n\n\n<p>In Java, the\u00a0<code>Map<\/code>\u00a0interface allows elements to be stored in\u00a0<strong>key\/value<\/strong>\u00a0pairs. Keys are unique names that can be used to access a particular element in a map. And, each key has a single value associated with it.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Java Iterator Interface<\/h2>\n\n\n\n<p>In Java, the\u00a0<code>Iterator<\/code>\u00a0interface provides methods that can be used to access elements of collections.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Why the Collections Framework?<\/h2>\n\n\n\n<p>The Java collections framework provides various data structures and algorithms that can be used directly. This has two main advantages:<\/p>\n\n\n\n<ul><li>We do not have to write code to implement these data structures and algorithms manually.<\/li><li>Our code will be much more efficient as the collections framework is highly optimized.<\/li><\/ul>\n\n\n\n<p>Moreover, the collections framework allows us to use a specific data structure for a particular type of data. Here are a few examples,<\/p>\n\n\n\n<ul><li>If we want our data to be unique, then we can use the&nbsp;<code>Set<\/code>&nbsp;interface provided by the collections framework.<\/li><li>To store data in&nbsp;<strong>key\/value<\/strong>&nbsp;pairs, we can use the&nbsp;<code>Map<\/code>&nbsp;interface.<\/li><li>The&nbsp;<code>ArrayList<\/code>&nbsp;class provides the functionality of resizable arrays.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Example: ArrayList Class of Collections<\/h2>\n\n\n\n<p>Before we wrap up this tutorial, let&#8217;s take an example of the\u00a0ArrayList class\u00a0of the collections framework.<\/p>\n\n\n\n<p>The&nbsp;<code>ArrayList<\/code>&nbsp;class allows us to create resizable arrays. The class implements the&nbsp;<code>List<\/code>&nbsp;interface (which is a subinterface of the&nbsp;<code>Collection<\/code>&nbsp;interface).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ The Collections framework is defined in the java.util package\nimport java.util.ArrayList;\n\nclass Main {\n    public static void main(String&#91;] args){\n        ArrayList&lt;String&gt; animals = new ArrayList&lt;&gt;();\n        \/\/ Add elements\n        animals.add(\"Dog\");\n        animals.add(\"Cat\");\n        animals.add(\"Horse\");\n\n        System.out.println(\"ArrayList: \" + animals);\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><samp>ArrayList: [Dog, Cat, Horse]\n<\/samp><\/pre>\n\n\n\n<p>In the later tutorials, we will learn about the collections framework (its interfaces and classes) in detail with the help of examples.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about different interfaces of the Java collections framework. The Java&nbsp;collections&nbsp;framework provides a set of interfaces and classes to implement various data structures and algorithms. For example, the&nbsp;LinkedList&nbsp;class of the collections framework provides the implementation of the doubly-linked list data structure. Interfaces of Collections FrameWork The Java collections framework provides [&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\/1249"}],"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=1249"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1249\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}