Add Your Heading Text Here

Collections are the data structures(i.e., specialised format for storing and managing diffrent types of data) which are used to store and manipulate the group of obects.

general collections supported by dart are as below,

  • List
  • Map
  • Set

out of which list and map have been already covered in previous tutorials.

What is Set ?

Dart Set is a collection of unique objects or items.

Dart set is an unordered collection as it not allows you to add elements or items at particular index or position in the set.

So, traversing to different position or elements in set is difficult.

If you try to add any item in the set which is already present then it will not be inserted or stored in the set. Which means set doesn’t allow the duplicate entries while inserting.

Set Common Properties

For ex :

void main(){  
  Set languages=new Set<String>();
  languages.add("C");
  languages.add("C++");
  languages.add("Java");
  languages.add("Dart");
  languages.add("C++");
  languages.add("Java");
  
  Set humanLanguage=new Set<String>();
  humanLanguage.add("English");
  humanLanguage.add("Hindi");
  humanLanguage.add("Gujarati");
  humanLanguage.add("C++");
  humanLanguage.add("Java");
  
  //duplicate values not allowed in set
  print(languages.toSet());
  print(languages.toList());
  print(languages.length);
  
  print(humanLanguage.length);
  print(humanLanguage.toSet());
  
  print(humanLanguage.contains("English"));
  print(humanLanguage.remove("Hindi"));
  print(humanLanguage.union(languages));
  print(humanLanguage.intersection(languages));
  print(humanLanguage.difference(languages));
  humanLanguage.clear();
  print(humanLanguage.length);
}

Output :

{C, C++, Java, Dart}
[C, C++, Java, Dart]
4
5
{English, Hindi, Gujarati, C++, Java}
true
true
{English, Gujarati, C++, Java, C, Dart}
{C++, Java}
{English, Gujarati}
0

Set collection have many methods which makes your collection handy to use.

Set is generally used to make your collection having unique values of same data type.

Make yourself comfortable with collections like List, Map and Set to better understand the different data structures.

Feel free to ask your questions in the comment section. Keep reading and commenting your suggestions to make toastguyz a better site to learn different programming languages.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Show Buttons
Hide Buttons