{"id":1276,"date":"2022-02-26T07:36:38","date_gmt":"2022-02-26T07:36:38","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1276"},"modified":"2022-02-26T07:36:38","modified_gmt":"2022-02-26T07:36:38","slug":"java-set-interface","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/26\/java-set-interface\/","title":{"rendered":"Java Set Interface"},"content":{"rendered":"\n<p>In this tutorial, we will learn about the Set interface in Java and its methods.<\/p>\n\n\n\n<p>The&nbsp;<code>Set<\/code>&nbsp;interface of the Java&nbsp;<code>Collections<\/code>&nbsp;framework provides the features of the mathematical set in Java. It extends the&nbsp;<code>Collection<\/code>&nbsp;interface.<\/p>\n\n\n\n<p>Unlike the&nbsp;<code>List<\/code>&nbsp;interface, sets cannot contain duplicate elements.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"classes\">Classes that implement Set<\/h2>\n\n\n\n<p>Since&nbsp;<code>Set<\/code>&nbsp;is an interface, we cannot create objects from it.<\/p>\n\n\n\n<p>In order to use functionalities of the&nbsp;<code>Set<\/code>&nbsp;interface, we can use these classes:<\/p>\n\n\n\n<ul><li>HashSet<\/li><li>LinkedHashSet<\/li><li>EnumSet<\/li><li>TreeSet<\/li><\/ul>\n\n\n\n<p>These classes are defined in the&nbsp;<code>Collections<\/code>&nbsp;framework and implement the&nbsp;<code>Set<\/code>&nbsp;interface.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-set-implementation.png\" alt=\"Interfaces SortedSet and NavigableSet extends the Set interface.\"\/><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"interfaces\">Interfaces that extend Set<\/h2>\n\n\n\n<p>The&nbsp;<code>Set<\/code>&nbsp;interface is also extended by these subinterfaces:<\/p>\n\n\n\n<ul><li><a href=\"https:\/\/www.programiz.com\/java-programming\/sortedset\">SortedSet<\/a><\/li><li>NavigableSet<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-set.png\" alt=\"Classes EnumSet, HashSet, LinkedHastSet and TreeSet implement the Set interface.\"\/><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"use\">How to use Set?<\/h2>\n\n\n\n<p>In Java, we must import&nbsp;<code>java.util.Set<\/code>&nbsp;package in order to use&nbsp;<code>Set<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Set implementation using HashSet\nSet&lt;String&gt; animals = new HashSet&lt;&gt;();\n<\/code><\/pre>\n\n\n\n<p>Here, we have created a&nbsp;<code>Set<\/code>&nbsp;called&nbsp;<var>animals<\/var>. We have used the&nbsp;<code>HashSet<\/code>&nbsp;class to implement the&nbsp;<code>Set<\/code>&nbsp;interface.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"methods\">Methods of Set<\/h2>\n\n\n\n<p>The&nbsp;<code>Set<\/code>&nbsp;interface includes all the methods of the&nbsp;<code>Collection<\/code>&nbsp;interface. It&#8217;s because&nbsp;<code>Collection<\/code>&nbsp;is a super interface of&nbsp;<code>Set<\/code>.<\/p>\n\n\n\n<p>Some of the commonly used methods of the&nbsp;<code>Collection<\/code>&nbsp;interface that&#8217;s also available in the&nbsp;<code>Set<\/code>&nbsp;interface are:<\/p>\n\n\n\n<ul><li><strong>add()<\/strong>&nbsp;&#8211; adds the specified element to the set<\/li><li><strong>addAll()<\/strong>&nbsp;&#8211; adds all the elements of the specified collection to the set<\/li><li><strong>iterator()<\/strong>&nbsp;&#8211; returns an iterator that can be used to access elements of the set sequentially<\/li><li><strong>remove()<\/strong>&nbsp;&#8211; removes the specified element from the set<\/li><li><strong>removeAll()<\/strong>&nbsp;&#8211; removes all the elements from the set that is present in another specified set<\/li><li><strong>retainAll()<\/strong>&nbsp;&#8211; retains all the elements in the set that are also present in another specified set<\/li><li><strong>clear()<\/strong>&nbsp;&#8211; removes all the elements from the set<\/li><li><strong>size()<\/strong>&nbsp;&#8211; returns the length (number of elements) of the set<\/li><li><strong>toArray()<\/strong>&nbsp;&#8211; returns an array containing all the elements of the set<\/li><li><strong>contains()<\/strong>&nbsp;&#8211; returns&nbsp;<code>true<\/code>&nbsp;if the set contains the specified element<\/li><li><strong>containsAll()<\/strong>&nbsp;&#8211; returns&nbsp;<code>true<\/code>&nbsp;if the set contains all the elements of the specified collection<\/li><li><strong>hashCode()<\/strong>&nbsp;&#8211; returns a hash code value (address of the element in the set)<\/li><\/ul>\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=\"operations\">Set Operations<\/h2>\n\n\n\n<p>The Java&nbsp;<code>Set<\/code>&nbsp;interface allows us to perform basic mathematical set operations like union, intersection, and subset.<\/p>\n\n\n\n<ul><li><strong>Union<\/strong>&nbsp;&#8211; to get the union of two sets&nbsp;<var>x<\/var>&nbsp;and&nbsp;<var>y<\/var>, we can use&nbsp;<code>x.addAll(y)<\/code><\/li><li><strong>Intersection<\/strong>&nbsp;&#8211; to get the intersection of two sets&nbsp;<var>x<\/var>&nbsp;and&nbsp;<var>y<\/var>, we can use&nbsp;<code>x.retainAll(y)<\/code><\/li><li><strong>Subset<\/strong>&nbsp;&#8211; to check if&nbsp;<var>x<\/var>&nbsp;is a subset of&nbsp;<var>y<\/var>, we can use&nbsp;<code>y.containsAll(x)<\/code><\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"implementation-classes\">Implementation of the Set Interface<\/h2>\n\n\n\n<p><strong>1. Implementing HashSet Class<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Set;\nimport java.util.HashSet;\n\nclass Main {\n\n    public static void main(String&#91;] args) {\n        \/\/ Creating a set using the HashSet class\n        Set&lt;Integer&gt; set1 = new HashSet&lt;&gt;();\n\n        \/\/ Add elements to the set1\n        set1.add(2);\n        set1.add(3);\n        System.out.println(\"Set1: \" + set1);\n\n        \/\/ Creating another set using the HashSet class\n        Set&lt;Integer&gt; set2 = new HashSet&lt;&gt;();\n\n        \/\/ Add elements\n        set2.add(1);\n        set2.add(2);\n        System.out.println(\"Set2: \" + set2);\n\n        \/\/ Union of two sets\n        set2.addAll(set1);\n        System.out.println(\"Union is: \" + set2);\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>Set1: &#91;2, 3]\nSet2: &#91;1, 2]\nUnion is: &#91;1, 2, 3]\n<\/samp><\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>2. Implementing TreeSet Class<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Set;\nimport java.util.TreeSet;\nimport java.util.Iterator;\n\nclass Main {\n\n    public static void main(String&#91;] args) {\n        \/\/ Creating a set using the TreeSet class\n        Set&lt;Integer&gt; numbers = new TreeSet&lt;&gt;();\n\n        \/\/ Add elements to the set\n        numbers.add(2);\n        numbers.add(3);\n        numbers.add(1);\n        System.out.println(\"Set using TreeSet: \" + numbers);\n\n        \/\/ Access Elements using iterator()\n        System.out.print(\"Accessing elements using iterator(): \");\n        Iterator&lt;Integer&gt; iterate = numbers.iterator();\n        while(iterate.hasNext()) {\n            System.out.print(iterate.next());\n            System.out.print(\", \");\n        }\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>Set using TreeSet: &#91;1, 2, 3]\nAccessing elements using iterator(): 1, 2, 3,\n<\/samp><\/code><\/pre>\n\n\n\n<p>To learn more about\u00a0<code>TreeSet<\/code>, visit\u00a0Java TreeSet.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Now that we know what&nbsp;<code>Set<\/code>&nbsp;is, we will see its implementations in classes like&nbsp;<code>EnumSet<\/code>,&nbsp;<code>HashSet<\/code>,&nbsp;<code>LinkedHashSet<\/code>&nbsp;and&nbsp;<code>TreeSet<\/code>&nbsp;in the next tutorials.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about the Set interface in Java and its methods. The&nbsp;Set&nbsp;interface of the Java&nbsp;Collections&nbsp;framework provides the features of the mathematical set in Java. It extends the&nbsp;Collection&nbsp;interface. Unlike the&nbsp;List&nbsp;interface, sets cannot contain duplicate elements. Classes that implement Set Since&nbsp;Set&nbsp;is an interface, we cannot create objects from it. In order to use [&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\/1276"}],"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=1276"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1276\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}