loading

Dart Collection

Unlike other programming languages, Dart does not offer arrays for data storage. Array data structures can be replaced with the Dart collection. Using the dart::core library, we can make the other classes in the collection available in our Dart script.

The following categories apply to dart collections.

Dart CollectionDescription
ListA list is the collection of the ordered group of collection. The dart::core library offers the list class that allows us to create and modify the list. It provides the following types of lists.
  • Fixed Length List – We cannot change the list’s length at runtime.
  • Growable List – We can change the length of the list at run-time.
SetA set is the collection of the objects in which each object can be declared at once. The dart::core library offers the Set class to use its facilities.
MapsThe maps are the collection of the key-value pair of data. Each value is stored with a particular key. The key and value can be any type in the dart. A Map is a dynamic collection. We can say that map can be modified at the run-time. The dart::core library makes available the Map class to work with it.
QueueA queue is the collection of the where data stores in the first-in-first-out format. It can be manipulated at both ends. Simply, we can enter the element from one end and delete it from another end.

Iterating Collections

The iterator class, made possible by the dart::core package, makes collection traversal simple. Every collection has an iterator property, as far as we are aware. An iterator pointing to the collection’s objects is returned by this property. Let’s examine the next illustration.

Example

				
					import 'dart:collection';   
void main() {   
   Queue que = new Queue();   
   que.addAll([10,20,30]);    
   Iterator i= que.iterator;   
     
   while(i.moveNext()) {   
      print(i.current);   
   }   
}  
				
			

Output

				
					10
20
30
				
			

Explanation

The moveNext() function in the code above produced a Boolean value denoting whether or not there is a further item. The object that the iterator presently points to is returned by the current property of the.

HashMap

The Map’s implementation serves as the foundation for the HashMap class. The key needs to be unique and have consistent implementations of Object.hashCode and Object == (equal to operator), as we covered earlier. Null is another option for a key. The Map’s components can be arranged in any sequence. Only when the map is altered does the order of iterations alter. The map’s values are iterated in the same order as their corresponding keys if the map is iterated.

Share this Doc

Dart Collection

Or copy link

Explore Topic