{"id":1278,"date":"2022-02-26T07:38:38","date_gmt":"2022-02-26T07:38:38","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1278"},"modified":"2022-02-26T07:38:38","modified_gmt":"2022-02-26T07:38:38","slug":"hash-set-class","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/26\/hash-set-class\/","title":{"rendered":"Hash Set Class"},"content":{"rendered":"\n<p>In this tutorial, we will learn about the Java HashSet class. We will learn about different hash set methods and operations with the help of examples.<\/p>\n\n\n\n<p>The&nbsp;<code>HashSet<\/code>&nbsp;class of the Java Collections framework provides the functionalities of the hash table 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-hashset.png\" alt=\"Java HashSet class implements the Set interface.\"\/><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"create\">Creating a HashSet<\/h2>\n\n\n\n<p>In order to create a hash set, we must import the&nbsp;<code>java.util.HashSet<\/code>&nbsp;package first.<\/p>\n\n\n\n<p>Once we import the package, here is how we can create hash sets in Java.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ HashSet with 8 capacity and 0.75 load factor\nHashSet&lt;Integer&gt; numbers = new HashSet&lt;&gt;(8, 0.75);\n<\/code><\/pre>\n\n\n\n<p>Here, we have created a hash set named&nbsp;<code>numbers<\/code>.<\/p>\n\n\n\n<p>Notice, the part new&nbsp;<code>HashSet&lt;&gt;(8, 0.75)<\/code>. Here, the first parameter is&nbsp;<strong>capacity<\/strong>, 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 set 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 hash table without defining its capacity and load factor. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ HashSet with default capacity and load factor\nHashSet&lt;Integer&gt; numbers1 = new HashSet&lt;&gt;();\n<\/code><\/pre>\n\n\n\n<p>By default,<\/p>\n\n\n\n<ul><li>the capacity of the 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\">Methods of HashSet<\/h2>\n\n\n\n<p>The&nbsp;<code>HashSet<\/code>&nbsp;class provides various methods that allow us to perform various operations on the 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 HashSet<\/h2>\n\n\n\n<ul><li><code>add()<\/code>&nbsp;&#8211; inserts the specified element to the set<\/li><li><code>addAll()<\/code>&nbsp;&#8211; inserts all the elements of the specified collection to the set<\/li><\/ul>\n\n\n\n<p>For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.HashSet;\n\nclass Main {\n    public static void main(String&#91;] args) {\n        HashSet&lt;Integer&gt; evenNumber = new HashSet&lt;&gt;();\n\n        \/\/ Using add() method\n        evenNumber.add(2);\n        evenNumber.add(4);\n        evenNumber.add(6);\n        System.out.println(\"HashSet: \" + evenNumber);\n\n        HashSet&lt;Integer&gt; numbers = new HashSet&lt;&gt;();\n        \n        \/\/ Using addAll() method\n        numbers.addAll(evenNumber);\n        numbers.add(5);\n        System.out.println(\"New HashSet: \" + 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>HashSet: &#91;2, 4, 6]\nNew HashSet: &#91;2, 4, 5, 6]\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 HashSet Elements<\/h2>\n\n\n\n<p>To access the elements of a 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.HashSet;\nimport java.util.Iterator;\n\nclass Main {\n    public static void main(String&#91;] args) {\n        HashSet&lt;Integer&gt; numbers = new HashSet&lt;&gt;();\n        numbers.add(2);\n        numbers.add(5);\n        numbers.add(6);\n        System.out.println(\"HashSet: \" + numbers);\n\n        \/\/ Calling iterator() method\n        Iterator&lt;Integer&gt; iterate = numbers.iterator();\n        System.out.print(\"HashSet using Iterator: \");\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>HashSet: &#91;2, 5, 6]\nHashSet using Iterator: 2, 5, 6,\n<\/samp><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"remove\">Remove Elements<\/h2>\n\n\n\n<ul><li><code>remove()<\/code>&nbsp;&#8211; removes the specified element from the set<\/li><li><code>removeAll()<\/code>&nbsp;&#8211; removes all the elements from the set<\/li><\/ul>\n\n\n\n<p>For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.HashSet;\n\nclass Main {\n    public static void main(String&#91;] args) {\n        HashSet&lt;Integer&gt; numbers = new HashSet&lt;&gt;();\n        numbers.add(2);\n        numbers.add(5);\n        numbers.add(6);\n        System.out.println(\"HashSet: \" + numbers);\n\n        \/\/ Using 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>HashSet: &#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=\"operations\">Set Operations<\/h2>\n\n\n\n<p>The various methods of the&nbsp;<code>HashSet<\/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<h2 class=\"wp-block-heading\">Union of Sets<\/h2>\n\n\n\n<p>To 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.HashSet;\n\nclass Main {\n    public static void main(String&#91;] args) {\n        HashSet&lt;Integer&gt; evenNumbers = new HashSet&lt;&gt;();\n        evenNumbers.add(2);\n        evenNumbers.add(4);\n        System.out.println(\"HashSet1: \" + evenNumbers);\n\n        HashSet&lt;Integer&gt; numbers = new HashSet&lt;&gt;();\n        numbers.add(1);\n        numbers.add(3);\n        System.out.println(\"HashSet2: \" + numbers);\n\n        \/\/ Union of two set\n        numbers.addAll(evenNumbers);\n        System.out.println(\"Union is: \" + 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>HashSet1: &#91;2, 4]\nHashSet2: &#91;1, 3]\nUnion is: &#91;1, 2, 3, 4]\n<\/samp><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Intersection of Sets<\/h2>\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.HashSet;\n\nclass Main {\n    public static void main(String&#91;] args) {\n        HashSet&lt;Integer&gt; primeNumbers = new HashSet&lt;&gt;();\n        primeNumbers.add(2);\n        primeNumbers.add(3);\n        System.out.println(\"HashSet1: \" + primeNumbers);\n\n        HashSet&lt;Integer&gt; evenNumbers = new HashSet&lt;&gt;();\n        evenNumbers.add(2);\n        evenNumbers.add(4);\n        System.out.println(\"HashSet2: \" + 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>HashSet1: &#91;2, 3]\nHashSet2: &#91;2, 4]\nIntersection is: &#91;2]\n<\/samp><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Difference of Sets<\/h2>\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.HashSet;\n\nclass Main {\n    public static void main(String&#91;] args) {\n        HashSet&lt;Integer&gt; primeNumbers = new HashSet&lt;&gt;();\n        primeNumbers.add(2);\n        primeNumbers.add(3);\n        primeNumbers.add(5);\n        System.out.println(\"HashSet1: \" + primeNumbers);\n\n        HashSet&lt;Integer&gt; oddNumbers = new HashSet&lt;&gt;();\n        oddNumbers.add(1);\n        oddNumbers.add(3);\n        oddNumbers.add(5);\n        System.out.println(\"HashSet2: \" + oddNumbers);\n\n        \/\/ Difference between HashSet1 and HashSet2\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>HashSet1: &#91;2, 3, 5]\nHashSet2: &#91;1, 3, 5]\nDifference: &#91;2]\n<\/samp><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Subset<\/h2>\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.HashSet;\n\nclass Main {\n    public static void main(String&#91;] args) {\n        HashSet&lt;Integer&gt; numbers = new HashSet&lt;&gt;();\n        numbers.add(1);\n        numbers.add(2);\n        numbers.add(3);\n        numbers.add(4);\n        System.out.println(\"HashSet1: \" + numbers);\n\n        HashSet&lt;Integer&gt; primeNumbers = new HashSet&lt;&gt;();\n        primeNumbers.add(2);\n        primeNumbers.add(3);\n        System.out.println(\"HashSet2: \" + primeNumbers);\n\n        \/\/ Check if primeNumbers is a subset of numbers\n        boolean result = numbers.containsAll(primeNumbers);\n        System.out.println(\"Is HashSet2 is subset of HashSet1? \" + 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>HashSet1: &#91;1, 2, 3, 4]\nHashSet2: &#91;2, 3]\nIs HashSet2 is a subset of HashSet1? 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 HashSet<\/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>HashSet<\/code><\/td><\/tr><tr><td><code>contains()<\/code><\/td><td>Searches the <code>HashSet<\/code> for the specified element and returns a boolean result<\/td><\/tr><tr><td><code>isEmpty()<\/code><\/td><td>Checks if the <code>HashSet<\/code> is empty<\/td><\/tr><tr><td><code>size()<\/code><\/td><td>Returns the size of the <code>HashSet<\/code><\/td><\/tr><tr><td><code>clear()<\/code><\/td><td>Removes all the elements from the <code>HashSet<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\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=\"why\">Why HashSet?<\/h2>\n\n\n\n<p>In Java,&nbsp;<code>HashSet<\/code>&nbsp;is commonly used if we have to access elements randomly. It is because elements in a hash table are accessed using hash codes.<\/p>\n\n\n\n<p>The hashcode of an element is a unique identity that helps to identify the element in a hash table.<\/p>\n\n\n\n<p><code>HashSet<\/code>&nbsp;cannot contain duplicate elements. Hence, each hash set element has a unique hashcode.<\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;HashSet is not synchronized. That is if multiple threads access the hash set at the same time and one of the threads modifies the hash set. Then it must be externally synchronized.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about the Java HashSet class. We will learn about different hash set methods and operations with the help of examples. The&nbsp;HashSet&nbsp;class of the Java Collections framework provides the functionalities of the hash table data structure. It implements the\u00a0Set interface. Creating a HashSet In order to create a hash set, [&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\/1278"}],"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=1278"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1278\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}