Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

removeall
linkedhashset
dart
communitycreator

How to use the removeAll() method of LinkedHashSet in Dart

Programming Bytes

Overview

The removeAll() method removes all the elements of the LinkedHashSet object present in the provided collection. The method leaves the elements which are not present in the argument.

Syntax

void removeAll(Iterable<Object?> elements);

Parameter

This method takes an iterable/collection object to be removed from the LinkedHashSet as a parameter.

Return value

This method doesn’t return any value.

Code

The code below demonstrates the use of the removeAll method:

import 'dart:collection';
void main() {
  //create a new LinkedHashSet which can have int type elements
  LinkedHashSet set = new LinkedHashSet<int>();

  // add three elements to the set
  set.add(1);
  set.add(2);
  set.add(3);
  print('The set is $set');

  // create a queue
  ListQueue queue = new ListQueue();

  // add 3 elements to the queue
  queue.add(1);
  queue.add(2);
  queue.add(4);

  print('The queue is $queue');
  set.removeAll(queue); // remove all elements of queue from set
  print('\nAfter calling set.removeAll(queue) The set is: $set');
}
Using the removeAll method

Explanation

In the code above:

  • Line 1: We import the collection library.

  • Lines 4 to 9: We create a new LinkedHashSet object named set. We then use the add() method to add three elements (1,2,3) to the set object.

  • Lines 13 to 18: We create a new ListQueue object named queue. Then, we use the add() method to add three elements (1,2,4) to the queue object.

  • Line 20: We use the removeAll method to remove all the elements present in the queue from the set. In our case, the elements of the queue and the set are as follows:

Queue – 1, 2, 4
Set – 1, 2, 3

The removeAll method:

  • Checks if element 1 of the queue is present in the set. The set contains 1, so 1 is removed from the set.

  • Checks if element 2 of the queue is present in the set. The set contains 2, so 2 is removed from the set.

  • Checks if element 4 of the queue is present in the set. The set doesn’t contain 4.

The resulting set is [3].

RELATED TAGS

removeall
linkedhashset
dart
communitycreator
RELATED COURSES

View all Courses

Keep Exploring