We can use the remove()
method to remove an element from the SplayTreeSet
.
bool remove(Object value);
The element to be removed from the SplayTreeSet
is passed as an argument.
This method returns true
if the element is present and removed from the SplayTreeSet
. Otherwise, false
will be returned.
The code below demonstrates how to use the remove()
method to remove an element from the SplayTreeSet
:
import 'dart:collection'; void main() { //create a new SplayTreeSet which can have int type elements SplayTreeSet set = new SplayTreeSet<int>((a,b) => b.compareTo(a)); // add five elements to the set set.add(5); set.add(4); set.add(3); set.add(2); set.add(1); print('The set is $set'); // Remove element 5 bool isRemoved = set.remove(5); print('\nIs element 5 removed from set : $isRemoved'); print('The set is $set'); // Remove element 10 isRemoved = set.remove(10); print('\nIs element 10 removed from set : $isRemoved'); print('The set is $set'); }
Line 1: We import the collection
library.
Line 4: We create a new SplayTreeSet
object with the name set
. We pass a compare
function as an argument. This function is used for maintaining the order of the set
elements. In our case, the compare
function orders the elements in descending order.
Lines 7–11: We add five new elements to the set
. Now, the set is {5,4,3,2,1}
.
Line 16: We use the remove()
method to remove the element 5
. The element 5
is present in the set
so it is removed and true
is returned.
Line 21: We use the remove()
method to remove the element 10
. The element 10
is not present in the set
so false
is returned and the set
remains unchanged.
RELATED TAGS
CONTRIBUTOR
View all Courses