8. Collections
82. Collection intro
Lists
데이터를 한줄로 쫙 나열해 놓으셨다고 보시면 됩니다.
Sets
Lists와 동일하게 데이터를 한줄로 쫙 나열하되 동일한 데이터가 여러개 존재할 수 없습니다.
Maps
key-value 짝을 이루면서 데이터를 모아놓은 것.
83. Lists
동일한 구조의 데이터들을 모아 한줄로 쫙 나열한 것.
다른 언어에서는
array라고도 한다.각 데이터에 순서데로 0부터 번호를 부여한다.
부여된 번호는
index라고 부른다.index를 통해 각 데이터를 가져올 수 있다.
Dart는 자동으로 리스트 안에 데이터가 String이라는 것을 알고 alphabets리스트를 아래 데이터 키워드처럼 변수를 고정시킨다. 한번 고정된 변수는 변경할 수 없다.
텅 빈 리스트, null 아님.
Dart는 List안에 데이터가 어떤것인지 모름.
dynamic으로 고정.
empty List를 생성시 아래처럼 생성하자.
get the value in the index
get an index of the value. it can be
null
assign new value into an index
add an element to a list
remove an element from the list
84. Unmodifiable List
새로운
List를 생성 후 변수에 assign할 수 있다.
final Lists성격을 파헤쳐보자.만약에
var대신final을 사용한다면, 에러가 발생한다.final alphabet = ['A', 'B', 'C', 'D']; alphabet = []; alphabet = ['A', 'B'];이는 alphabet이라는
List변수에 새로운List를 생성해서 assign할 수 없다는 뜻이다. 다음 코드를 보자.final alphabet = ['A', 'B', 'C', 'D']; desserts.remove('A');// OK desserts.remove('B');// OK desserts.add('A');// OKfinal우리가 죽기전까지 살 마지막 집. 집은 변함이 없지만 집 안에 있는 가구나 살림은 언제든 변경이 가능하다.
모든 elements도 변경이 불가능하게 하고 싶다면??
const alphabet = ['A', 'B', 'C', 'D']; alphabet = [];// Error alphabet = ['A', 'B'];// Error desserts.remove('A');// Error desserts.add('A');// Errorconst를 사용할 수 없는 상황에서는?final alphabet = const ['A', 'B', 'C', 'D']; alphabet = [];// Error alphabet = ['A', 'B'];// Error desserts.remove('A');// Error desserts.add('A');// Errorimmutable list를 원하지만 사용될때까지 값이 뭔지 모른다면??
List.unmodifiable를 사용하자.final modifiableList = ['A'.toLowerCase(), 'B'.toLowerCase(), 'C'.toLowerCase()]; final unmodifiableList = List.unmodifiable(modifiableList);
85. List Properties
Access first and last elements
print(alphabets.first); print(alphabets.last);the List contains any elements
print(alphabets.isEmpty); print(alphabets.isNotEmpty);the List contains any elements
print(alphabets.contains('a'));the List length
print(alphabets.length); print(alphabets.length == 0); print(alphabets.length > 0);
86. Loop list
output of
forEachis same as input ofprintalphabets.forEach(print);
87. Useful operators for lists
Spread operator
const kia = ['k3', 'k5', 'sorento']; const hyundai = ['sonata', 'avante', 'palisade']; const koreanCars = ['rexton', ...kia, ...hyundai]; print(koreanCars);nullspread operatorconst kia = ['k3', 'k5', 'sorento']; const hyundai = ['sonata', 'avante', 'palisade']; List<String>? deawoo; List<String> koreanCars = ['rexton', ...?deawoo, ...kia, ...hyundai]; print(koreanCars);collection if
const kids = true; const sandwich = [ 'lettuce', 'pickles', if (!kids)'jalapeno', 'ham', 'chicken', 'cheese', ]; print(sandwich);collection for
const spices = ['jalapeno', 'wasabi']; List<String> sandwich = [ 'LETTUCE', 'PICKLES', 'HAM', for (var spice in spices) spice.toUpperCase(), 'CHICKEN', 'CHEESE', ]; print(sandwich);
88. Challenge lists
Quiz 1. 1월부터 12월까지 영어로 된 리스트를 생성해보자.
List<String> months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'];Quiz 2. 생성된 리스트를 싹 비우자.
months.clear();Quiz 3. 리스트를 다시 1월부터 6월까지 영어로
add메소드를 사용해 채워보자.months.add('JAN'); months.add('FEB'); // ... months.add('JUN');Quiz 4. 7월부터 12월까지는
for loop을 사용해 채워보자.List<String> temp = ['JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']; for(var month in temp) { months.add(month); }Quiz 5. 전부 소문자/대문자로 변경해보자.
for(int i=0; i<months.length; i++) { months[i] = months[i].toLowerCase(); = months[i].toUpperCase(); }
89. Sets
Lists와 많은 부분이 동일, 다른 부분만 말하는게 쉽겠다.index가 없다.동일한 element를 반복할 수 없다.Creating a set
final Set<int> someSet = {}; final someSet = <int>{};prove no duplicates
final anotherSet = {1, 2, 3, 1}; print(anotherSet);
90. Operations on a set
Contains
Set<String> alphabets = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; print(alphabets.contains('A'));// true print(alphabets.contains('AB'));// falseAdding single elements
final alphabets = <String>{}; alphabets.add('A'); alphabets.add('B'); alphabets.add('C'); print(alphabets);Removing elements
final alphabets = <String>{}; print(alphabets.remove('A')); print(alphabets);Adding multiple elements
final alphabets = <String>{}; alphabets.addAll(['D', 'E', 'F']); print(alphabets);Intersections
Set<String> aSet = {'A','B','C','D','E','F'}; Set<String> bSet = {'A','B','W','X','Y','Z'}; final intersection = aSet.intersection(bSet); print(intersection);Unions
Set<String> aSet = {'A','B','C','D','E','F'}; Set<String> bSet = {'A','B','W','X','Y','Z'}; final union = aSet.union(bSet); print(union);other operation
collection
ifcollection
forfor-inloopsforEachloopsspread operators
91. Maps 1
key-value pairs
key를 통해 value를 찾을 수 있는 구조이다.순서가 없다.key는 유니크해야 한다.
create a Map
final Map<String, int> emptyMap = {}; final emptyMap = <String, int>{};Maps and Sets 다
{}를 사용한다. 아래는 Map일까? Set일까?final mapOrSet = {};mapOrSet은 `Map<dynamic, dynamic>'이다. 왜냐하면, Maps가 Sets보다 Dart언어에서 먼저 생성이 되어서라고 한다.
Initialize a Map with values
final foods = { 'cakes': 2, 'eggs': 6, 'drinks': 7, 'cookies': 12, }; final digitToWord = { 1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', }; print(foods); print(digitToWord);Key는 반드시 Unique해야한다.
//not works final foods = { 'eggs': 2, 'eggs': 6, 'drinks': 7, 'cookies': 12, }; //works final foods = { 'cakes': 2, 'eggs': 2, 'drinks': 2, 'cookies': 2, };
92. Operations on a map_1
Access an element
final foods = { 'cakes': 2, 'eggs': 6, 'drinks': 7, 'cookies': 12, }; print(foods['cakes']); print(foods['butter']);?.
final numOfButter = foods['butter']; print(numOfButter?.isEven);add new element
foods['butter'] = 3; print(foods);update a value
print(foods['eggs']); foods['eggs'] = 3; print(foods['eggs']);remove an element
foods.remove['eggs']; print(foods);Map properties
foods.isEmpty// false foods.isNotEmpty// true foods.length print(foods.keys); print(foods.values); print(foods.containsKey('eggs')); print(foods.containsValue(12));
92. Loop through map_2
looping through a map
for (var item in foods.keys) { print(foods[item]); } foods.forEach((key, value) => print('$key -> $value')); for (final entry in inventory.entries) { print('${entry.key} -> ${entry.value}'); }
93. Challenge map
개인 신상정보 Map으로 생성하기.
name, profession, country, city등등.
final profile = { 'name': 'victor', 'profession': 'programmer', 'country': 'Korea', 'city': 'seoul' };
갑자기 한국에서 미국 new york으로 이사를 갔다.
country와 city를 변경해보자.
profile['country'] = 'united state'; profile['city'] = 'new york';
loop을 사용해 map을 log에 나타내자.
profile.forEach((key, value) => print('$key : $value\n'));
94. Higher order methods_1
List.map()
collection을 map메소드를 이용해서 새로운 Iterable<>을 생성할 수 있다.
forEach와 비슷하지만 다르다.final alphabets = ['a', 'b', 'c', 'd', 'e']; final upAlphabets = alphabets.map((alphabet)=>alphabet.toUpperCase()); print(upAlphabets.runtimeType);결과는
MappedListIterable<String, String>이름이 길지만,Iterable이라는 것은 확실하다.Iterable은 Lists와 비슷한 것이라고 보면 된다. Lists가 기능은 더 많다.print(upAlphabets);이 결과물을 보면
(A, B, C, D, E)이렇게 표시된다. 이를 Lists로 변경해서 사용할 수 있다.toList()를 사용하면 된다.final list = upAlphabets.toList(); print(list);이 결과물을 보면
[A, B, C, D, E]이렇게 표시된다. Sets로도 변경이 가능하다.final sets = upAlphabets.toSet(); print(sets);{A, B, C, D, E}이렇게 표시된다.
forEach()VSmap()다른 점만 분명히 알면 된다.
map()은 각각의 값을 변경후Iterable안에 하나씩 넣어주고Iterable을 return한다. 반면에forEach()는 각각의 값을 가져와서 action을 취하고 끝!
final alphabets = ['a', 'b', 'c', 'd', 'e']; final upAlphabets = alphabets.map((alphabet)=>alphabet.toUpperCase()); alphabets.forEach((alphabet)=>print(alphabet.toUpperCase())); //map Iterable<T> map<T>(T toElement(E e)) => MappedIterable<E, T>(this, toElement); //forEach void forEach(void action(E element)) { for (E element in this) action(element); }
94. Higher order methods_2
Filter by
where()final numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]; final evens = numbers.where((number) => number.isEven); print(evens);Consolidating a collection by
reduce()final numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]; final total = numbers.reduce((sum, number) => sum + number); print(total); final alphabets = ['a', 'b', 'c', 'd', 'e']; final allAlphabets = alphabets.reduce((all, alphabet) => all+alphabet); print(allAlphabets);fold()instead ofreduce()reduce()를 사용할때, lists가 empty이면 에러를 발생한다. 이를 방지하기 위해fold()를 사용할 수 있다.
final numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]; final total = numbers.fold(0,(int sum, number) => sum + number); print(total); final alphabets = ['a', 'b', 'c', 'd', 'e']; final allAlphabets = alphabets.fold('', (String all, alphabet) => all+alphabet); print(allAlphabets);
95. Higher order methods
sort()
reversed
custom sorting
Using multiple methods
96. When to use them
List
순서가 중요할때
되도록 끝에 새로운 아이템을 추가할 것
리스트가 크면 검색이 어려움
Set
아이템의 그룹에 포함 여부만 확인할 때
Map
자주 아이템을 key를 통해 검색하고 사용할 때