Set_(Collection)_Dart
void main()
{
//There are two ways to declare Set as shown below :
//1) Set<Int> ratings = {}; (Here, "ratings" is name of Set)
//2) var ratings = <Int>{};
/*Set<int> ratings = {};
var ratings1 = <int>{};*/
///NOTE : Set will always remove(will not print duplicate elements) duplicate
/// elements from the Set elements
var theSet = {10,20,30,40,50,60,20,70,50,10,80,30,90,100};
print(theSet);
print("\n");
///Getting the Number of Elements
// print("Length is : ${theSet.length}");
///Accessing an element by index
// print(theSet.elementAt(0));
// print(theSet.elementAt(1));
// print(theSet.elementAt(2));
// print(theSet.elementAt(3));
// print(theSet.elementAt(4));
///Also,you can use the first and last property to access the first and last elements respectively
// print(theSet.first);
// print(theSet.last);
///Adding an element to a set
// theSet.add(60);
// theSet.add(70);
// print(theSet);
///Adding Multiple Elements
// theSet.addAll([60,70,80,90,100]);
// print(theSet);
///Checking the existence of elements
// print(theSet.contains(20));
// print(theSet.contains(300));
///Finding the intersection of Two elements
// var checkSet1 = {11,12,13,14,15,16,17,18,19,20,21,22,23,24,25};
// var checkSet2 = {21,22,14,19,23,24,25,16,26,11,27,12,28,20,29};
// var resultSet = checkSet1.intersection(checkSet2);
// print(resultSet);
///Finding the Union of Two sets
// var oneSet = {11,12,13,14,15,16,17,18,19,20,21,22,23,24,25};
// var twoSet = {21,22,14,19,23,24,25,16,26,11,27,12,28,20,29};
// var resultSet1 = oneSet.union(twoSet); //FIRST WAY
// print(resultSet1);
// print(oneSet.union(twoSet)); //SECOND WAY TO PRINT UNION
///Iterating over elements of a set using for loop
// for(var name in theSet)
// {
// print(name);
// }
///Since the Set class has the length property that returns the number of elements of a set, you can
///loop over the elements using the for loop:
for(var i = 0; i < theSet.length; i++)
{
print(theSet.elementAt(i));
}
}
Comments
Post a Comment