Reputation: 171
I know some differences like list = [] ; set = {}
set have different method like intersection() and others I forget them but what's the main different between them ?
Upvotes: 7
Views: 4236
Reputation: 11
Lists vs Sets useful cases are given below:
Upvotes: 1
Reputation: 76273
A List is an ordered collection of elements where the same element may occure several times at different positions.
A Set is (usually) an unordered collection of unique elements. The uniqueness is evaluated by using ==
and hashCode
.
main() {
var x = 1;
var y = 1;
var z = 2;
var list = <int>[];
list.add(x);
list.add(y);
list.add(z);
list.add(x);
print(list); // 4 elements [1, 1, 2, 1]
var set = <int>{};
set.add(x);
set.add(y);
set.add(z);
set.add(x);
print(set); // only 2 elements {1, 2}
}
Upvotes: 15