A set is, As similar to a List. The major difference between a List and a Set is, A Set does not allow duplicate values it means every value in a set is unique. whereas in List, repetition of values is allowed. For declaring a set we use Set as a keyword.
We can declare the set in two different ways, Kindly look into the following syntax.
Syntax:
Set<Variable type> sampleSet= {}
or
var sampleSet = <Variable type> {}
Eg: here are the sample examples of both ways approaches.
Set<String> sampleStrSet = {'Aditya','Hari','Kishan'};
print(sampleStrSet);
// Output : {Aditya, Hari, Kishan}
Let's try with second way approach
var sampleStrSet = <String>{'Aditya','Hari','Kishan'};
print(sampleStrSet);
// Output : {Aditya, Hari, Kishan}
Let's add and check duplicates to a set
Set<int> numberSet = {10,20, 30, 40, 50, 60, 10, 20}
print(numberSet);
// Output : {10,20, 30, 40, 50, 60,}
As we discussed earlier a Set which doesn't allow duplicates, we can notice that in the above output.
now we dicuss on the other properties and method which are related to Set.
Set numberSet = Set();
numberSet.add(10);
numberSet.add(20);
numberSet.add(30);
numberSet.add(40);
numberSet.add(50);
print(numberSet);
print(numberSet.length);
numberSet.add(60);
print(numberSet);
print(numberSet.length);
// Output: {10, 20, 30, 40, 50}
5
{10, 20, 30, 40, 50, 60}
6
We use this method when we want to clear all the elements from a set, for this let us use our exisitng numberSet example
Eg:
print(numberSet);
//Output : {10, 20, 30, 40, 50, 60}
numberSet.clear();
print(numberSet);
//Output : {}
Whe we compare two different Sets, to findout or to retrieve the elements which are not common we use this method
Eg:
Set<String> setA = {'A', 'B', 'C'};
Set<String> setB = {'B', 'D','E'};
var diffSetA = setA.difference(setB);
print(diffSetA);
// Output : {A, C}
var diffSetB = setB.difference(setA);
print(diffSetB);
// Output : {D, E}
The purpose of this method is, When we compare a Set with the other set it creates a new set with common elemets.
Eg:
Set<String> setA = {'A', 'B', 'C'};
Set<String> setB = {'B', 'D','E'};
var interSetctionSet = setA.intersection(setB);
print(interSetctionSet);
// Output : {B}
This method is used for finding an object from the given set. When corresponding object is found from a given set it'll return the object or else it'll return a null value.
Eg:
var lookForChar = <String>{'A', 'B', 'C'};
var containsB = lookForChar.lookup('B');
print(containsB);
// Output: B
var containsD = lookForChar.lookup('D');
print(containsD);
// Output: null
The name itself a self explanatory, The purpose of this method is removing an object from a set. It returns a true value when an object is removed and returns false when an object not found in the given set. The method has no effect if value was not in the set.
Eg:
// Remove
var removeChar = <String>{'A', 'B', 'C'};
var removeB = removeChar.remove('B');
print(removeB);
//Output: true
// lets remove an object which is not available in the above set
var removeD = removeChar.remove('D');
print(removeD);
// Output: False
print(removeChar);
// Output: {A, C}
When we want to remove certain objects from a set we adopt this method, Here is the example
Eg:
var charSet = <String>{'A', 'B', 'C', 'D'};
charSet.removeAll({'A', 'B', 'D', 'X'});
print(charSet);
// Output: {C}
This method helps in removing all the elements that satisfies the where condition
Eg:
var charSet = <String>{'A', 'B', 'Ad', 'Bd', 'Boat', 'Ball', 'C'};
// now we're remove all the objects which starts with B.
charSet.removeWhere((element) => element.startsWith('B'));
print(charSet);
// Output: {A, Ad, C}
// Similar to above example lets try another approach which contains B, instead of startsWith B.
charSet.removeWhere((element) => element.contains('B'));
print(charSet);
// Output: {A, Ad, C}
For any further information please visit this page on Set
Here is the combined example of the things we have discussed on this page.
void main(){
Set<int> numberSet = {10,20, 30, 40, 50, 60, 10, 20};
print(numberSet);
Set<String> sampleStrSet = {'Aditya','Hari','Kishan'};
print(sampleStrSet);
var sampleStrSet = <String>{'Aditya','Hari','Kishan'};
print(sampleStrSet);
Set numberSet = Set();
numberSet.add(10);
numberSet.add(20);
numberSet.add(30);
numberSet.add(40);
numberSet.add(50);
print(numberSet);
print(numberSet.length);
numberSet.add(60);
print(numberSet);
print(numberSet.length);
// Output : {10, 20, 30, 40, 50}
//Output : 5
// Output : {10, 20, 30, 40, 50, 60}
//Output : 6
print(numberSet);
//Output : {10, 20, 30, 40, 50, 60}
numberSet.clear(); // {}
print(numberSet);
//Output : {}
// difference
Set<String> setA = {'A', 'B', 'C'};
Set<String> setB = {'B', 'D','E'};
final diffSetA = setA.difference(setB);
print(diffSetA);
// Output : {A, C}
var diffSetB = setB.difference(setA);
print(diffSetB);
// Output : {D, E}
Set<String> setA = {'A', 'B', 'C'};
Set<String> setB = {'B', 'D','E'};
var interSetctionSet = setA.intersection(setB);
print(interSetctionSet);
// Output : {B}
// Look Up
var lookForChar = <String>{'A', 'B', 'C'};
var containsB = lookForChar.lookup('B');
print(containsB);
// Output: B
var containsD = lookForChar.lookup('D');
print(containsD);
// Output: null
// Remove
var removeChar = <String>{'A', 'B', 'C'};
var removeB = removeChar.remove('B');
print(removeB);
//Output: true
// lets remove an object which is not available in the above set
var removeD = removeChar.remove('D');
print(removeD);
// Output: False
print(removeChar);
// Output: {A, C}
var charSet = <String>{'A', 'B', 'C', 'D'};
charSet.removeAll({'A', 'B', 'D', 'X'});
print(charSet);
// Output: {C}
final charSet = <String>{'A', 'B', 'Ad', 'Bd', 'Boat', 'Ball', 'Tab', 'C'};
// charSet.removeWhere((element) => element.startsWith('B'));
charSet.removeWhere((element) => element.contains('b'));
print(charSet);
}