How to remove items from a LinkedHashMap in Dart:
In this post, we will learn how to remove a single item or multiple items from a LinkedHashMap in Dart. We can remove a single element or multiple elements by using a given function. Let’s learn how to use these methods with examples.
Method 1: Remove a single element from a LinkedHashMap by using the remove method:
The remove
method takes one key as the parameter and it removes the key-value pair with that given key from the LinkedHashMap. The syntax of this method is:
remove(Object? k) → V?
It will look for the key k in the LinkedHashMap and if this is found it removes the key-value pair. It returns the value of the key k before it is removed. If the key is not found, it returns null
.
For example,
import 'dart:collection';
void main() {
final linkedHMap = LinkedHashMap.from({1: 'Mango', 4: 'Banana', 3: 'Orange'});
print('Given map: ${linkedHMap}');
linkedHMap.remove(4);
print('Final LinkedHashMap: ${linkedHMap}');
}
It will print:
Given map: {1: Mango, 4: Banana, 3: Orange}
Final LinkedHashMap: {1: Mango, 3: Orange}
The key-value pair with key 4 is removed from the LinkedHashMap.
Method 2: Remove multiple elements with a given function:
There is another method called removeWhere
to remove all elements of a LinkedHashMap as satisfied by a given function. This function is useful to remove multiple items from a LinkedHashMap.
The syntax of this method is:
removeWhere(bool f(K key, V value)) -> void
It takes one function f
as the parameter and it removes all entries that are satisfied by this function. The following example removes all entries with even keys:
import 'dart:collection';
void main() {
final linkedHMap = LinkedHashMap.from({
1: 'Sun',
2: 'Mon',
3: 'Tues',
4: 'Wed',
5: 'Thurs',
6: 'Fri',
7: 'Sat'
});
print('Given LinkedHashMap: ${linkedHMap}');
linkedHMap.removeWhere((key, value) => key % 2 == 0);
print('Final LinkedHashMap: ${linkedHMap}');
}
In this example, entries with even keys are removed from the linkedHMap
LinkedHashMap. If you run this program, it will print:
Given LinkedHashMap: {1: Sun, 2: Mon, 3: Tues, 4: Wed, 5: Thurs, 6: Fri, 7: Sat}
Final LinkedHashMap: {1: Sun, 3: Tues, 5: Thurs, 7: Sat}
Method 3: Remove all entries of a LinkedHashMap:
The clear()
method removes all entries of a given LinkedHashMap. The syntax of the clear
method is:
clear() -> void
Let’s try it with an example:
import 'dart:collection';
void main() {
final linkedHMap = LinkedHashMap.from({
1: 'Sun',
2: 'Mon',
3: 'Tues',
4: 'Wed',
5: 'Thurs',
6: 'Fri',
7: 'Sat'
});
print('Given LinkedHashMap: ${linkedHMap}');
linkedHMap.clear();
print('Final LinkedHashMap: ${linkedHMap}');
}
If you run this program, it will print:
Given LinkedHashMap: {1: Sun, 2: Mon, 3: Tues, 4: Wed, 5: Thurs, 6: Fri, 7: Sat}
Final LinkedHashMap: {}
You might also like:
- Different ways to create a HashSet in Dart
- 3 ways to find if a HashSet is empty or not in Dart
- Dart HashSet.take and HashSet.takeWhile methods
- Dart HashSet where and whereType explanation with examples
- Dart HashSet fold and reduce methods explanation with examples
- Dart HashSet skip and skipWhile methods explanation with example
- Introduction to Dart LinkedHashMap class
- 7 ways to create a LinkedHashMap in Dart
- 3 ways to add items to a LinkedHashMap in Dart