Minary
Minary

Reputation: 171

what the different between lists and sets in dart ? . the question asked before but about java

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

Answers (2)

Moeen Ahmad
Moeen Ahmad

Reputation: 11

Lists vs Sets useful cases are given below:

  • Use lists for ordered data and sets for unordered data
  • Sets are more intelligent than lists because they will remove duplicate data.
  • Sets are mostly used for sets(Mathematics) Operations.

Upvotes: 1

Alexandre Ardhuin
Alexandre Ardhuin

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

Related Questions