{"id":4167,"date":"2017-09-19T09:34:06","date_gmt":"2017-09-19T09:34:06","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=4167"},"modified":"2021-03-21T06:43:13","modified_gmt":"2021-03-21T06:43:13","slug":"what-is-the-difference-between-set-and-list","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/what-is-the-difference-between-set-and-list\/","title":{"rendered":"What is the difference between Set and List"},"content":{"rendered":"<h3>Introduction<\/h3>\n<p>In this tutorial, we will discuss the differences between Set and List. Set and List are both interfaces in the Java Collection Framework. Both extend the core Collection interface.<\/p>\n<p><strong><a href=\"https:\/\/www.testingdocs.com\/java-collection-framework\/\">https:\/\/www.testingdocs.com\/java-collection-framework\/<\/a><\/strong><\/p>\n<h3>Set<\/h3>\n<p>The Set is an unordered collection. The order of the elements in the collection is not guaranteed. The Set doesn&#8217;t allow duplicate elements in the collection.<\/p>\n<pre>package list;\r\n\r\nimport java.util.HashSet;\r\nimport java.util.Set;\r\n\r\npublic class SetDemo {\r\n\r\n    Set&lt;String&gt; set = new HashSet&lt;String&gt;();\r\n    public SetDemo() {\r\n        set.add(\"One\");\r\n        set.add(\"One\");\r\n        set.add(null);  \/\/ null is allowed\r\n\r\n        set.add(\"Two\");\r\n        set.add(\"Three\");\r\n        set.add(\"Four\");\r\n    }\r\n\r\n    \/\/traverse method using for each\r\n    public void traverse() {\r\n        for(String str : set) {\r\n            System.out.println(str); \/\/ order is not guaranteed\r\n             }\r\n    }\r\n\r\n    public int setSize() {\r\n        return set.size();\r\n    }\r\n\r\n    public static void main(String[] args) {\r\n        SetDemo d = new SetDemo();\r\n        d.traverse();\r\n        System.out.println(\"Set Size=\" + d.setSize());\/\/duplicates are removed\r\n        }\r\n}\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>null<br \/>\nOne<br \/>\nFour<br \/>\nTwo<br \/>\nThree<br \/>\nSet Size=5<\/p>\n<h3>List<\/h3>\n<p>A List is an ordered collection and it maintains the insert order of the elements. The list allows duplicates into the collection.<\/p>\n<pre>package list;\r\n\r\nimport java.util.ArrayList;\r\nimport java.util.Iterator; \r\nimport java.util.List; \r\n\r\npublic class ListDemo { \r\n    List&lt;String&gt; list = new ArrayList&lt;String&gt;(); \r\n    public ListDemo() { \r\n        list.add(\"One\");\r\n        list.add(\"Two\"); \r\n        list.add(\"Three\"); \r\n        list.add(\"Four\");\r\n        list.add(\"Five\"); \r\n        list.add(\"One\"); \/\/ allows duplicates,see output \r\n        } \r\n        \r\n        \/\/traverse method using for each \r\n       public void traverse() { \r\n        for(String str : list) {\r\n            System.out.println(str); \/\/ maintains the order\r\n            }\r\n    } \r\n    \r\n    public static void main(String[] args) {\r\n        ListDemo d = new ListDemo();\r\n        d.traverse(); \r\n    } \r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>One<br \/>\nTwo<br \/>\nThree<br \/>\nFour<br \/>\nFive<br \/>\nOne<\/p>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-4167\" data-postid=\"4167\" class=\"themify_builder_content themify_builder_content-4167 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Introduction In this tutorial, we will discuss the differences between Set and List. Set and List are both interfaces in the Java Collection Framework. Both extend the core Collection interface. https:\/\/www.testingdocs.com\/java-collection-framework\/ Set The Set is an unordered collection. The order of the elements in the collection is not guaranteed. The Set doesn&#8217;t allow duplicate elements [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-4167","post","type-post","status-publish","format-standard","hentry","category-java","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/4167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=4167"}],"version-history":[{"count":3,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/4167\/revisions"}],"predecessor-version":[{"id":19741,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/4167\/revisions\/19741"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=4167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=4167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=4167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}