{"id":1064,"date":"2016-03-09T07:33:14","date_gmt":"2016-03-09T07:33:14","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?page_id=1064"},"modified":"2018-06-05T06:31:13","modified_gmt":"2018-06-05T06:31:13","slug":"hashset","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/java\/hashset","title":{"rendered":"Hashset Tutorial In Java With Example"},"content":{"rendered":"<p>Hashset is a type of Java Collection that implements the Set Interface and extends AbstractSet Interface. As HashSet is an implementation of Set Interface, it do not allow duplicate values to be stored, But it can store null elements. Also it does not maintain insertion order.<\/p>\n<p>HashSet uses Hash table for storing the data. Hash table internally uses a phenomena known as hashing, In hashing a unique key is generated automatically called Hash code, which is mapped as the index of the elements in the hashset.<\/p>\n<hr \/>\n<h4><strong>Hierarchy of HashSet class<\/strong><\/h4>\n<p>HashSet Class extends AbstractSet Class and implements Set Interface, Set Interface further extends Collection Interface and Iterable Interface, as shown in figure 1.<\/p>\n<p>Dotted Arrow shows implementation and Bold arrow shows Extending a Class.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1069\" src=\"\/java\/wp-content\/uploads\/2016\/03\/Hierarchy-of-HashSet-class.png\" alt=\"Hierarchy of HashSet class\" width=\"197\" height=\"409\" \/><\/center><\/p>\n<hr \/>\n<h4><strong>HashSet Methods\u00a0in JAVA<\/strong><\/h4>\n<p>Let us discuss all the HashSet\u00a0methods one by one with Examples in Java.<\/p>\n<p><span style=\"color: #008000;\"><strong>1. boolean add(Object o)<\/strong><\/span><\/p>\n<p>This methods adds the value in the hashSet, only if the element to be added is not present in existing Set, as shown in the following program.<\/p>\n<pre>import java.util.*;\r\n\r\npublic class HashSetDemo{\r\n\r\npublic static void main(String args[]){\r\n\r\nHashSet &lt;String&gt; hashSetObject = new HashSet &lt;String&gt;();\r\n\r\n\/\/Calling add() mehthod\r\n\r\nhashSetObject.add(\"I\");\r\n\r\nhashSetObject.add(\"Love\");\r\n\r\nhashSetObject.add(\"Java\");\r\n\r\nhashSetObject.add(\"Java\");\r\n\r\nhashSetObject.add(\"I\");\r\n\r\nSystem.<em>out<\/em>.println(\"values in HashSet object \"+ hashSetObject);\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>values in HashSet clone object [Java, Love, I]\r\n<\/pre>\n<p>In Output, We can see that we are not getting repeated values.<\/p>\n<p><span style=\"color: #008000;\"><strong>2. void clear()<\/strong><\/span><\/p>\n<p>This method as the name suggests remove all the elements from the set, as shown in the following program<\/p>\n<pre>import java.util.*;\r\n\r\npublic class HashSetDemo{\r\n\r\npublic static void main(String args[]){\r\n\r\nHashSet &lt;String&gt; hashSetObject = new HashSet &lt;String&gt;();\r\n\r\nhashSetObject.add(\"I\");\r\n\r\nhashSetObject.add(\"Love\");\r\n\r\nhashSetObject.add(\"Java\");\r\n\r\nSystem.<em>out<\/em>.println(\"values in HashSet object \"+ hashSetObject);\r\n\r\n\/\/Calling clear() mehthod\r\n\r\nhashSetObject.clear();\r\n\r\nSystem.<em>out<\/em>.println(\"values in HashSet object After using Clear method\"+ hashSetObject);\r\n}\r\n}\u00a0\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>values in HashSet object [Java, Love, I]\r\n\r\nvalues in HashSet object After using Clear method[]\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>3. Object clone()<\/strong><\/span><\/p>\n<p>This method returns the exact same copy of the hashSet object, as shown in the following program<\/p>\n<pre>import java.util.*;\r\n\r\npublic class HashSetDemo{\r\n\r\npublic static void main(String args[]){\r\n\r\nHashSet &lt;String&gt; hashSetObject1 = new HashSet &lt;String&gt;();\r\n\r\nHashSet &lt;String&gt; hashSetObject2 = new HashSet &lt;String&gt;();\r\n\r\nhashSetObject1.add(\"I\");\r\n\r\nhashSetObject1.add(\"Love\");\r\n\r\nhashSetObject1.add(\"Java\");\r\n\r\nhashSetObject2= (HashSet) hashSetObject1.clone();\r\n\r\nSystem.<em>out<\/em>.println(\"values in HashSet clone object \"+ \u00a0\u00a0hashSetObject2);\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>values in HashSet clone object [Java, I, Love]<strong><u>\u00a0<\/u><\/strong>\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>4. boolean contains(Object o)<\/strong><\/span><\/p>\n<p>This method returns true if the calling hashset object contains the specific element as given in the argument list, otherwise it returns false, as shown in the following program.<\/p>\n<pre>import java.util.*;\r\n\r\npublic class HashSetDemo{\r\n\r\npublic static void main(String args[]){\r\n\r\nHashSet &lt;String&gt; hashSetObject = new HashSet &lt;String&gt;();\r\n\r\nhashSetObject.add(\"I\");\r\n\r\nhashSetObject.add(\"Love\");\r\n\r\nhashSetObject.add(\"Java\");\r\n\r\nif(hashSetObject.contains(\"I\")){\r\n\r\nSystem.<em>out<\/em>.println(\"Hash Set Contains 'I'\");\r\n\r\n}\r\n\r\nelse{\r\n\r\nSystem.<em>out<\/em>.println(\"Hash Set Do not Contains 'I'\");\r\n\r\n}\r\n\r\nif(hashSetObject.contains(\"India\")){\r\n\r\nSystem.<em>out<\/em>.println(\"Hash Set Contains 'India'\");\r\n\r\n}\r\n\r\nelse{\r\n\r\nSystem.<em>out<\/em>.println(\"Hash Set Do not Contains 'India'\");\r\n}\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Hash Set Contains 'I'\r\n\r\nHash Set Do not Contains 'India'<\/pre>\n<p>Read All <a href=\"\/java\/hashset-methods.html\">HashSet Methods With Example in Java<\/a><\/p>\n<hr \/>\n<h4>\u00a0<strong>Example of <\/strong><strong>HashSet <\/strong><strong>class<\/strong><\/h4>\n<p>Let us discuss Hash set with the help of program; following program has been divided into 3 Steps that we will discuss one by one.<\/p>\n<pre>import java.util.*;\r\n\r\npublic class HashSetDemo{\r\n\r\npublic static void main(String args[]){\r\n\/\/Step: 1\r\nHashSet &lt;String&gt; hashSetObject= new HashSet &lt;String&gt;();\r\n\r\n\/\/Step: 2\r\n\r\nhashSetObject.add(\"I\"); \r\n\r\nhashSetObject.add(\"Love\");\r\n\r\nhashSetObject.add(\"Java\");\r\n\r\nhashSetObject.add(\"Java\");\r\n\r\nhashSetObject.add(\"I\");\r\n\r\n\/\/Step: 3\r\n\r\nSystem.<em>out<\/em>.println(\"Values in HashSet are:\" +hashSetObject); ) \r\n}\r\n}\u00a0\r\n<\/pre>\n<ul>\n<li>In Step 1, we have created an object of HashSet collection, we have defined element to be stored of String type.<\/li>\n<li>In Step 2, we have used add method to add elements of String type in the hashSet object that we have created in Step 1.<\/li>\n<li>In Step 3, we have printed all the values of hashSet object.<\/li>\n<\/ul>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Values in HashSet are:[Java, Love, I]\r\n<\/pre>\n<p>As we can see in above output Duplicate values are not Stored and Insertion order is not maintained.<\/p>\n<hr \/>\n<h4><strong>HashSet Important Points<\/strong><\/h4>\n<ul>\n<li>HashSet only stores unique values, that is duplicate values are not allowed.<\/li>\n<li>HashSet do not maintain insertion order, which means it does not return 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>HashSet allows only one null value in it, As duplicates are not allowed.<\/li>\n<li>HashSet uses hash table to store the values.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Hashset is a type of Java Collection that implements the Set Interface and extends AbstractSet Interface. As HashSet is an implementation of Set Interface, it do not allow duplicate values to be stored, But it can store null elements. Also it does not maintain insertion order. HashSet uses Hash table for storing the data. Hash &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/hashset\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Hashset Tutorial In Java With Example<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"footnotes":""},"class_list":["post-1064","page","type-page","status-publish","hentry"],"psp_head":"<title>Hashset Tutorial In Java With Example \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Hashset is a type of Java Collection that implements the Set Interface and extends AbstractSet Interface, hashSet uses Hash table for storing the data.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/hashset\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/1064","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=1064"}],"version-history":[{"count":1,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/1064\/revisions"}],"predecessor-version":[{"id":1475,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/1064\/revisions\/1475"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=1064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}