Conversation


Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!

Join the discussion and share your insights now!

Comments 0


Collections in Dart Programming and their Usage

collections in dart


Collections are used to store, retrieve, and communicate mass data. They represent data items that form a natural cluster, such as a mall folder (assortment of letters), or a directory (a mapping of names to phone numbers). A collection is an entity that represents a group of objects called elements. The dart:core library enables support for collections in Dart and can be added to a Dart script using the import keyword.

Highly optimized data structures are formed as a result of collections which are nothing but groups of classes and interfaces. Reduction in effort required for programming and improvement in the performance of code can be achieved if collections are chosen wisely.

Types of Collections in Dart

Following collections in Dart:

  • List
  • Set
  • Map
  • HashSet
  • LinkedHashSet
  • LinkedHashMap
  • LinkedList
  • HashMap
  • NestedCollections
  • Collection-if
  • Collection-for

1. List:

A list is an ordered group of objects. A list class that enables the creation and modification of a list is provided by dart:core library.

The list can subdivided into two types:

  • Fixed Length List: This Kind of List cannot be changed or modified at runtime. The size of the List is specified during initiation and cannot be modified later.

void main(){
List listname = List.filled(2, []);
listname[0] = 50;
listname[1] = 100;
print(listname);
}


  • Growable Length List: This specific List can be altered or modified at runtime, Specifying the size of the list is not required during the initialization of a new list to a variable. This means any number of elements can be added or removed from the list.

void main(){
var listname = [10, 20, 30, 40];
listname.add(50);
print(listname);
}



2. Set:

A set is a unique, unordered collection of different values of the same data type. These values can occur only once in a set. The dart:core library provides the set class. This has to be enabled for creation and then modification is done using the dart:core library.

void main(){
Set listname = new Set();
listname.add(10);
listname.add(20);
listname.add(30);
listname.add(40);
listname.add(50);
print(listname);
}



3. Map:

The map is a dynamic collection with a simple key-value pair. map supports all kinds of data types and every key inside a map must exist in such a way that they are unique or different from each other.

Following are the two separate ways to declare a Map:

  • Map Literals: Map is declared by enclosing key-value pairs inside a pair of curly braces{}.

void main(){
var userdetails = {'name' : 'John', 'age' : '20'};
print(userdetails);
}


  • Map Constructor: Map instance is declared and then, initialized. In a key value. The key is included, the key is included in the map using brackets, and the value is included in the key.

void main(){
var details = new Map();
details['Name'] = 'John';
details['Age'] = '20';
print(details);
}



4. HashSet:

HashSet is a hash table that has an unordered set implementation, if value x is inserted before value y in a HashSet, it is possible to get value y before value x upon iterating the elements. HashSet is provided by dart:collection library in Dart.

import 'dart:collection';

void main(){
Set numbers = new HashSet();
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.add(40);
numbers.add(50);
print(numbers);
}



5. HashMap:

HashMap is a hash-table Map implementation that is unordered. If key x is inserted before key y in a hashMap it is possible to get key y before key x upon iterating the elements. HashMap is provided by dart:collection library in Dart.

import 'dart:collection';

void main(){
Map details = new HashMap();
details['Name'] = 'John'; 
details['Age'] = '20';
print(details);
}



6: Linked List:

A LinkedList is a sequential data structure that is linear. The elements that extend the LinkedListEntry class can be put in a LinkedList. Although the name includes List, this class does not implement the List interface. It is unidirectional and has several advantages compared to a Dart list. Some of these include non-contiguous storage in memory, faster insertions and removals, and better performance.



7. LinkedHashMap:

The LinkedHashMap executes data based on the order in which it was sorted, quite similar to the insertion order. If one adds the x key first and then, another key y, it is possible to receive the x value first when the iteration is done. LinkedHashMap can store one null key and multiple null values.

import 'dart:collection';

void main(){
var list = new LinkedHashMap();
list['1'] = 'Alice';
list['2'] = 'Bob';
list['3'] = 'John';
list['4'] = 'Patrick';
print(list);
}



8. LinkedHashSet:

The LinkedHashSet executes data in the order they are inserted. If one adds the x value first and then, inserts another value y, the x value will be received before the y value during iteration. LinkedHashSet stores only one null value.

import 'dart:collection';

void main(){
var list = new LinkedHashSet();
list.add('John');
list.add('Parick');
list.add('sam');
print(list);
}



9. Nested Collections:

A collection of collections is termed a nested collection. Another interpretation of this concept is a multi-dimensional array, which is a group of rows and columns. A nested collection can be conceptually thought of as a multi-dimensional array.

This can be implemented in Dart by using List, Set, Map, HashMap, HashSet, And LinkedList. In Nested Collections, unlimited rows and columns can be implemented and hence, are useful for the creation of matrices.

import 'dart:collection';
void main(){
 List listname = List.generate(2,(_) => List.generate(2, (_) => 0));
 print(listname);
}



10. Collection-if:

The collection of objects is possible to be checked and manipulated using if conditions. This is usually used when modification of the collection is required for values based on certain conditions.

import 'dart:collection';
void main(){
List listname = [];
listname.add(10);
listname.add(20);
listname.add(30);
listname.add(40);
listname.add(40);
print(listname[0]);
if(listname[0] < listname[1]){
listname[0] = listname[0] + 10;
print(listname[0]);
}
}



11. Collection-for:

It is a collection of objects that can be looped over. the elements or the collection of objects can be obtained using for and for-each loops by which all the elements in a list or map can be iterated.

void main() {
  List<int> listname = [];
  listname.add(10);
  listname.add(20);
  listname.add(30);
  listname.add(40);
  listname.add(50);
  for (var i in listname) {
    print(i);
  }
}


Dart Collections collection in Dart Dart Programming Types of Collections
Blogs