ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

Member-only story

Mastering Dart Collections

Denis Crăciunescu
ProAndroidDev
Published in
6 min readJul 27, 2021

Photo by icons8

Collections are a set of interfaces and classes that implement highly optimised data structures. They reduce the programming effort and improve the performance of the code if chosen carefully.

In this article, you will see the strengths and weaknesses of all the collections to enable you to choose the perfect fit for your use case.

Choosing the right collection solves 90% of the problem — managing it is the remaining part and it comes naturally after selecting the right one.

Before jumping into the more complex collections available, let’s have a look at the most common collection types in Dart.

Common Collection Types

The main collection types in Dart are:

  • List
  • Set
  • Map

Lists are 0-indexed collections of objects with a length.
In other programming languages, they are called arrays.
Lists are Iterable and the iteration occurs in the index order. If the length of the list is changed during an iteration, a ConcurrentModificationError is thrown.

Sets are collections of objects in which every object can only occur once.
Two objects are considered indistinguishable, hence can occur only once, if they are equal with regard to the Object.== operator.
Sets are Iterable and the order of iteration depends on the specific Set implementations.

Maps are collections of key/value pairs, from which you retrieve a value using its associated key.
Every key in the map is unique.
Maps and the keys and values are Iterable and the order of iteration depends on the specific Map implementations.

Collection Literals

Collection literals are a syntactic expression that evaluates to an aggregate collection type.

In Dart, collection literals are preferred over unnamed constructor instantiation of collections. (see relevant Effective Dart tip)

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Write a response