Dart Set containsAll()
Syntax & Examples


Set.containsAll() method

The `containsAll` method in Dart returns whether this Set contains all the elements of another iterable.


Syntax of Set.containsAll()

The syntax of Set.containsAll() method is:

 bool containsAll(Iterable<Object> other) 

This containsAll() method of Set returns whether this Set contains all the elements of other.

Parameters

ParameterOptional/RequiredDescription
otherrequiredAn iterable of objects to check for containment in this Set.

Return Type

Set.containsAll() returns value of type bool.



✐ Examples

1 Check for numbers

In this example,

  1. We create a Set set containing numbers from 1 to 5.
  2. We create a List numbers containing elements 2 and 4.
  3. We use the containsAll() method to check if the set contains all numbers from the list.
  4. We print the result to standard output.

Dart Program

void main() {
  Set<int> set = {1, 2, 3, 4, 5};
  List<int> numbers = [2, 4];
  bool result = set.containsAll(numbers);
  print('Set contains all numbers: $result');
}

Output

Set contains all numbers: true

2 Check for characters

In this example,

  1. We create a Set set containing characters 'a' to 'e'.
  2. We create a List chars containing characters 'a', 'b', and 'f'.
  3. We use the containsAll() method to check if the set contains all characters from the list.
  4. We print the result to standard output.

Dart Program

void main() {
  Set<String> set = {'a', 'b', 'c', 'd', 'e'};
  List<String> chars = ['a', 'b', 'f'];
  bool result = set.containsAll(chars);
  print('Set contains all characters: $result');
}

Output

Set contains all characters: false

3 Check for strings

In this example,

  1. We create a Set set containing strings 'apple', 'banana', 'orange', and 'grape'.
  2. We create a List fruits containing strings 'banana', 'orange', and 'kiwi'.
  3. We use the containsAll() method to check if the set contains all strings from the list.
  4. We print the result to standard output.

Dart Program

void main() {
  Set<String> set = {'apple', 'banana', 'orange', 'grape'};
  List<String> fruits = ['banana', 'orange', 'kiwi'];
  bool result = set.containsAll(fruits);
  print('Set contains all fruits: $result');
}

Output

Set contains all fruits: false

Summary

In this Dart tutorial, we learned about containsAll() method of Set: the syntax and few working examples with output and detailed explanation for each example.