{"id":689,"date":"2016-03-02T05:27:16","date_gmt":"2016-03-02T05:27:16","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?page_id=689"},"modified":"2018-06-05T06:34:43","modified_gmt":"2018-06-05T06:34:43","slug":"linkedhashset","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/java\/linkedhashset","title":{"rendered":"LinkedHashSet Tutorials In Java With Example"},"content":{"rendered":"<p>LinkedHashSet is a type of Collection, which takes all the functionalities of HashSet class, that it does not allow duplicate elements to be stored\u00a0and\u00a0allow null elements in it.<\/p>\n<p>As LinkedHashSet extends HashSet class so it also indirectly extends AbstractSet Class And implements Set Interface.<\/p>\n<p>Everything in LinkedHashSet including the basic structure that we get from AbstractSet class, all the methods that are defined in HashSet is extended from its parent class.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> Hash set does not maintains the insertion order, that is when we retrieve values from it we do not get that values in the same order we have entered in it.\u00a0So, The functionality of maintaining the insertion order is added into LinkedHashSet and to attain this\u00a0 functionality it uses doubly-linked list. This doubly-linked list maintains the iteration ordering, which is in general the order in which values are added in\u00a0 the set.\u00a0Apart from adding this functionality of maintaining insertion order, it do not add any new method of its own.<\/p>\n<hr \/>\n<h4><strong>Hierarchy of <\/strong><strong>Linked<\/strong><strong>HashSet Class<\/strong><\/h4>\n<p>LinkedHashSet Class extends HashSet Class which further extends AbstractSet Class and implements Set Interface. Set Interface further extends Collection Interface and Iterable Interface, as shown in figure below:<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-955\" src=\"\/java\/wp-content\/uploads\/2016\/02\/Hierarchy-of-LinkedHashSet-class.png\" alt=\"Hierarchy of LinkedHashSet class\" width=\"223\" height=\"519\" srcset=\"https:\/\/abhiandroid.com\/java\/wp-content\/uploads\/2016\/02\/Hierarchy-of-LinkedHashSet-class.png 249w, https:\/\/abhiandroid.com\/java\/wp-content\/uploads\/2016\/02\/Hierarchy-of-LinkedHashSet-class-129x300.png 129w\" sizes=\"auto, (max-width: 223px) 100vw, 223px\" \/><\/center><\/p>\n<hr \/>\n<h4><strong style=\"line-height: 1.6471;\">Example of LinkedHashSet Class<\/strong><\/h4>\n<p><span style=\"line-height: 1.6471;\">Let us discuss LinkedHashset class with the help of program, following program has been divided into 3 Steps that we will discuss one by one:<\/span><\/p>\n<pre>import java.util.*;\r\n\r\npublic class LinkedHashSetDemo\r\n{\r\n\r\n  public static void main(String args[]){\r\n\/\/Step 1:\r\n\r\nLinkedHashSet&lt;String&gt; linkedHashSetObject1= new LinkedHashSet&lt;String&gt;(); \r\n \r\nLinkedHashSet&lt;Integer&gt; linkedHashSetObject2= new LinkedHashSet&lt;Integer&gt;();\r\n\r\n \/\/Step 2: \r\n\r\n  linkedHashSetObject1.add(\"I\"); \r\n\r\n  linkedHashSetObject1.add(\"Love\");\r\n\r\n  linkedHashSetObject1.add(\"Java\");\r\n\r\n  linkedHashSetObject1.add(\"Java\");\r\n\r\n  linkedHashSetObject1.add(\"I\");\r\n\r\n  linkedHashSetObject2.add(9);\r\n\r\n  linkedHashSetObject2.add(3);\r\n\r\n  linkedHashSetObject2.add(4);\r\n\r\n  linkedHashSetObject2.add(9);\r\n\r\n  linkedHashSetObject2.add(5);\r\n\r\n  linkedHashSetObject2.add(9);\r\n\r\n\/\/step 3:\r\nSystem.out.println(\"Values in Linked HashSet String object are:\"  +linkedHashSetObject1); \r\n\r\nSystem.out.println(\"Values in Linked HashSet Inteeger object are:\"\r\n\r\n+linkedHashSetObject2);\r\n\r\n }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Values in Linked HashSet String object are:[I, Love, Java]\r\n\r\nValues in Linked HashSet Inteeger object are:[9, 3, 4, 5]<\/pre>\n<p><span style=\"color: #008000;\"><strong>Description:<\/strong><\/span><\/p>\n<p>From output, it is clear that linked hash set maintains insertion order, and do not allow duplicate values.<\/p>\n<ul>\n<li>In Step 1, we have created two objects of LinkedHashSet collection, we have defined these objects to store value of String type and Integer 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 printed values of these objects, and found that linkedHashSet does not store duplicate values, and maintains insertion order.<\/li>\n<\/ul>\n<hr \/>\n<h4>\u00a0<strong>Important Points About LinkedHashSet<\/strong><\/h4>\n<ul>\n<li>LinkedHashSet is similar to HashSet that only stores unique values, that is duplicate values are not allowed.<\/li>\n<li>LinkedHashSet maintains insertion order, which means it returns the elements in the order in which they are added.<\/li>\n<li>It does not do any kind of sorting to the stored values.<\/li>\n<li>LinkedHashSet is similar to HashSet which allows only one null value in it, As duplicates are not allowed.<\/li>\n<li>LinkedHashSet uses hash table to store the values.<\/li>\n<\/ul>\n<hr \/>\n<h4>\u00a0<strong>Quick Revision Points About LinkedHashSet<\/strong><\/h4>\n<ul>\n<li>It Maintains Insertion Order using Doubly Linked List.<\/li>\n<li>No duplicate values are allowed.<\/li>\n<li>Only one null value can be stored.<\/li>\n<li>Uses HashTable to store values.<\/li>\n<li>No new Method is added in this class, all are extended from HashSet.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>LinkedHashSet is a type of Collection, which takes all the functionalities of HashSet class, that it does not allow duplicate elements to be stored\u00a0and\u00a0allow null elements in it. As LinkedHashSet extends HashSet class so it also indirectly extends AbstractSet Class And implements Set Interface. Everything in LinkedHashSet including the basic structure that we get from &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/linkedhashset\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">LinkedHashSet Tutorials In Java With Example<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"home.php","meta":{"footnotes":""},"class_list":["post-689","page","type-page","status-publish","hentry"],"psp_head":"<title>LinkedHashSet Tutorials In Java With Example \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"LinkedHashSet is a type of Collection which takes all the functionalities of HashSet class and maintains the insertion order using doubly-linked list.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/linkedhashset\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/689","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=689"}],"version-history":[{"count":1,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/689\/revisions"}],"predecessor-version":[{"id":1482,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/689\/revisions\/1482"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}