{"id":1107,"date":"2016-03-09T07:30:48","date_gmt":"2016-03-09T07:30:48","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?p=1107"},"modified":"2016-03-09T07:30:48","modified_gmt":"2016-03-09T07:30:48","slug":"hashset-methods","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/java\/hashset-methods.html","title":{"rendered":"Hashset Methods In Java With Example"},"content":{"rendered":"<p>Hashset is a type of Java Collection that uses Hash table for storing the data, which internally uses a phenomena known as hashing. HashSet\u00a0do not allow duplicate values, but allow null elements to be stored and also it does not maintains any insertion order.<\/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'<strong><u>\u00a0<\/u><\/strong>\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>5. boolean isEmpty()<\/strong><strong><u>\u00a0<\/u><\/strong><\/span><\/p>\n<p>As the name specifies this method checks whether the hashset is empty or not. If it is empty this method will return true, else it will return false, as shown in 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\n\/\/Checking whether hashset is empty\r\n\r\nflag= hashSetObject.isEmpty();\r\n\r\nSystem.<em>out<\/em>.println(\"Is hash set empty ? \"+flag);\r\n\r\n\/\/clearing hashset\r\n\r\nhashSetObject.clear();\r\n\r\nflag= hashSetObject.isEmpty();\r\n\r\nSystem.<em>out<\/em>.println(\"Is hash set empty ? \"+flag);\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Is hash set empty ? false\r\n\r\nIs hash set empty ? true<strong><u>\u00a0<\/u><\/strong>\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>6. Iterator iterator()<\/strong><\/span><\/p>\n<p>This methods returns the iterator, so that we can traverse over the elements of the hashset , 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\n\/\/Creating object of Iterator class\r\n\r\nIterator itr = hashSetObject.iterator();\r\n\u00a0\r\n\/\/ Printing out hashset using iterator\r\n\r\nwhile (itr.hasNext()){\r\n\r\nSystem.<em>out<\/em>.println(\"Value in hash set are: \"+itr.next() + \" \");\r\n}\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Values in hash set are: Java\r\n\r\nValues in hash set are: Love\r\n\r\nValues in hash set are: I\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>7. boolean remove(Object o)<\/strong><strong>\u00a0<\/strong><\/span><\/p>\n<p>This method removes first Occurrence of the element specified in the argument list from the hashset , and returns true if that element is removed, or false if it is not, 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\r\nhashSetObject.add(\"I\");\r\n\r\nhashSetObject.add(\"Love\");\r\n\r\nhashSetObject.add(\"Java\");\r\n\r\nSystem.<em>out<\/em>.println(\"hash set values :\" +hashSetObject);\r\n\r\n\/\/calling remove method to remove \"java\"\r\n\r\nhashSetObject.remove(\"Java\");\r\n\r\nSystem.<em>out<\/em>.println(\"hash set values after using remove method:\" +hashSetObject);\r\n}\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>hash set values :[Java, Love, I]\r\n\r\nhash set values after using remove method:[Love, I]\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>8. int size()<\/strong><\/span><\/p>\n<p>This method gives the size of the hashset, 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\nint size;\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\nsize= hashSetObject.size();\r\n\r\nSystem.<em>out<\/em>.println(\"Size of hash set is \"+size);\r\n}\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>Size of hash set is 3\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Hashset is a type of Java Collection that uses Hash table for storing the data, which internally uses a phenomena known as hashing. HashSet\u00a0do not allow duplicate values, but allow null elements to be stored and also it does not maintains any insertion order. HashSet Methods\u00a0in JAVA: Let us discuss all the HashSet\u00a0methods one by &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/hashset-methods.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Hashset Methods In Java With Example<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,77],"tags":[],"class_list":["post-1107","post","type-post","status-publish","format-standard","hentry","category-archieve","category-java-collections"],"psp_head":"<title>Hashset Methods In Java With Example \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Explanation of list of all HashSet methods in JAVA with example. Hashset is a type of Java Collection that uses Hash table for storing the data, which internally uses a phenomena known as hashing.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/hashset-methods.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/1107","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/post"}],"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=1107"}],"version-history":[{"count":1,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/1107\/revisions"}],"predecessor-version":[{"id":1457,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/1107\/revisions\/1457"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=1107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/categories?post=1107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/tags?post=1107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}