{"id":1228,"date":"2026-09-03T13:00:00","date_gmt":"2026-09-03T07:30:00","guid":{"rendered":"https:\/\/swapnilzambare.com\/blog\/apex-collections-list-set-map\/"},"modified":"2026-09-03T13:00:00","modified_gmt":"2026-09-03T07:30:00","slug":"apex-collections-list-set-map","status":"publish","type":"post","link":"https:\/\/swapnilzambare.com\/blog\/apex-collections-list-set-map\/","title":{"rendered":"Apex Collections: List, Set and Map Explained"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Apex Collections are variables that hold many values at once instead of just one. Apex gives you three kinds: List, Set and Map. They are the key to code that handles 1 record or 200 records with the same ease. Salesforce calls this bulk-safe code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the last post on <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-loops-for-while-do-while\/\">Apex loops<\/a>, you saw why queries inside loops break limits. Collections are the fix. You will also use them in almost every trigger, class and test you write from now on. If you need a reminder of basic types, look back at <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-data-types-variables-constants\/\">Apex data types<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Are Apex Collections?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A collection is a container that groups many values under one name. Each kind of collection stores its values in a different way. For example, a List keeps them in order. A Set keeps only unique values, while a Map pairs each key with a value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is an everyday picture. A List is like a queue at a ticket counter, where order matters and the same person can appear twice. Next, a Set is like a guest list for a party, where each name appears once. Finally, a Map is like a phone book, where you look up a name to find a number.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Apex Collections Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Salesforce runs code in bulk. A trigger can get 200 records at once, and every transaction has strict limits. So you cannot query or save records one at a time. Instead, you gather records into Apex Collections, query once, and save once.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is what Apex Collections let you do at Bright Path Tutors.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Hold all courses returned by one SOQL query.<\/li>\n\n<li>Collect unique course levels without duplicates.<\/li>\n\n<li>Look up a course by its Id in one step, without another query.<\/li>\n\n<li>Save 200 updated courses with a single DML statement.<\/li>\n\n<li>Count how many courses each level has.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">In short, without collections, real Salesforce code does not scale.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Apex Collections Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Each collection has a type for the values it holds, written in angle brackets. For example, <code>List&lt;String&gt;<\/code> holds text, and <code>Map&lt;Id, Course__c&gt;<\/code> holds course records keyed by their Id. Apex checks these types when it compiles, so you cannot add the wrong kind of value.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-collections-list-set-map-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-collections-list-set-map-diagram-1-1024x640.png\" alt=\"Apex Collections diagram showing how List, Set and Map store values\" class=\"wp-image-1226\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-collections-list-set-map-diagram-1-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-collections-list-set-map-diagram-1-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-collections-list-set-map-diagram-1-768x480.png 768w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-collections-list-set-map-diagram-1-1536x960.png 1536w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-collections-list-set-map-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">How a List, a Set and a Map each store values.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">First, you create a collection with <code>new<\/code>. Then you add values with <code>add<\/code> or <code>put<\/code>, and read them back with <code>get<\/code> or a loop. Collections live in memory, so they are very fast. They count against the heap size limit, but for normal code that limit is rarely a problem.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Types of Apex Collections<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">List<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A List is an ordered collection. Each item has an index number that starts at 0, not 1. Lists allow duplicates, so the same value can appear many times. SOQL queries return their results as a List.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;String&gt; levels = new List&lt;String&gt;{'Beginner', 'Advanced', 'Beginner'};\nlevels.add('Intermediate');\n\nSystem.debug(levels.size());   \/\/ 4\nSystem.debug(levels.get(0));   \/\/ Beginner\nSystem.debug(levels);          \/\/ (Beginner, Advanced, Beginner, Intermediate)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The list starts with three values, including a duplicate. Then <code>add<\/code> puts a fourth value at the end. Next, <code>size()<\/code> returns the count, and <code>get(0)<\/code> reads the first item.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Set<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Unlike a List, a Set holds unique values only. If you add a value that already exists, the Set simply ignores it. Sets are perfect for collecting record Ids before a query.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Set&lt;String&gt; uniqueLevels = new Set&lt;String&gt;{'Beginner', 'Advanced', 'Beginner'};\nuniqueLevels.add('Advanced');\n\nSystem.debug(uniqueLevels.size());               \/\/ 2\nSystem.debug(uniqueLevels.contains('Beginner')); \/\/ true<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Even though we added &#8220;Beginner&#8221; twice and &#8220;Advanced&#8221; twice, the Set keeps one of each. So <code>size()<\/code> returns 2. The <code>contains<\/code> method checks membership very quickly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Map<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Next, a Map stores key and value pairs. Each key is unique and points to one value. You use <code>put<\/code> to add a pair and <code>get<\/code> to read a value by its key.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Map&lt;String, Decimal&gt; feeByCourse = new Map&lt;String, Decimal&gt;();\nfeeByCourse.put('Apex Basics', 4500);\nfeeByCourse.put('LWC Basics', 6000);\nfeeByCourse.put('Apex Basics', 4800); \/\/ replaces the old value\n\nSystem.debug(feeByCourse.get('Apex Basics'));       \/\/ 4800\nSystem.debug(feeByCourse.containsKey('Git Basics')); \/\/ false\nSystem.debug(feeByCourse.keySet());                  \/\/ {Apex Basics, LWC Basics}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Putting the same key again replaces its value, so Apex Basics now costs 4800. The <code>containsKey<\/code> check returns false for a course that is not in the Map. Finally, <code>keySet()<\/code> returns all keys as a Set.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Useful Methods for Apex Collections<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The table below lists the methods you will use most. Many work on more than one kind of collection.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Method<\/th><th>Works on<\/th><th>What it does<\/th><\/tr><\/thead><tbody><tr><td>add(value)<\/td><td>List, Set<\/td><td>Adds one value<\/td><\/tr><tr><td>addAll(collection)<\/td><td>List, Set<\/td><td>Adds many values at once<\/td><\/tr><tr><td>put(key, value)<\/td><td>Map<\/td><td>Adds or replaces a pair<\/td><\/tr><tr><td>get(index or key)<\/td><td>List, Map<\/td><td>Reads one value<\/td><\/tr><tr><td>contains(value)<\/td><td>List, Set<\/td><td>Checks if a value exists<\/td><\/tr><tr><td>containsKey(key)<\/td><td>Map<\/td><td>Checks if a key exists<\/td><\/tr><tr><td>remove(item)<\/td><td>List, Set, Map<\/td><td>Removes an item<\/td><\/tr><tr><td>size()<\/td><td>All<\/td><td>Returns the number of items<\/td><\/tr><tr><td>isEmpty()<\/td><td>All<\/td><td>Returns true when there are no items<\/td><\/tr><tr><td>keySet() and values()<\/td><td>Map<\/td><td>Returns all keys or all values<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">List vs Set vs Map<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Choosing the right Apex Collections makes your code shorter and faster. The table below compares all three.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>List<\/th><th>Set<\/th><th>Map<\/th><\/tr><\/thead><tbody><tr><td>Keeps order<\/td><td>Yes<\/td><td>Do not rely on it<\/td><td>Do not rely on it<\/td><\/tr><tr><td>Allows duplicates<\/td><td>Yes<\/td><td>No<\/td><td>Keys no, values yes<\/td><\/tr><tr><td>Read by<\/td><td>Index, from 0<\/td><td>Loop or contains<\/td><td>Key<\/td><\/tr><tr><td>Fast lookup<\/td><td>No<\/td><td>Yes<\/td><td>Yes<\/td><\/tr><tr><td>Returned by SOQL<\/td><td>Yes<\/td><td>No<\/td><td>Yes, with a constructor<\/td><\/tr><tr><td>Typical use<\/td><td>Query results, DML<\/td><td>Unique Ids<\/td><td>Lookups by Id<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">When to use which comes down to one question. Use a List when order or duplicates matter, and a Set for unique values. Then use a Map when you need to find something by a key.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-collections-list-set-map-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-collections-list-set-map-diagram-2-1024x640.png\" alt=\"Apex Collections diagram comparing List, Set and Map\" class=\"wp-image-1227\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-collections-list-set-map-diagram-2-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-collections-list-set-map-diagram-2-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-collections-list-set-map-diagram-2-768x480.png 768w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-collections-list-set-map-diagram-2-1536x960.png 1536w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-collections-list-set-map-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">List vs Set vs Map in Apex.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">A Real Apex Collections Example: The Bulk Pattern<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now let&#8217;s fix the bad loop from the previous post. Bright Path Tutors wants each new course to note how many courses already exist at the same level. The bad version ran one query per course. However, the good version uses Apex Collections and runs just one query.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>trigger CourseLevelCount on Course__c (before insert) {\n    \/\/ 1. Collect the levels we need\n    Set&lt;String&gt; levels = new Set&lt;String&gt;();\n    for (Course__c c : Trigger.new) {\n        if (c.Level__c != null) {\n            levels.add(c.Level__c);\n        }\n    }\n\n    \/\/ 2. One query for all levels, counted in a Map\n    Map&lt;String, Integer&gt; countByLevel = new Map&lt;String, Integer&gt;();\n    for (Course__c existing : [SELECT Level__c FROM Course__c WHERE Level__c IN :levels]) {\n        Integer current = countByLevel.get(existing.Level__c) ?? 0;\n        countByLevel.put(existing.Level__c, current + 1);\n    }\n\n    \/\/ 3. Use the Map inside the loop, with no extra queries\n    for (Course__c c : Trigger.new) {\n        Integer total = countByLevel.get(c.Level__c) ?? 0;\n        System.debug(c.Name + ' joins ' + total + ' other ' + c.Level__c + ' courses');\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The first loop gathers unique levels into a Set. Then one query uses <code>IN :levels<\/code> to fetch all matching courses at once. Next, a Map counts courses per level. Finally, the last loop reads counts from the Map in memory. No matter how many records come in, the trigger runs exactly one query.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This &#8220;collect, query once, map, loop&#8221; pattern appears in almost every professional trigger. So learn it well, and practise it until you can write it from memory. Interviewers often ask candidates to fix a trigger with a query inside a loop. With Apex Collections, your answer takes less than a minute.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Building a Map Straight from a Query<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Apex has a handy shortcut. You can pass a query directly into a Map constructor. Apex then uses each record&#8217;s Id as the key.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Map&lt;Id, Course__c&gt; courseById = new Map&lt;Id, Course__c&gt;(\n    [SELECT Id, Name, Fee__c FROM Course__c WHERE Is_Online__c = true]\n);\n\nfor (Id courseId : courseById.keySet()) {\n    Course__c c = courseById.get(courseId);\n    System.debug(c.Name + ' costs ' + c.Fee__c);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">One line gives you every online course, keyed by Id. Later, any code that has an Id can find the full record instantly with <code>get<\/code>. This is one of the most used lines in all of Apex.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Saving Records with a List<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">DML also works on collections. Instead of saving records one by one, you update a List with one statement. For example, <code>update coursesToFix;<\/code> saves every course in the list at once. That counts as just one DML statement out of the 150 allowed per transaction.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Nested Apex Collections<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Apex Collections can hold other collections. The most common example is a Map whose value is a List. It groups records by a key, such as all courses for each level.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Map&lt;String, List&lt;Course__c&gt;&gt; coursesByLevel = new Map&lt;String, List&lt;Course__c&gt;&gt;();\n\nfor (Course__c c : [SELECT Name, Level__c FROM Course__c WHERE Level__c != null]) {\n    if (!coursesByLevel.containsKey(c.Level__c)) {\n        coursesByLevel.put(c.Level__c, new List&lt;Course__c&gt;());\n    }\n    coursesByLevel.get(c.Level__c).add(c);\n}\nSystem.debug(coursesByLevel.get('Beginner')?.size());<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For each course, the code creates an empty List the first time it sees a level. Then it adds the course to that level&#8217;s List. As a result, <code>coursesByLevel.get('Beginner')<\/code> returns every Beginner course in one step.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Sorting a List<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Lists have a built-in <code>sort()<\/code> method. For Strings and numbers, it sorts in ascending order. For example, a list of fees <code>{6000, 4500, 9000}<\/code> becomes <code>{4500, 6000, 9000}<\/code> after <code>fees.sort();<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Sorting records by a field needs a bit more work, such as a custom comparator class. We will cover that later. For now, the easiest way to sort records is an <code>ORDER BY<\/code> clause in your SOQL query.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with Apex Collections<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Calling <code>get<\/code> on a List with an index that does not exist. Check <code>size()<\/code> first, or you get a ListException.<\/li>\n\n<li>Expecting a Set or Map to keep a fixed order. Do not build logic on their order.<\/li>\n\n<li>Forgetting that <code>Map.get<\/code> returns null for a missing key. Use <code>containsKey<\/code> or <code>??<\/code>.<\/li>\n\n<li>Running DML on records one at a time inside a loop. Add them to a List and save once.<\/li>\n\n<li>Using a List when you need fast lookups. A Map is far quicker for finding a record by Id.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Apex Collections<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use a <code>Set&lt;Id&gt;<\/code> to gather Ids before a query, so the query never has duplicates.<\/li>\n\n<li>Build <code>Map&lt;Id, sObject&gt;<\/code> from queries for quick lookups by Id.<\/li>\n\n<li>Name collections in plural or with the key, such as <code>courses<\/code> or <code>courseById<\/code>.<\/li>\n\n<li>Check <code>isEmpty()<\/code> before a query or DML, so you skip work when there is nothing to do.<\/li>\n\n<li>Keep the &#8220;collect, query once, map, loop&#8221; pattern as your default for triggers.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Write anonymous Apex that queries all courses and builds a <code>Map&lt;String, Decimal&gt;<\/code> of total fees per level. Then print each level with its total. Hint: loop over the query, use <code>get<\/code> with <code>?? 0<\/code>, and <code>put<\/code> the new total back.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexcode.meta\/apexcode\/langCon_apex_collections.htm\" target=\"_blank\" rel=\"noopener noreferrer\">Apex Developer Guide: Collections<\/a><\/li>\n\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexref.meta\/apexref\/apex_methods_system_map.htm\" target=\"_blank\" rel=\"noopener noreferrer\">Apex Reference: Map Class<\/a><\/li>\n\n<li><a href=\"https:\/\/trailhead.salesforce.com\/content\/learn\/modules\/apex_database\" target=\"_blank\" rel=\"noopener noreferrer\">Trailhead: Apex Basics and Database<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Apex Collections hold many values under one name.<\/li>\n\n<li>A List keeps order and allows duplicates, with indexes starting at 0.<\/li>\n\n<li>Sets keep unique values and check membership quickly.<\/li>\n\n<li>A Map pairs unique keys with values for fast lookups.<\/li>\n\n<li>Collect, query once, map and loop to keep code bulk-safe.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Your code is growing, so it is time to organise it. Next, learn <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-methods-parameters-return-types\/\">Apex methods, parameters and return types<\/a> to split logic into clean, reusable pieces.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Apex Collections are variables that hold many values at once instead of just one. Apex gives you three kinds: List, Set and Map. They&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1225,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[8,96],"tags":[98,15,7,97],"swapz_series":[],"class_list":["post-1228","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-salesforce","category-salesforce-developer","tag-apex","tag-beginner","tag-salesforce","tag-salesforce-developer"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Apex Collections: List, Set and Map Explained<\/title>\n<meta name=\"description\" content=\"Apex Collections explained simply: how List, Set and Map work, their key methods, and the bulk-safe pattern every Salesforce developer uses.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/swapnilzambare.com\/blog\/apex-collections-list-set-map\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Apex Collections: List, Set and Map Explained\" \/>\n<meta property=\"og:description\" content=\"Apex Collections explained simply: how List, Set and Map work, their key methods, and the bulk-safe pattern every Salesforce developer uses.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/swapnilzambare.com\/blog\/apex-collections-list-set-map\/\" \/>\n<meta property=\"og:site_name\" content=\"Swapnil Zambare - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-03T07:30:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-collections-list-set-map-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Swapnil Zambare\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Swapnil Zambare\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-collections-list-set-map\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-collections-list-set-map\\\/\"},\"author\":{\"name\":\"Swapnil Zambare\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/922ac0131aaa7088c78c2cb1700e0c9a\"},\"headline\":\"Apex Collections: List, Set and Map Explained\",\"datePublished\":\"2026-09-03T07:30:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-collections-list-set-map\\\/\"},\"wordCount\":1534,\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-collections-list-set-map\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/apex-collections-list-set-map-featured.png\",\"keywords\":[\"Apex\",\"Beginner\",\"Salesforce\",\"Salesforce Developer\"],\"articleSection\":[\"Salesforce\",\"Salesforce Developer\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-collections-list-set-map\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-collections-list-set-map\\\/\",\"name\":\"Apex Collections: List, Set and Map Explained\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-collections-list-set-map\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-collections-list-set-map\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/apex-collections-list-set-map-featured.png\",\"datePublished\":\"2026-09-03T07:30:00+00:00\",\"description\":\"Apex Collections explained simply: how List, Set and Map work, their key methods, and the bulk-safe pattern every Salesforce developer uses.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-collections-list-set-map\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-collections-list-set-map\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-collections-list-set-map\\\/#primaryimage\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/apex-collections-list-set-map-featured.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/apex-collections-list-set-map-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Apex Collections featured image: title card with List, Set and Map code\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-collections-list-set-map\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Apex Collections: List, Set and Map Explained\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"name\":\"Swapnil Zambare - Blog\",\"description\":\"Salesforce &amp; Tech Guides\",\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\",\"name\":\"Swapnil Zambare - Blog\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"width\":512,\"height\":512,\"caption\":\"Swapnil Zambare - Blog\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/theSwapnilZambare\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/922ac0131aaa7088c78c2cb1700e0c9a\",\"name\":\"Swapnil Zambare\",\"sameAs\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\"],\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/author\\\/swapnil-zambare\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Apex Collections: List, Set and Map Explained","description":"Apex Collections explained simply: how List, Set and Map work, their key methods, and the bulk-safe pattern every Salesforce developer uses.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/swapnilzambare.com\/blog\/apex-collections-list-set-map\/","og_locale":"en_US","og_type":"article","og_title":"Apex Collections: List, Set and Map Explained","og_description":"Apex Collections explained simply: how List, Set and Map work, their key methods, and the bulk-safe pattern every Salesforce developer uses.","og_url":"https:\/\/swapnilzambare.com\/blog\/apex-collections-list-set-map\/","og_site_name":"Swapnil Zambare - Blog","article_published_time":"2026-09-03T07:30:00+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-collections-list-set-map-featured.png","type":"image\/png"}],"author":"Swapnil Zambare","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Swapnil Zambare","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/swapnilzambare.com\/blog\/apex-collections-list-set-map\/#article","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-collections-list-set-map\/"},"author":{"name":"Swapnil Zambare","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/922ac0131aaa7088c78c2cb1700e0c9a"},"headline":"Apex Collections: List, Set and Map Explained","datePublished":"2026-09-03T07:30:00+00:00","mainEntityOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-collections-list-set-map\/"},"wordCount":1534,"publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-collections-list-set-map\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-collections-list-set-map-featured.png","keywords":["Apex","Beginner","Salesforce","Salesforce Developer"],"articleSection":["Salesforce","Salesforce Developer"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/swapnilzambare.com\/blog\/apex-collections-list-set-map\/","url":"https:\/\/swapnilzambare.com\/blog\/apex-collections-list-set-map\/","name":"Apex Collections: List, Set and Map Explained","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-collections-list-set-map\/#primaryimage"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-collections-list-set-map\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-collections-list-set-map-featured.png","datePublished":"2026-09-03T07:30:00+00:00","description":"Apex Collections explained simply: how List, Set and Map work, their key methods, and the bulk-safe pattern every Salesforce developer uses.","breadcrumb":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-collections-list-set-map\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/swapnilzambare.com\/blog\/apex-collections-list-set-map\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/apex-collections-list-set-map\/#primaryimage","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-collections-list-set-map-featured.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-collections-list-set-map-featured.png","width":2400,"height":1260,"caption":"Apex Collections featured image: title card with List, Set and Map code"},{"@type":"BreadcrumbList","@id":"https:\/\/swapnilzambare.com\/blog\/apex-collections-list-set-map\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/swapnilzambare.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Apex Collections: List, Set and Map Explained"}]},{"@type":"WebSite","@id":"https:\/\/swapnilzambare.com\/blog\/#website","url":"https:\/\/swapnilzambare.com\/blog\/","name":"Swapnil Zambare - Blog","description":"Salesforce &amp; Tech Guides","publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/swapnilzambare.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/swapnilzambare.com\/blog\/#organization","name":"Swapnil Zambare - Blog","url":"https:\/\/swapnilzambare.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","width":512,"height":512,"caption":"Swapnil Zambare - Blog"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.linkedin.com\/in\/theSwapnilZambare"]},{"@type":"Person","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/922ac0131aaa7088c78c2cb1700e0c9a","name":"Swapnil Zambare","sameAs":["https:\/\/swapnilzambare.com\/blog"],"url":"https:\/\/swapnilzambare.com\/blog\/author\/swapnil-zambare\/"}]}},"_links":{"self":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/1228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/comments?post=1228"}],"version-history":[{"count":0,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/1228\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media\/1225"}],"wp:attachment":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media?parent=1228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/categories?post=1228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/tags?post=1228"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=1228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}