集合是代表特定元素的对象组。 dart::collection 库用于实现dart的集合。 dart提供了多种集合。
Dart Collection 的一些类是 –
- List
: List 是一组有序的对象。 - Set
: 每个对象只出现一次的对象集合 - Map
: 具有简单的基于键/值对的对象的对象集合。映射的键和值可以是任何类型。 - Queue
: Queue 是一个可以在两端操作的集合。可以使用 Iterator 或使用 forEach 遍历队列。 - DoubleLinkedQueue
: 基于队列数据结构的双向链表。 - HashMap
: 基于哈希表的映射,即无序映射。 - HashSet
: 基于哈希表的集合,即无序集合。 - LinkedHashMap
: 类似于HashMap,但基于LinkedList。 - LinkedHashSet
: 类似于 HashSet 但基于 LinkedList。 - LinkedList
>: 它是一个特殊的元素双链表。 - LinkedListEntry
>: LinkedList 的一个元素。 - MapBase
: 这是 Map 的基类。 - UnmodifiableListView
: 另一个 List 的不可修改的 List 视图。 - UnmodifiableMapBase
: 不可修改 Map 的基本实现。 - UnmodifiableMapView
: 地图的不可修改视图。
我们将在此处通过示例讨论 4 个基本集合。
1.列表
该列表是一组有序的对象,其中每个对象都来自一种特定类型。要在dart定义列表,请在尖括号 (<>) 内指定对象类型,如下所示:
List fruits = ["Mango", "Apple", "Banana"]
例子:
这里我们定义了一个列表,并执行了一些常用的操作以及一些基本的常用方法。
Dart
void main() {
// creating a new empty List
List geekList = new List();
// We can also create a list with a predifined type
// as List sampleList = new List()
// and also define a list of fixed length as
// List sampleList = new List(5)
// Adding an element to the geekList
geekList.addAll([1,2,3,4,5,"Apple"]);
print(geekList);
// Looping over the list
for(var i = 0; i.generate(10).toList();
print(geekList);
}
Dart
void main() {
// Initializing the Set and Adding the values
Set geekSet = new Set();
geekSet.addAll([9,1,2,3,4,5,6,1,1,9]);
// Looping over the set
for(var el in geekSet){
print(el);
}
// length of the set.
print('Length: ${geekSet.length}');
// printing the first element in the set
print('First Element: ${geekSet.first}');
// Deleting an element not present. No Change
geekSet.remove(10);
// Deleting an element 9
geekSet.remove(9);
print(geekSet);
}
Dart
void main() {
// Intializing the map with sample values.
var geekMap = {1:"Apple",2:"Mango",3:"Banana"};
print(geekMap);
// Adding elements by different methods.
geekMap.addAll({4:'Pineapple',2:'Grapes'});
geekMap[9]="Kiwi";
print(geekMap);
// printing key and values
print('Keys: ${geekMap.keys} \nValues: ${geekMap.values}');
// removig an element from the map by its key
geekMap.remove(2);
// printing the map and its length
print('{$geekMap} length is ${geekMap.length}');
}
Dart
import 'dart:collection';
void main() {
// Initializing the Set and Adding the values
// We can also initialize a queue of a specific type
// as Queue q = new Queue();
var geekQueue = new Queue();
geekQueue.addAll([9,1,2,3,4,5,6,1,1,9]);
// Adds Element to the Start of the Queue
geekQueue.addFirst("GFG");
// Adds Element to the End of the Queue
geekQueue.addLast("GFG2");
print(geekQueue);
// Removes the first Element
geekQueue.removeFirst();
print(geekQueue);
// Removes the Last Element
geekQueue.removeLast();
print(geekQueue);
// printing the first element in the set
print('First Element: ${geekQueue.first}');
// Looping over the set
for(var el in geekQueue){
print(el);
}
// Other Operations
// length of the set.
print('Length: ${geekQueue.length}');
// Deleting an element not present. No Change
geekQueue.remove(10);
// Deleting an element 9
geekQueue.remove(2);
print(geekQueue);
}
输出:
[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]
2. 设置
Sets 是Dart Collections 的重要组成部分之一。集合被定义为唯一对象的无序集合。要定义一个集合,请遵循以下步骤:
Set fruits = Set.from("Mango", "Apple", "Banana")
例子:
如前所述,集合存储一组不重复的对象。示例程序如下所示。
Dart
void main() {
// Initializing the Set and Adding the values
Set geekSet = new Set();
geekSet.addAll([9,1,2,3,4,5,6,1,1,9]);
// Looping over the set
for(var el in geekSet){
print(el);
}
// length of the set.
print('Length: ${geekSet.length}');
// printing the first element in the set
print('First Element: ${geekSet.first}');
// Deleting an element not present. No Change
geekSet.remove(10);
// Deleting an element 9
geekSet.remove(9);
print(geekSet);
}
输出:
9
1
2
3
4
5
6
Length: 7
First Element: 9
{1, 2, 3, 4, 5, 6}
3. 地图
在Dart,Maps 是无序的键值对集合,它为其中的值设置了一个关联键。要定义 Map,请在尖括号 (<>) 内指定键类型和值类型,如下所示:
Map fruits = {1: "Mango", 2:"Apple", 3:"Banana"}
例子:
地图集合将对象存储为键值对。一个例子如下所示。
Dart
void main() {
// Intializing the map with sample values.
var geekMap = {1:"Apple",2:"Mango",3:"Banana"};
print(geekMap);
// Adding elements by different methods.
geekMap.addAll({4:'Pineapple',2:'Grapes'});
geekMap[9]="Kiwi";
print(geekMap);
// printing key and values
print('Keys: ${geekMap.keys} \nValues: ${geekMap.values}');
// removig an element from the map by its key
geekMap.remove(2);
// printing the map and its length
print('{$geekMap} length is ${geekMap.length}');
}
输出:
{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
4. 队列
队列用于实现 FIFO(先进先出)收集。这个集合可以从两端操作。 dart的队列定义如下:
Queue queue = new Queue("Mango", "Apple","Banana")
例子:
Dart
import 'dart:collection';
void main() {
// Initializing the Set and Adding the values
// We can also initialize a queue of a specific type
// as Queue q = new Queue();
var geekQueue = new Queue();
geekQueue.addAll([9,1,2,3,4,5,6,1,1,9]);
// Adds Element to the Start of the Queue
geekQueue.addFirst("GFG");
// Adds Element to the End of the Queue
geekQueue.addLast("GFG2");
print(geekQueue);
// Removes the first Element
geekQueue.removeFirst();
print(geekQueue);
// Removes the Last Element
geekQueue.removeLast();
print(geekQueue);
// printing the first element in the set
print('First Element: ${geekQueue.first}');
// Looping over the set
for(var el in geekQueue){
print(el);
}
// Other Operations
// length of the set.
print('Length: ${geekQueue.length}');
// Deleting an element not present. No Change
geekQueue.remove(10);
// Deleting an element 9
geekQueue.remove(2);
print(geekQueue);
}
输出:
{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}
Documentation