Questions for Practice on Dart Collections
- Create a List of integers from 1 to 10 and print all the elements.
- Write a program to add three different fruits to a List and print the List.
- Demonstrate how to access the third element in a List of strings.
- Create a List of numbers and remove the number 5. Print the updated List.
- Iterate over a List of integers and print only those that are even using a
forEach
method. - Create a Set of strings containing names, add a duplicate name, and print the Set.
- Write a program to check if a Set contains a specific name.
- Create a Map to store student names and their grades, then print each student's name and grade.
- Filter a List of integers to only include numbers greater than 5 and print the result.
- Create a Map of countries and their capitals, then retrieve and print the capital of a specified country.
- Write a program to find the intersection of two Sets and print the result.
- Use the
where
method to filter a List of strings to only include names starting with 'A'. - Create a List of mixed data types (int, String) and print only the integers.
- Write a program that combines two Lists into a Set to remove duplicates.
- Demonstrate how to clear all elements from a Map and check if it is empty.
Solutions with Code Explanation
1. Create a List of integers from 1 to 10 and print all the elements.
void main() {
List<int> numbers = List.generate(10, (index) => index + 1);
print(numbers); // Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] }
This code uses List.generate
to create a list of integers from 1 to 10.
2. Write a program to add three different fruits to a List and print the List.
void main() {
List<String> fruits = [];
fruits.add('Apple');
fruits.add('Banana');
fruits.add('Cherry');
print(fruits); // Output: [Apple, Banana, Cherry] }
Here, we initialize an empty list and use the add
method to insert fruits.
3. Demonstrate how to access the third element in a List of strings.
void main() {
List<String> fruits = ['Apple', 'Banana', 'Cherry', 'Date'];
print(fruits[2]); // Output: Cherry }
Lists are zero-indexed, so the third element is at index 2.
4. Create a List of numbers and remove the number 5. Print the updated List.
void main() {
List<int> numbers = [1, 2, 3, 4, 5, 6];
numbers.remove(5);
print(numbers); // Output: [1, 2, 3, 4, 6]
}
The remove
method deletes the specified element from the List.
5. Iterate over a List of integers and print only those that are even using a forEach
method.
void main() {
List<int> numbers = [1, 2, 3, 4, 5, 6];
numbers.forEach((number) {
if (number.isEven) {
print(number); // Output: 2, 4, 6
}
});
}
The forEach
method applies a function to each element in the List.
6. Create a Set of strings containing names, add a duplicate name, and print the Set.
void main() {
Set<String> names = {'Alice', 'Bob', 'Charlie'};
names.add('Alice'); // Adding a duplicate
print(names); // Output: {Alice, Bob, Charlie}
}
Sets automatically ignore duplicate entries.
7. Write a program to check if a Set contains a specific name.
void main() {
Set<String> names = {'Alice', 'Bob', 'Charlie'};
print(names.contains('Bob')); // Output: true }
The contains
method checks for the presence of an element in the Set.
8. Create a Map to store student names and their grades, then print each student's name and grade.
void main() {
Map<String, int> grades = {
'Alice': 90,
'Bob': 85,
'Charlie': 88,
};
grades.forEach((name, grade) {
print('$name: $grade'); // Output: Alice: 90, Bob: 85, Charlie: 88 });
}
The forEach
method is used to iterate over key-value pairs in the Map.
9. Filter a List of integers to only include numbers greater than 5 and print the result.
void main() {
List<int> numbers = [1, 2, 3, 6, 8, 5];
var filtered = numbers.where((number) => number > 5);
print(filtered.toList()); // Output: [6, 8] }
The where
method filters the list based on the specified condition.
10. Create a Map of countries and their capitals, then retrieve and print the capital of a specified country.
void main() {
Map<String, String> capitals = {
'USA': 'Washington, D.C.',
'France': 'Paris',
'Japan': 'Tokyo',
};
print(capitals['France']); // Output: Paris }
You access the value by using the key in the Map.
11. Write a program to find the intersection of two Sets and print the result.
void main() {
Set<int> setA = {1, 2, 3, 4};
Set<int> setB = {3, 4, 5, 6};
var intersection = setA.intersection(setB);
print(intersection); // Output: {3, 4} }
The intersection
method returns a new Set containing common elements.
12. Use the where
method to filter a List of strings to only include names starting with 'A'.
void main() {
List<String> names = ['Alice', 'Bob', 'Annie', 'Charlie'];
var filteredNames = names.where((name) => name.startsWith('A'));
print(filteredNames.toList()); // Output: [Alice, Annie] }
The where
method filters names based on the starting character.
13. Create a List of mixed data types (int, String) and print only the integers.
void main() {
List<dynamic> mixedList = [1, 'two', 3, 'four', 5];
var integers = mixedList.where((item) => item is int);
print(integers.toList()); // Output: [1, 3, 5] }
The is
operator checks the type of each element.
14. Write a program that combines two Lists into a Set to remove duplicates.
void main() {
List<int> list1 = [1, 2, 3, 4];
List<int> list2 = [3, 4, 5, 6];
Set<int> combinedSet = {...list1, ...list2};
print(combinedSet); // Output: {1, 2, 3, 4, 5, 6}
}
Using the spread operator combines two Lists into a Set, removing duplicates.
15. Demonstrate how to clear all elements from a Map and check if it is empty.
void main() {
Map<String, int> grades = {
'Alice': 90,
'Bob': 85,
};
grades.clear();
print(grades.isEmpty); // Output: true
}
The clear
method removes all entries from the Map, and isEmpty
checks if it’s empty.
These questions and solutions provide a comprehensive overview of working with collections in Dart, covering lists, sets, maps, and various operations you can perform on them.