No one uses collections better than I do! Yes, it's a collection in dart

brief introduction

There are three sets in dart: list, set and map. Dart provides very useful methods for these three collections in the dart:core package. Let's take a look.

Use of List

First, create a list. You can create an empty list or a list with values:

var emptyList =[];

var nameList = ['jack','mac'];

Use the constructor of List to create:

var nameList = List.filled(2, 'max');

Add an element or list to the list:

nameList.add('tony');
nameList.addAll(['lili', 'bruce']);

Delete element:

nameList.removeAt(0);
nameList.clear();

dart provides a sort() method for list s. Sort can be followed by a comparison function to indicate who is first and who is last:

var names = ['jack', 'tony', 'max'];

fruits.sort((a, b) => a.compareTo(b));

Generic types can also be used in the list to represent the fixed types in the list:

var names = <String>[];

names.add('jack');

Use of Set

Set represents a collection of non repeating elements. But the difference between set and list is that set is unordered, so you can't use index to locate the elements in set.

Let's look at the basic usage of set:

//Create an empty set
var names = <String>{};

// Add new element
names.addAll(['jack', 'tony', 'max']);

//Delete element

names.remove('jack');

Or use the constructor of Set to construct Set:

var names = Set.from(['jack', 'tony', 'max']);

Determine whether the elements in the collection exist:

assert(names.contains('jack'));

assert(names.containsAll(['jack', 'tony']));

set also has an intersection operation to find the intersection of two sets:

var name1 = Set<String>();
name1.addAll(['jack', 'tony', 'max']);

var name2 = Set.from(['tony', 'bily']);
var intersection = name1.intersection(name2);

Use of Map

Map is a data type of key and value, and it is also a very common data type in programs.

Let's first look at how to create a Map:

// Create map
var studentMap = {
  'name': 'jack',
  'age': '18',
  'class': 'class one'
};


var teacherMap = Map();

var teacherMap2 = Map<String, String>();

Add and remove:

  var studentMap =Map();
  studentMap.putIfAbsent('name', ()=>'jack');
  studentMap.remove('name');

To determine whether a key is included in the map, you can use containsKey():

assert(studentMap.containsKey('name'));

Common collection methods

The most common method in a collection is to determine whether the collection is empty:

assert(studentMap.isEmpty);
assert(studentMap.isNotEmpty);

If you want to perform a function operation on each element in the collection, you can use forEach():

var names = ['jack', 'bob', 'tom'];

names.forEach((name) => print('the name is $name'));

var nameMap = {};

nameMap.forEach((k, v) {
});

If it is a traversable object, there is a map method, and the map method will return a new object:

var names = ['jack', 'bob', 'mark'];

var names2 = names.map((name) => name.toUpperCase());

Note that the map returns an iteratable, which is calculated with delay. It will be calculated only when it is used.

If you want to calculate immediately, you can use map().toList() or map().toSet():

var names2 =
    names.map((name) => name.toUpperCase()).toList();

Traversable objects can also make conditional selection. For example, use where() to get all matching objects, any() to judge whether there are matching objects in the collection, and every() to judge whether all objects in the collection match.

var names = ['jack', 'bob', 'max'];

bool hasJack(String name) =>
    name == 'jack';

var seleteJack =
    names.where((name) => hasJack(name));

assert(names.any(hasJack));

assert(!names.every(hasJack));

summary

Set is a very common type in the process of programming. We must master it skillfully.

This article has been included in http://www.flydean.com/15-dart-collection/

The most popular interpretation, the most profound dry goods, the most concise tutorial, and many tips you don't know are waiting for you to find!

Welcome to my official account: "those things in procedure", understand technology, know you better!

Tags: Flutter set dart

Posted on Thu, 25 Nov 2021 21:14:24 -0500 by misschristina95