{"id":359,"date":"2025-07-01T12:14:42","date_gmt":"2025-07-01T12:14:42","guid":{"rendered":"https:\/\/thetestdata.com\/blog\/?p=359"},"modified":"2025-07-16T10:06:07","modified_gmt":"2025-07-16T10:06:07","slug":"what-is-the-difference-between-list-and-set","status":"publish","type":"post","link":"https:\/\/thetestdata.com\/blog\/what-is-the-difference-between-list-and-set\/","title":{"rendered":"What is the difference between List and Set?"},"content":{"rendered":"\n<p>\ud83d\uddc2\ufe0f Difference Between List and Set<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Feature<\/th><th><code>List<\/code><\/th><th><code>Set<\/code><\/th><\/tr><\/thead><tbody><tr><td><strong>Order<\/strong><\/td><td>Maintains insertion order<\/td><td>No guaranteed order (unless using LinkedHashSet)<\/td><\/tr><tr><td><strong>Duplicates<\/strong><\/td><td>\u2705 Allows duplicates<\/td><td>\u274c Does <strong>not<\/strong> allow duplicates<\/td><\/tr><tr><td><strong>Indexing<\/strong><\/td><td>Indexed\u2014can access elements by index (<code>get(i)<\/code>)<\/td><td>Not indexed\u2014no position-based access<\/td><\/tr><tr><td><strong>Implementations<\/strong><\/td><td><code>ArrayList<\/code>, <code>LinkedList<\/code>, <code>Vector<\/code>, etc.<\/td><td><code>HashSet<\/code>, <code>LinkedHashSet<\/code>, <code>TreeSet<\/code>, etc.<\/td><\/tr><tr><td><strong>Best Use Case<\/strong><\/td><td>When order &amp; duplicates matter<\/td><td>When uniqueness is required<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\ud83d\udcbb Code Example<\/p>\n\n\n\n<p>\ud83d\udccb Using a <code>List<\/code> (e.g., <code>ArrayList<\/code>)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.*;\n\npublic class ListExample {\n    public static void main(String&#91;] args) {\n        List&lt;String> fruits = new ArrayList&lt;>();\n        fruits.add(\"Apple\");\n        fruits.add(\"Banana\");\n        fruits.add(\"Apple\");  \/\/ Duplicate allowed\n\n        System.out.println(fruits);  \/\/ &#91;Apple, Banana, Apple]\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u2705 Using a <code>Set<\/code> (e.g., <code>HashSet<\/code>)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.*;\n\npublic class SetExample {\n    public static void main(String&#91;] args) {\n        Set&lt;String> fruits = new HashSet&lt;>();\n        fruits.add(\"Apple\");\n        fruits.add(\"Banana\");\n        fruits.add(\"Apple\");  \/\/ Duplicate ignored\n\n        System.out.println(fruits);  \/\/ &#91;Apple, Banana] (order may vary)\n    }\n}<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc When to Use Which?<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Situation<\/th><th>Choose<\/th><\/tr><\/thead><tbody><tr><td>You want to store elements <strong>with duplicates<\/strong> and <strong>preserve order<\/strong><\/td><td><code>List<\/code><\/td><\/tr><tr><td>You need a <strong>collection of unique values<\/strong><\/td><td><code>Set<\/code><\/td><\/tr><tr><td>You want to perform fast <strong>lookup, insert, delete<\/strong>, and don&#8217;t care about order<\/td><td><code>HashSet<\/code><\/td><\/tr><tr><td>You need <strong>sorted unique elements<\/strong><\/td><td><code>TreeSet<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>\ud83c\udfaf Interview Takeaways<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u2705 Highlight that <code>List<\/code> allows duplicates and is indexed.<\/li>\n\n\n\n<li>\u2705 Stress that <code>Set<\/code> is all about <strong>uniqueness<\/strong> and often better for <strong>quick lookups<\/strong>.<\/li>\n\n\n\n<li>\u274c Don\u2019t confuse order preservation\u2014only <code>LinkedHashSet<\/code> maintains insertion order, not <code>HashSet<\/code>.<\/li>\n\n\n\n<li>\u2705 Mention performance: <code>HashSet<\/code> has <strong>O(1)<\/strong> average time complexity for basic operations.<\/li>\n\n\n\n<li>Pro tip: <strong>Always choose the right collection<\/strong> based on the behavior you want\u2014don&#8217;t just default to <code>ArrayList<\/code>.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Understand the key differences between List and Set in Java. Learn about ordering, duplicates, and use cases to choose the right data structure in coding.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[17],"tags":[],"class_list":["post-359","post","type-post","status-publish","format-standard","hentry","category-java-interview-questions"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/359","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/comments?post=359"}],"version-history":[{"count":2,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/359\/revisions"}],"predecessor-version":[{"id":415,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/359\/revisions\/415"}],"wp:attachment":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/media?parent=359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/categories?post=359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/tags?post=359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}