How to manipulate Sets Lists and Maps (Class 14)

How to manipulate Sets Lists and Maps

After learning collections, it is important to know how to use or manipulate Sets, Lists, and Maps. So we are going to learn them in detail today. Let’s start.

 

Length of a List

A list contains ordered items and there are specific numbers of items in a list. For example:-

List<int> list = [10,20,30,40,50,60,70];

Length = 7

 

Index Number Concept

Every list has a unique number which is called an index number. We can access any list item which has an index number. There is also an index concept in Flutter in the bottom bar, list view, tabs, etc. List items are in memory like boxes in a train. In the index, the first item is 0 and then 1 onwards.  For example, if there are 10 items then the index will be from 0 to 9. We can do operations using index numbers.

 

Is there any Index in Set and Map?

In comparison, sets and maps are in an unordered form and can be in different places in memory. So there is no sequence or index in them. In the map, we access through its key.

 

How to print List, Set & Map?

In the last class, we learned a for-in loop. Now we will discuss in detail and will understand how to print collections.

 

Printing List

We can print all items of a list one by one using for in loop. For example:

void main() {
  List<int> list = [10, 20, 30, 40, 50, 60, 70];
  for (var item in list) {
    print(item);
  }
}

 

As the above items are in integer form we can also use int instead of var like:

for (int item in list) {
  print(item);
}

 

Printing Set

Similarly, we can print Set items as shown below:

void main() {
  Set <int> set = {23,223,324,3278, 2675};
  for (int item in set) {
    print(item);
  }
}

 

Printing Map

Now the scenario of Map comes. As you know there are two things on the map, i.e. keys and values. So we can print separately keys and values like:

Keys

void main() {
  Map map = {“name”:”Ali”,”roll_no”:45};
  for (String item in map.keys) {
    print(item);
  }
}

 

Values

void main() {
  Map map = {“name”:”Ali”,”roll_no”:45};
  for (dynamic item in map.values) {
    print(item);
  }
}

 

Whole Map

We can also print the whole map containing keys and values using the following code:

void main() {
  Map map = {“name”:”Ali”,”roll_no”:45};
  for (var item in map.entries) {
    print(item);
  }
}

 

Built-in Functions for Collections in Dart

Reversing list items:

We can reverse list items using .reversed. For example:-

void main() {
  List list = [12,234,234,23423,34];
  list.reversed;
  print(list.reversed);
}

 

How to find length:

We can find the length of a List, Set, or Map using .length. For example:

void main() {
  List list = [12,234,234,23423,34];
  Set set = {23,535,5643,675,237,21};
  Map map = {“name”:”Ali”,”roll_no”:45};
  print(“List’s length is ${list.length}”);
  print(“Set’s length is ${set.length}”);
  print(“Map’s length is ${map.length}”);
}

 

How to add items in List, Set, Map

In List

We can add items by using .add. Suppose we have a blank list we will add three items to that list with the following code:-

void main() {
  List list2 = [];
  list2.add(2323);
  list2.add(21);
  list2.add(21);
  print(list2);

}

 

In Set

We can do it similarly for a Set as under:

void main() {
  Set set2 = {};
  set2.add(123);
  set2.add(57);
  set2.add(13);
  print(set2);
}

 

In Map

 For a Map we need to do it with values and keys using .all as shown under:-

void main() {
  Map map2 = {};
  map2.addAll({“name”:”Ali”,”roll_no”:45, “gpa”:.34});
  print(map2);
}

 

Edit/ update items to List, Set, Map

In List

We can edit or update List items in the following manner:

void main() {
  List list3 = [12,234,234,23423,34];
  list3[3] = 321;
  list3[4] = 123;
  print(list3);
}

 

In Set 

It is to be noted that there is no direct way to update/ edit Set items. However, we can do it by converting List to Set as under:-

void main() {
  Set set3 = {23,535,5643,675,237,21};
  List set3change = set3.toList();
  set3change[2] = 232;
  print(set3change);
}

 

In Map

For a map, we can edit by using both key and value in our code as given under:

void main() {
  Map map = {“name”:”Ali”,”roll_no”:45};
  map[‘name’] = “Hamza”;
  print(map[‘name’]);
}

 

Removing items from List, Set & Map

In List

We can remove items in a list by using .removeAt as given under:-

void main() {
  List list5 = [12,234,234,23423,34];
  list5.removeAt(3);
  print(list5);
}

 

In Set

For the set, we will enter the value as shown below:

void main() {
  Set set4 = {23,535,5643,675,237,21};
  set4.remove(675);
  print(set4);
}

Try yourself in Dart Pad.

 

In Map

For the map, we will use a key with .remove

void main() {
  Map map4 = {“name”:”Ali”,”roll_no”:45};
  map4.remove(“name”);
  print(map4);
}

 

It is very important to do it yourself to understand how to manipulate Sets, Lists, and Maps. Best of luck!!

 

ROBINA KOUSAR
Content Writer
**************************
You can get all Mobile App Development Classes link from here:-

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.