{"id":1280,"date":"2022-02-26T07:41:12","date_gmt":"2022-02-26T07:41:12","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1280"},"modified":"2022-02-26T07:41:12","modified_gmt":"2022-02-26T07:41:12","slug":"java-enum-set","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/26\/java-enum-set\/","title":{"rendered":"Java Enum Set"},"content":{"rendered":"\n<p>In this tutorial, we will learn about the Java LinkedHashSet class and its methods with the help of examples.<\/p>\n\n\n\n<p>The&nbsp;<code>LinkedHashSet<\/code>&nbsp;class of the Java collections framework provides functionalities of both the hashtable and the linked list data structure.<\/p>\n\n\n\n<p>It implements the\u00a0Set interface.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-linkedhashset.png\" alt=\"Java LinkedHastSet class implements the Set interface.\"\/><\/figure>\n\n\n\n<p>Elements of\u00a0<code>LinkedHashSet<\/code>\u00a0are stored in hash tables similar to\u00a0HashSet.<\/p>\n\n\n\n<p>However, linked hash sets maintain a doubly-linked list internally for all of its elements. The linked list defines the order in which elements are inserted in hash tables.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"create\">Create a LinkedHashSet<\/h2>\n\n\n\n<p>In order to create a linked hash set, we must import the&nbsp;<code>java.util.LinkedHashSet<\/code>&nbsp;package first.<\/p>\n\n\n\n<p>Once we import the package, here is how we can create linked hash sets in Java.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ LinkedHashSet with 8 capacity and 0.75 load factor\nLinkedHashSet&lt;Integer&gt; numbers = new LinkedHashSet&lt;&gt;(8, 0.75);\n<\/code><\/pre>\n\n\n\n<p>Here, we have created a linked hash set named&nbsp;<var>numbers<\/var>.<\/p>\n\n\n\n<p>Notice, the part&nbsp;<code>new LinkedHashSet&lt;&gt;(8, 0.75)<\/code>. Here, the first parameter is&nbsp;<strong>capacity<\/strong>&nbsp;and the second parameter is&nbsp;<strong>loadFactor<\/strong>.<\/p>\n\n\n\n<ul><li><strong>capacity<\/strong>&nbsp;&#8211; The capacity of this hash set is 8. Meaning, it can store 8 elements.<\/li><li><strong>loadFactor<\/strong>&nbsp;&#8211; The load factor of this hash set is 0.6. This means, whenever our hash table is filled by 60%, the elements are moved to a new hash table of double the size of the original hash table.<\/li><\/ul>\n\n\n\n<p><strong>Default capacity and load factor<\/strong><\/p>\n\n\n\n<p>It&#8217;s possible to create a linked hash set without defining its capacity and load factor. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ LinkedHashSet with default capacity and load factor\nLinkedHashSet&lt;Integer&gt; numbers1 = new LinkedHashSet&lt;&gt;();\n<\/code><\/pre>\n\n\n\n<p>By default,<\/p>\n\n\n\n<ul><li>the capacity of the linked hash set will be 16<\/li><li>the load factor will be 0.75<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Creating LinkedHashSet from Other Collections<\/h2>\n\n\n\n<p>Here is how we can create a linked hash set containing all the elements of other collections.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.LinkedHashSet;\nimport java.util.ArrayList;\n\nclass Main {\n    public static void main(String&#91;] args) {\n        \/\/ Creating an arrayList of even numbers\n        ArrayList&lt;Integer&gt; evenNumbers = new ArrayList&lt;&gt;();\n        evenNumbers.add(2);\n        evenNumbers.add(4);\n        System.out.println(\"ArrayList: \" + evenNumbers);\n\n        \/\/ Creating a LinkedHashSet from an ArrayList\n        LinkedHashSet&lt;Integer&gt; numbers = new LinkedHashSet&lt;&gt;(evenNumbers);\n        System.out.println(\"LinkedHashSet: \" + numbers);\n    }\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>ArrayList: &#91;2, 4]\nLinkedHashSet: &#91;2, 4]\n<\/samp><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Methods of LinkedHashSet<\/h2>\n\n\n\n<p>The&nbsp;<code>LinkedHashSet<\/code>&nbsp;class provides methods that allow us to perform various operations on the linked hash set.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"insert\">Insert Elements to LinkedHashSet<\/h2>\n\n\n\n<ul><li><code>add()<\/code>&nbsp;&#8211; inserts the specified element to the linked hash set<\/li><li><code>addAll()<\/code>&nbsp;&#8211; inserts all the elements of the specified collection to the linked hash set<\/li><\/ul>\n\n\n\n<p>For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.LinkedHashSet;\n\nclass Main {\n    public static void main(String&#91;] args) {\n        LinkedHashSet&lt;Integer&gt; evenNumber = new LinkedHashSet&lt;&gt;();\n\n        \/\/ Using add() method\n        evenNumber.add(2);\n        evenNumber.add(4);\n        evenNumber.add(6);\n        System.out.println(\"LinkedHashSet: \" + evenNumber);\n\n        LinkedHashSet&lt;Integer&gt; numbers = new LinkedHashSet&lt;&gt;();\n        \n        \/\/ Using addAll() method\n        numbers.addAll(evenNumber);\n        numbers.add(5);\n        System.out.println(\"New LinkedHashSet: \" + numbers);\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>LinkedHashSet: &#91;2, 4, 6]\nNew LinkedHashSet: &#91;2, 4, 6, 5]\n<\/samp><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"access\">Access LinkedHashSet Elements<\/h2>\n\n\n\n<p>To access the elements of a linked hash set, we can use the&nbsp;<code>iterator()<\/code>&nbsp;method. In order to use this method, we must import the&nbsp;<code>java.util.Iterator<\/code>&nbsp;package. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.LinkedHashSet;\nimport java.util.Iterator;\n\nclass Main {\n    public static void main(String&#91;] args) {\n        LinkedHashSet&lt;Integer&gt; numbers = new LinkedHashSet&lt;&gt;();\n        numbers.add(2);\n        numbers.add(5);\n        numbers.add(6);\n        System.out.println(\"LinkedHashSet: \" + numbers);\n\n        \/\/ Calling the iterator() method\n        Iterator&lt;Integer&gt; iterate = numbers.iterator();\n\n        System.out.print(\"LinkedHashSet using Iterator: \");\n\n        \/\/ Accessing elements\n        while(iterate.hasNext()) {\n            System.out.print(iterate.next());\n            System.out.print(\", \");\n        }\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>LinkedHashSet: &#91;2, 5, 6]\nLinkedHashSet using Iterator: 2, 5, 6,\n<\/samp><\/code><\/pre>\n\n\n\n<p><strong>Note<\/strong>:<\/p>\n\n\n\n<ul><li><code>hasNext()<\/code>&nbsp;returns&nbsp;<code>true<\/code>&nbsp;if there is a next element in the linked hash set<\/li><li><code>next()<\/code>&nbsp;returns the next element in the linked hash set<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"remove\">Remove Elements from HashSet<\/h2>\n\n\n\n<ul><li><code>remove()<\/code>&nbsp;&#8211; removes the specified element from the linked hash set<\/li><li><code>removeAll()<\/code>&nbsp;&#8211; removes all the elements from the linked hash set<\/li><\/ul>\n\n\n\n<p>For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.LinkedHashSet;\n\nclass Main {\n    public static void main(String&#91;] args) {\n        LinkedHashSet&lt;Integer&gt; numbers = new LinkedHashSet&lt;&gt;();\n        numbers.add(2);\n        numbers.add(5);\n        numbers.add(6);\n        System.out.println(\"LinkedHashSet: \" + numbers);\n\n        \/\/ Using the remove() method\n        boolean value1 = numbers.remove(5);\n        System.out.println(\"Is 5 removed? \" + value1);\n\n        boolean value2 = numbers.removeAll(numbers);\n        System.out.println(\"Are all elements removed? \" + value2);\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>LinkedHashSet: &#91;2, 5, 6]\nIs 5 removed? true\nAre all elements removed? true\n<\/samp><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"set-operations\">Set Operations<\/h2>\n\n\n\n<p>The various methods of the&nbsp;<code>LinkedHashSet<\/code>&nbsp;class can also be used to perform various set operations.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Union of Sets<\/h3>\n\n\n\n<p>Two perform the union between two sets, we can use the&nbsp;<code>addAll()<\/code>&nbsp;method. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.LinkedHashSet;\n\nclass Main {\n    public static void main(String&#91;] args) {\n        LinkedHashSet&lt;Integer&gt; evenNumbers = new LinkedHashSet&lt;&gt;();\n        evenNumbers.add(2);\n        evenNumbers.add(4);\n        System.out.println(\"LinkedHashSet1: \" + evenNumbers);\n\n        LinkedHashSet&lt;Integer&gt; numbers = new LinkedHashSet&lt;&gt;();\n        numbers.add(1);\n        numbers.add(3);\n        System.out.println(\"LinkedHashSet2: \" + numbers);\n\n        \/\/ Union of two set\n        numbers.addAll(evenNumbers);\n        System.out.println(\"Union is: \" + numbers);\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>LinkedHashSet1: &#91;2, 4]\nLinkedHashSet2: &#91;1, 3]\nUnion is: &#91;1, 3, 2, 4]\n<\/samp><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Intersection of Sets<\/h3>\n\n\n\n<p>To perform the intersection between two sets, we can use the&nbsp;<code>retainAll()<\/code>&nbsp;method. For example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.LinkedHashSet;\n\nclass Main {\n    public static void main(String&#91;] args) {\n        LinkedHashSet&lt;Integer&gt; primeNumbers = new LinkedHashSet&lt;&gt;();\n        primeNumbers.add(2);\n        primeNumbers.add(3);\n        System.out.println(\"LinkedHashSet1: \" + primeNumbers);\n\n        LinkedHashSet&lt;Integer&gt; evenNumbers = new LinkedHashSet&lt;&gt;();\n        evenNumbers.add(2);\n        evenNumbers.add(4);\n        System.out.println(\"LinkedHashSet2: \" + evenNumbers);\n\n        \/\/ Intersection of two sets\n        evenNumbers.retainAll(primeNumbers);\n        System.out.println(\"Intersection is: \" + evenNumbers);\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>LinkedHashSet1: &#91;2, 3]\nLinkedHashSet2: &#91;2, 4]\nIntersection is: &#91;2]\n<\/samp><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Difference of Sets<\/h3>\n\n\n\n<p>To calculate the difference between the two sets, we can use the&nbsp;<code>removeAll()<\/code>&nbsp;method. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.LinkedHashSet;\n\nclass Main {\n    public static void main(String&#91;] args) {\n        LinkedHashSet&lt;Integer&gt; primeNumbers = new LinkedHashSet&lt;&gt;();\n        primeNumbers.add(2);\n        primeNumbers.add(3);\n        primeNumbers.add(5);\n        System.out.println(\"LinkedHashSet1: \" + primeNumbers);\n\n        LinkedHashSet&lt;Integer&gt; oddNumbers = new LinkedHashSet&lt;&gt;();\n        oddNumbers.add(1);\n        oddNumbers.add(3);\n        oddNumbers.add(5);\n        System.out.println(\"LinkedHashSet2: \" + oddNumbers);\n\n        \/\/ Difference between LinkedHashSet1 and LinkedHashSet2\n        primeNumbers.removeAll(oddNumbers);\n        System.out.println(\"Difference : \" + primeNumbers);\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>LinkedHashSet1: &#91;2, 3, 5]\nLinkedHashSet2: &#91;1, 3, 5]\nDifference: &#91;2]\n<\/samp><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Subset<\/h3>\n\n\n\n<p>To check if a set is a subset of another set or not, we can use the&nbsp;<code>containsAll()<\/code>&nbsp;method. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.LinkedHashSet;\n\nclass Main {\n    public static void main(String&#91;] args) {\n        LinkedHashSet&lt;Integer&gt; numbers = new LinkedHashSet&lt;&gt;();\n        numbers.add(1);\n        numbers.add(2);\n        numbers.add(3);\n        numbers.add(4);\n        System.out.println(\"LinkedHashSet1: \" + numbers);\n\n        LinkedHashSet&lt;Integer&gt; primeNumbers = new LinkedHashSet&lt;&gt;();\n        primeNumbers.add(2);\n        primeNumbers.add(3);\n        System.out.println(\"LinkedHashSet2: \" + primeNumbers);\n\n        \/\/ Check if primeNumbers is a subset of numbers\n        boolean result = numbers.containsAll(primeNumbers);\n        System.out.println(\"Is LinkedHashSet2 is subset of LinkedHashSet1? \" + result);\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>LinkedHashSet1: &#91;1, 2, 3, 4]\nLinkedHashSet2: &#91;2, 3]\nIs LinkedHashSet2 is a subset of LinkedHashSet1? true\n<\/samp><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Other Methods Of LinkedHashSet<\/h2>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><th>Method<\/th><th>Description<\/th><\/tr><tr><td><code>clone()<\/code><\/td><td>Creates a copy of the <code>LinkedHashSet<\/code><\/td><\/tr><tr><td><code>contains()<\/code><\/td><td>Searches the <code>LinkedHashSet<\/code> for the specified element and returns a boolean result<\/td><\/tr><tr><td><code>isEmpty()<\/code><\/td><td>Checks if the <code>LinkedHashSet<\/code> is empty<\/td><\/tr><tr><td><code>size()<\/code><\/td><td>Returns the size of the <code>LinkedHashSet<\/code><\/td><\/tr><tr><td><code>clear()<\/code><\/td><td>Removes all the elements from the <code>LinkedHashSet<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>To learn more about&nbsp;<code>LinkedHashSet<\/code>&nbsp;methods, visit&nbsp;<a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/LinkedHashSet.html\">Java LinkedHashSet (official Java documentation)<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"vs-hashset\">LinkedHashSet Vs. HashSet<\/h2>\n\n\n\n<p>Both&nbsp;<code>LinkedHashSet<\/code>&nbsp;and&nbsp;<code>HashSet<\/code>&nbsp;implements the&nbsp;<code>Set<\/code>&nbsp;interface. However, there exist some differences between them.<\/p>\n\n\n\n<ul><li><code>LinkedHashSet<\/code>&nbsp;maintains a linked list internally. Due to this, it maintains the insertion order of its elements.<\/li><li>The&nbsp;<code>LinkedHashSet<\/code>&nbsp;class requires more storage than&nbsp;<code>HashSet<\/code>. This is because&nbsp;<code>LinkedHashSet<\/code>&nbsp;maintains linked lists internally.<\/li><li>The performance of&nbsp;<code>LinkedHashSet<\/code>&nbsp;is slower than&nbsp;<code>HashSet<\/code>. It is because of linked lists present in&nbsp;<code>LinkedHashSet<\/code>.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"vs-treeset\">LinkedHashSet Vs. TreeSet<\/h2>\n\n\n\n<p>Here are the major differences between&nbsp;<code>LinkedHashSet<\/code>&nbsp;and&nbsp;<code>TreeSet<\/code>:<\/p>\n\n\n\n<ul><li>The&nbsp;<code>TreeSet<\/code>&nbsp;class implements the&nbsp;<code>SortedSet<\/code>&nbsp;interface. That&#8217;s why elements in a tree set are sorted. However, the&nbsp;<code>LinkedHashSet<\/code>&nbsp;class only maintains the insertion order of its elements.<\/li><li>A&nbsp;<code>TreeSet<\/code>&nbsp;is usually slower than a&nbsp;<code>LinkedHashSet<\/code>. It is because whenever an element is added to a&nbsp;<code>TreeSet<\/code>, it has to perform the sorting operation.<\/li><li><code>LinkedHashSet<\/code>&nbsp;allows the insertion of null values. However, we cannot insert a null value to&nbsp;<code>TreeSet<\/code>.<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about the Java LinkedHashSet class and its methods with the help of examples. The&nbsp;LinkedHashSet&nbsp;class of the Java collections framework provides functionalities of both the hashtable and the linked list data structure. It implements the\u00a0Set interface. Elements of\u00a0LinkedHashSet\u00a0are stored in hash tables similar to\u00a0HashSet. However, linked hash sets maintain a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[428],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1280"}],"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=1280"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1280\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}