Skip to content
tezvyn:

Dart Collections: Choosing List, Set, or Map

Source: dart.devMediumHow cards are made

Dart collections organize data: use a List for ordered items, a Set for unique items, and a Map for key-value pairs. This choice is fundamental for storing UI widgets or parsing JSON. The common footgun is using a List for lookups, which is slow; use.

Why it exists

Programs need to store groups of related objects. But "a group" can mean an ordered sequence, a collection of unique items, or a dictionary-style lookup table. Dart provides specialized collections for each job to ensure code is both correct and performant, preventing bugs and slow operations.

The mental model

Think of collections as different containers. A List is a numbered rack of shoeboxes; you access items by their position (index). A Set is a bag of marbles; you can quickly check if a specific marble is in the bag, but there are no duplicates and no guaranteed order. A Map is a dictionary; you look up a word (key) to find its definition (value).

How it works

A List is an ordered, indexable collection, like an array in other languages. It allows duplicates and is created with square brackets: var scores = [98, 87, 98];. You access elements by index, like scores[0].

A Set is an unordered collection of unique items. It's created with curly braces: var tags = {'flutter', 'dart'};. Adding an existing element does nothing. Its main job is fast existence checks.

A Map is a collection of key-value pairs. Each key must be unique. It's created with curly braces and colons: var user = {'id': 123, 'name': 'Alex'};. It provides very fast value lookups based on a key, like user['name'].

When to use it

Use a List when order matters, like items in a ListView, steps in a wizard, or a history of events. Use a Set when uniqueness is critical, like tracking selected filter options or de-duplicating items from another source. Use a Map when you need to associate one value with another, like parsing JSON data, storing app settings, or caching objects by their ID.

When not to use it

The biggest footgun is using a List when you need fast lookups. Calling List.contains() on a large list is very slow because it must check every element. If you need to frequently check for an item's existence, use a Set or a Map, which are optimized for this and provide near-instantaneous lookups.

One canonical example

Imagine a shopping cart. The items in the cart are a List, because the user might add the same item twice and their order of addition might matter. The set of unique products available in the store could be a Set, for quick checks on availability. A single product's details (like 'price', 'name', 'SKU') would be stored in a Map.

Interview question

You need to store a collection of unique product SKUs that are currently in stock and frequently check if a particular SKU is available. Which Dart collection is the most efficient choice for this task?

  • a.Set, because it guarantees uniqueness and optimizes for fast existence checks.Correct
  • b.List, because it allows for easy iteration over all SKUs.
  • c.Map, using the SKU as the key and a boolean as the value to indicate availability.
  • d.An ordered List, to quickly find SKUs using binary search if sorted.
Why?

Set is specifically designed for storing unique items and provides highly efficient checks for an item's existence, making it ideal for this scenario. While a Map can also provide fast lookups, a Set is the most direct and efficient choice when only uniqueness and existence checking are required, without needing to associate a value.

Just read this? Test yourself on what you have been reading.

Read the original → dart.dev

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on dart — each one lists the topics its interview covers.

See open roles