-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05Collecciones.dart
More file actions
193 lines (151 loc) · 5.45 KB
/
05Collecciones.dart
File metadata and controls
193 lines (151 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
void main1() {
// Listas
List<int> numbers = [1, 2, 3, 4, 5];
numbers.add(6);
numbers.remove(3);
print('Lista: $numbers'); // Output: Lista: [1, 2, 4, 5, 6]
// Conjuntos
Set<String> fruits = {'apple', 'banana', 'orange'};
fruits.add('apple'); // No se añadirá porque ya existe
fruits.remove('banana');
print('Conjunto: $fruits'); // Output: Conjunto: {apple, orange}
// Mapas
Map<String, int> fruitPrices = {'apple': 3, 'banana': 2, 'orange': 4};
fruitPrices['grape'] = 5;
fruitPrices.remove('banana');
print('Mapa: $fruitPrices'); // Output: Mapa: {apple: 3, orange: 4, grape: 5}
// Iteración con forEach
numbers.forEach((number) {
print('Número: $number');
});
// Transformación con map
var squares = numbers.map((number) => number * number);
print(
'Cuadrados: ${squares.toList()}'); // Output: Cuadrados: [1, 4, 16, 25, 36]
// Filtrado con where
var evenNumbers = numbers.where((number) => number % 2 == 0);
print(
'Números pares: ${evenNumbers.toList()}'); // Output: Números pares: [2, 4, 6]
// Reducción con reduce
var sum = numbers.reduce((a, b) => a + b);
print('Suma: $sum'); // Output: Suma: 18
}
void main2() {
List<int> numbers = [1, 2, 3, 4, 5];
// Añadir varios elementos
numbers.addAll([6, 7, 8]);
print(numbers); // Output: [1, 2, 3, 4, 5, 6, 7, 8]
// Insertar un elemento en una posición específica
numbers.insert(2, 10);
print(numbers); // Output: [1, 2, 10, 3, 4, 5, 6, 7, 8]
// Eliminar el último elemento
numbers.removeLast();
print(numbers); // Output: [1, 2, 10, 3, 4, 5, 6, 7]
// Crear una sublista
List<int> sublist = numbers.sublist(2, 5);
print(sublist); // Output: [10, 3, 4]
}
void main3() {
Set<String> fruits = {'apple', 'banana', 'orange'};
// Añadir varios elementos
fruits.addAll({'grape', 'pineapple'});
print(fruits); // Output: {apple, banana, orange, grape, pineapple}
// Intersección de conjuntos
Set<String> tropicalFruits = {'banana', 'pineapple', 'mango'};
Set<String> commonFruits = fruits.intersection(tropicalFruits);
print(commonFruits); // Output: {banana, pineapple}
// Unión de conjuntos
Set<String> allFruits = fruits.union(tropicalFruits);
print(allFruits); // Output: {apple, banana, orange, grape, pineapple, mango}
// Diferencia de conjuntos
Set<String> uniqueFruits = fruits.difference(tropicalFruits);
print(uniqueFruits); // Output: {apple, orange, grape}
}
void main4() {
// Crear un mapa inicial
Map<String, int> fruitPrices = {'apple': 3, 'banana': 2};
// putIfAbsent
fruitPrices.putIfAbsent('orange', () => 4);
fruitPrices.putIfAbsent(
'apple', () => 5); // No se asignará porque 'apple' ya existe
// update
fruitPrices.update(
'apple', (value) => value + 1); // Actualiza el valor de 'apple'
fruitPrices.update('orange', (value) => value + 1,
ifAbsent: () => 5); // Agrega 'orange' si no existe
// updateAll
fruitPrices.updateAll((key, value) => value * 2); // Duplica todos los valores
// remove
fruitPrices.remove('banana'); // Elimina 'banana'
// containsKey
print(fruitPrices.containsKey('apple')); // Output: true
print(fruitPrices.containsKey('banana')); // Output: false
// containsValue
print(fruitPrices.containsValue(6)); // Output: true
print(fruitPrices.containsValue(5)); // Output: false
// length
print(fruitPrices.length); // Output: 2
// isEmpty y isNotEmpty
print(fruitPrices.isEmpty); // Output: false
print(fruitPrices.isNotEmpty); // Output: true
// forEach
fruitPrices.forEach((key, value) {
print('$key: $value');
});
// Output:
// apple: 8
// orange: 10
// keys y values
Iterable<String> keys = fruitPrices.keys;
Iterable<int> values = fruitPrices.values;
print(keys); // Output: (apple, orange)
print(values); // Output: (8, 10)
// addAll
Map<String, int> moreFruitPrices = {'grape': 5, 'pineapple': 7};
fruitPrices.addAll(moreFruitPrices);
print(fruitPrices); // Output: {apple: 8, orange: 10, grape: 5, pineapple: 7}
// clear
fruitPrices.clear(); // Elimina todos los pares clave-valor
print(fruitPrices); // Output: {}
}
void main5() {
// Conversión entre tipos numéricos
int a = 10;
double b = a.toDouble();
print(b); // Output: 10.0
double c = 10.5;
int d = c.toInt();
print(d); // Output: 10
// Conversión de String a int y double
String str1 = '123';
String str2 = '123.45';
int e = int.parse(str1);
double f = double.parse(str2);
print(e); // Output: 123
print(f); // Output: 123.45
// Conversión de int y double a String
String str3 = e.toString();
String str4 = f.toString();
print(str3); // Output: '123'
print(str4); // Output: '123.45'
// Casting de dynamic a String
dynamic value = 'Hello, Dart!';
String str5 = value as String;
print(str5); // Output: 'Hello, Dart!'
// Verificación de tipos
if (value is String) {
print(
'value es una cadena de texto'); // Output: value es una cadena de texto
}
if (value is! int) {
print('value no es un entero'); // Output: value no es un entero
}
// Conversión de List<dynamic> a List<int>
List<dynamic> dynamicList = [1, 2, 3, 4, 5];
List<int> intList = dynamicList.cast<int>();
print(intList); // Output: [1, 2, 3, 4, 5]
// Conversión de Map<dynamic, dynamic> a Map<String, int>
Map<dynamic, dynamic> dynamicMap = {'a': 1, 'b': 2, 'c': 3};
Map<String, int> stringIntMap = dynamicMap.cast<String, int>();
print(stringIntMap); // Output: {a: 1, b: 2, c: 3}
}