{"id":2543,"date":"2020-04-14T23:27:06","date_gmt":"2020-04-14T17:57:06","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=2543"},"modified":"2020-04-14T23:48:32","modified_gmt":"2020-04-14T18:18:32","slug":"introduction-to-collections","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/introduction-to-collections\/","title":{"rendered":"Introduction to Collections"},"content":{"rendered":"<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">A Collection is a classification of individual objects which represents as a single unit.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">As we say, <a href=\"https:\/\/www.h2kinfosys.com\/courses\/java-online-training-course-details\">Java <\/a>also provides Collection Framework, which will define the several classes and interfaces to describes a group of the object as a single unit.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">There is two main root Collection interface of java classes are: Collection interface (<\/span><b>java.util.Collection<\/b><span style=\"font-weight: 400;\">) and the Map interface (<\/span><b>java.util.Map<\/b><span style=\"font-weight: 400;\">).<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Collections in java are also provided an architecture to store and manipulate the group of objects.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">It can also achieve all the operations that you perform on the data like searching, sorting, insertion, manipulation, and deletion.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">It provides many interfaces (Set, List, Queue, Deque) and classes (<\/span><span style=\"font-weight: 400;\">ArrayList<\/span><span style=\"font-weight: 400;\">, Vector,\u00a0<\/span><span style=\"font-weight: 400;\">LinkedList<\/span><span style=\"font-weight: 400;\">,\u00a0<\/span><span style=\"font-weight: 400;\">PriorityQueue<\/span><span style=\"font-weight: 400;\">, HashSet, LinkedHashSet, TreeSet).<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"wp-image-2544 aligncenter\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_170.png\" alt=\"Introduction to Collections\" width=\"682\" height=\"446\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_170.png 903w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_170-300x196.png 300w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2020\/04\/Screenshot_170-768x503.png 768w\" sizes=\"(max-width: 682px) 100vw, 682px\" \/><\/p>\n<p>[box type=&#8221;shadow&#8221; align=&#8221;&#8221; class=&#8221;&#8221; width=&#8221;&#8221;]&#8221;java.util&#8221; package, which contains all the <a href=\"https:\/\/www.h2kinfosys.com\/blog\/interfaces-abstract-classes\/\">classes and interfaces<\/a> for the Collection framework.[\/box]<\/p>\n<h3><b>Program: 1 for Collection:<\/b><\/h3>\n<pre>import java.io.*; \r\nimport java.util.*; \r\n \r\nclass TestCollection \r\n{ \r\n public static void main (String[] args) \r\n { \r\n \r\n int arr[] = new int[] {1, 2, 3}; \r\n Vector&lt;Integer&gt; v = new Vector(); \r\n Hashtable&lt;Integer, String&gt; h = new Hashtable(); \r\n v.addElement(1); \r\n v.addElement(2); \r\n h.put(1,\"colllections1\"); \r\n h.put(2,\"colllections2\"); \r\n \r\n System.out.println(arr[0]); System.out.println(v.elementAt(0)); \r\n System.out.println(h.get(1)); \r\n \r\n \r\n } \r\n} \r\n \r\n Output:\r\n1\r\n1\r\ncolllections2<\/pre>\n<p>&nbsp;<\/p>\n<h3><b>Program: 2 for Collection using ArrayList:<\/b><\/h3>\n<pre>import java.util.*; \r\nclass JavaArrayList{ \r\n public static void main(String args[]){ \r\n ArrayList&lt;String&gt; alist=new ArrayList&lt;String&gt;(); \r\n alist.add(\"Steve\");\r\n alist.add(\"Tim\");\r\n alist.add(\"Lucy\");\r\n alist.add(\"Pat\");\r\n alist.add(\"Angela\");\r\n alist.add(\"Tom\");\r\n \r\n \/\/displaying elements\r\n System.out.println(alist);\r\n \r\n \/\/Adding \"Steve\" at the fourth position\r\n alist.add(3, \"Steve\");\r\n \r\n \/\/displaying elements\r\n System.out.println(alist);\r\n } \r\n}\r\nOutput:\r\n \r\n[Steve, Tim, Lucy, Pat, Angela, Tom]\r\n \r\n[Steve, Tim, Lucy, Steve, Pat, Angela, Tom]<\/pre>\n<h3><b>Program: 3 for Collection using ArrayList to Remove Elements:<\/b><\/h3>\n<pre>import java.util.*;\r\nclass JavaRemoveArrayList{\r\n public static void main(String args[]){\r\n ArrayList&lt;String&gt; ralist=new ArrayList&lt;String&gt;(); \r\n ralist.add(\"Steve\");\r\n ralist.add(\"Tim\");\r\n ralist.add(\"Lucy\");\r\n ralist.add(\"Pat\");\r\n ralist.add(\"Angela\");\r\n ralist.add(\"Tom\");\r\n \r\n \r\n System.out.println(ralist);\r\n \r\n \/\/Removing \"Steve\" and \"Angela\"\r\n ralist.remove(\"Steve\");\r\n ralist.remove(\"Angela\");\r\n \r\n \/\/displaying elements\r\n System.out.println(ralist);\r\n \r\n \/\/Removing 3rd element\r\n ralist.remove(2);\r\n \r\n \/\/displaying elements\r\n System.out.println(ralist);\r\n }\r\n}\r\nOutput:\r\n[Steve, Tim, Lucy, Pat, Angela, Tom]\r\n[Tim, Lucy, Pat, Tom]\r\n[Tim, Lucy, Tom]<\/pre>\n<h3><b>Program: 4 for Collection using Queue<br \/>\n<\/b><\/h3>\n<pre>public class QueueDemo {\r\n public static void main(String[] args) {\r\n \r\n Queue&lt;String&gt; que = new LinkedList&lt;&gt;();\r\n que .add(\"one\");\r\n que .add(\"two\");\r\n que .add(\"three\");\r\n que .add(\"four\");\r\n System.out.println(que );\r\n \r\n que .remove(\"three\");\r\n System.out.println(que );\r\n System.out.println(\"Queue Size: \" + que .size());\r\n System.out.println(\"Queue Contains element 'two' or not? : \" + que .contains(\"two\"));\r\n \r\n \/\/ To empty the queue\r\n que .clear();\r\n }\r\n}\r\nOutput:-\r\n \r\n \r\n[one, two, three, four]\r\n[one, two, four]\r\nQueue Size: 3\r\nQueue Contains element 'two' or not? : true<\/pre>\n<p><b>Advantages of Collection Framework:<\/b><\/p>\n<p>&nbsp;<\/p>\n<ol>\n<li style=\"font-weight: 400;\"><b>Consistent API:<\/b> <span style=\"font-weight: 400;\">The API has a generic set of interfaces such as Collection, Set, List, or Map. All classes (ArrayList, LinkedList, Vector, etc.) which implement these interfaces have\u00a0<\/span><span style=\"font-weight: 400;\">some<\/span><span style=\"font-weight: 400;\">\u00a0standard set of methods.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Reduces programming effort<\/b><span style=\"font-weight: 400;\">: Programmer no need to worry about the design of Collection, programmer should focus on its best use in his program.<\/span><\/li>\n<li><b>Increases the program speed and quality:<\/b><span style=\"font-weight: 400;\"> Increases performance by providing high- \u00a0 \u00a0 performance implementations of useful data structures and algorithms.<\/span><\/li>\n<\/ol>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The collections framework is designed to meet all the several goals, such as \u2013<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The framework should be high-performance.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The implementations done for the fundamental collections (dynamic arrays, linked lists, trees, and hashtables) were to be highly structured.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The framework is to allow different types of groups to work in a similar manner and with a high degree of interoperability.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The framework had to extend and adapt a collection easily.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The <a href=\"https:\/\/www.h2kinfosys.com\/blog\/java-language-packages\/\">collections framework<\/a> is an architecture to represents and manipulate the collections.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Here are all the collections frameworks which contain the following: \u2212<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Implementations like Classes<\/b><span style=\"font-weight: 400;\">\u00a0\u2212 These are the concrete implementations of the collection interfaces. In essence, they are reusable data structures. \u00b7\u00a0 \u00a0 \u00a0 \u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Interfaces<\/b><span style=\"font-weight: 400;\">\u00a0\u2212 It allows collections to be manipulated independently of the details of their representation.<\/span><\/li>\n<\/ul>\n<ul>\n<li style=\"font-weight: 400;\"><b>Algorithms<\/b><span style=\"font-weight: 400;\">\u00a0\u2212 It is mostly useful for computations, like searching and sorting, on objects that implement collection interfaces.\u00a0<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">In addition to collections, the framework defines several map interfaces and classes. Maps store key\/value pairs. Although maps are not\u00a0<\/span><i><span style=\"font-weight: 400;\">collections<\/span><\/i><span style=\"font-weight: 400;\">\u00a0in the proper use of the term, they are fully integrated with collections.<\/span><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A Collection is a classification of individual objects which represents as a single unit.\u00a0 As we say, Java also provides Collection Framework, which will define the several classes and interfaces to describes a group of the object as a single unit. There is two main root Collection interface of java classes are: Collection interface (java.util.Collection) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2581,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[509,510,508],"class_list":["post-2543","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorials","tag-arraylist","tag-collection-using-queue","tag-java-collection-framework"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/2543","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=2543"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/2543\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/2581"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=2543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=2543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=2543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}