VOOZH about

URL: https://www.geeksforgeeks.org/dart/dart-collections/

⇱ Dart - Collections - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Dart - Collections

Last Updated : 11 Apr, 2025

Collections are groups of objects that represent a particular element. The dart::collection library is used to implement the Collection in Dart. There are a variety of collections available in Dart.

Dart Collection

There are 5 Interfaces that we have in the Dart Collection, as mentioned below:

  • List
  • Queue
  • Set
  • Map
  • LinkedList

👁 collection-in-dart-new

1. List Interface

The list is an ordered group of objects where each object is from one specific type. To define a list in Dart, specify the object type inside the angled brackets (<>).

Syntax:

List<String> fruits = ["Mango", "Apple", "Banana"]

Example: 

Here, we have defined a list and performed some common operations along with some basic, commonly used methods.


Output:

[1, 2, 3, 4, 5, Apple]
element 0 is 1
element 1 is 2
element 2 is 3
element 3 is 4
element 4 is 5
element 5 is Apple
[1, 2, 4, 5]
(5, 4, 2, 1)
false
1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


Classes Associated with List Interface

Class

Description

UnmodifiableListView<E>

An unmodifiable List view of another List.


2. Set Interface

Sets are one of the essential part of Dart Collections. A set is defined as an unordered collection of unique objects.

Syntax:

Set<String> fruits = {"Mango", "Apple", "Banana"};

Example:

As discussed earlier a set stores a group of objects that are not repeating. A sample program is shown below.

Output:

9
1
2
3
4
5
6
Length: 7
First Element: 9
{1, 2, 3, 4, 5, 6}


Classes Associated with Set Interfaces

Classes

Description

Set<E>

Collection of objects in which each of the objects occurs only once

HashSet<E>

Unordered set backed by a hash table

LinkedHashSet<E>

Ordered set that maintains insertion order

SplayTreeSet<E>

Automatically sorted set using a self-balancing binary search tree

UnmodifiableSetView<E>

Read-only view of a set


Hashset:

In the Dart programming language, a HashSet is a collection that stores a set of unique elements, where each element can be of any type. Here is an example of how to use a HashSet in Dart:

 Output:

true
false
apple
banana
cherry


3. Map Interface

In Dart, Maps are unordered key-value pair collections that set an associate key to the values within. To define a Map, specify the key type and the value type inside the angle brackets(<>) as shown below:

Syntax:

Map<int, String> fruits = {1: "Mango", 2: "Apple", 3: "Banana"};

Example:

The map collection stores the objects as a key-value pair. An example is shown below.

Output:

{1: Apple, 2: Mango, 3: Banana}
{1: Apple, 2: Grapes, 3: Banana, 4: Pineapple, 9: Kiwi}
Keys: (1, 2, 3, 4, 9)
Values: (Apple, Grapes, Banana, Pineapple, Kiwi)
{{1: Apple, 3: Banana, 4: Pineapple, 9: Kiwi}} length is 4


Classes Associated with Map Interfaces

Classes

Description

MapBase<K, V>

This is the base class for Map

HashMap<K, V>

Unordered key-value store

LinkedHashMap<K, V>

Maintains insertion order

SplayTreeMap<K, V>

Sorted map based on keys

UnmodifiableMapBase<K, V>

Base class for read-only maps

UnmodifiableMapView<K, V>

Read-only view of a map

i. Hashmap 

In the Dart programming language, a HashMap is a collection that stores key-value pairs, where the keys are unique and the values can be of any type. Here is an example of how to use a HashMap in Dart:

Output:

 one
1: one
2: two
3: three


4. Queue Interface

Queues are used to implement FIFO(First in First Out) collection. This collection can be manipulated from both ends.

Syntax:

import 'dart:collection';

Queue<String> queue = Queue.from(["Mango", "Apple", "Banana"]);

Example:


Output:

{GFG, 9, 1, 2, 3, 4, 5, 6, 1, 1, 9, GFG2}
{9, 1, 2, 3, 4, 5, 6, 1, 1, 9, GFG2}
{9, 1, 2, 3, 4, 5, 6, 1, 1, 9}
First Element: 9
9
1
2
3
4
5
6
1
1
9
Length: 10
{9, 1, 3, 4, 5, 6, 1, 1, 9}

Classes Associated with the Queue Interface

Classes

Description

DoubleLinkedQueue<E>

Doubly-linked list based on the queue data structure.


5. LinkedList Interface

It is a specialized, double-linked list of elements. This allows const time adding and removing at the either end also the time to increase the size is constant.

Syntax:

class MyEntry extends LinkedListEntry<MyEntry> {
final String value;
MyEntry(this.value);
}

LinkedList<MyEntry> list = LinkedList<MyEntry>();

Example:

Below is the implementation of a Doubly Linked List in Dart:

Output:

First Box , 1
Second Box , 2
Third Box , 3

After removal:
First Box , 1
Third Box , 3

Classes Associated with LinkedList Interface

Class

Description

LinkedListEntry<E extends LinkedListEntry<E>>

An element of a LinkedList.

Comment
Article Tags:
Article Tags:

Explore