Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of dart.core; | 5 part of dart.core; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * An unordered collection of key-value pairs, | 8 * An unordered collection of key-value pairs, |
| 9 * from which you retrieve a value by using its associated key. | 9 * from which you retrieve a value by using its associated key. |
| 10 * | 10 * |
|
mem
2013/09/06 16:42:22
Please show a code snippet here at the top...minim
| |
| 11 * Each key can occur at most once in a map. | 11 * Each key can occur at most once in a map. |
| 12 */ | 12 */ |
| 13 abstract class Map<K, V> { | 13 abstract class Map<K, V> { |
| 14 /** | 14 /** |
| 15 * Creates a Map instance with the default implementation. | 15 * Creates a Map instance with the default implementation. |
| 16 */ | 16 */ |
| 17 factory Map() = LinkedHashMap<K, V>; | 17 factory Map() = LinkedHashMap<K, V>; |
| 18 | 18 |
| 19 /** | 19 /** |
| 20 * Creates a Map instance that contains all key-value pairs of [other]. | 20 * Creates a Map instance that contains all key-value pairs of [other]. |
| 21 */ | 21 */ |
| 22 factory Map.from(Map<K, V> other) = LinkedHashMap<K, V>.from; | 22 factory Map.from(Map<K, V> other) = LinkedHashMap<K, V>.from; |
| 23 | 23 |
| 24 /** | 24 /** |
| 25 * Creates a Map instance | 25 * Creates a Map instance |
| 26 * where the keys and values are computed from the [iterable]. | 26 * where the keys and values are computed from the [iterable]. |
|
mem
2013/09/06 16:42:22
where -> in which
shailentuli
2013/09/23 11:49:46
Done.
| |
| 27 * | 27 * |
| 28 * For each element of the [iterable] this constructor computes a key-value | 28 * For each element of the [iterable] this constructor computes a key-value |
| 29 * pair, by applying [key] and [value] respectively. | 29 * pair, by applying [key] and [value] respectively. |
| 30 * | 30 * |
| 31 * List<int> list = [1, 2, 3, 4]; | |
| 32 * // The map keys are the list elements converted to Strings. | |
| 33 * // The map values are the squares of the list elements. | |
| 34 * Map<String, int> newMap = new Map.fromIterable(list, | |
| 35 * key: (item) => item.toString(), | |
| 36 * value: (item) => item * item)); | |
| 37 * | |
| 38 * list.map((key) => newMap[key.toString()]).join(', '); // '1, 4, 9, 16' | |
|
mem
2013/09/06 16:42:22
the last line of this code snippet makes this samp
shailentuli
2013/09/23 11:49:46
Agreed. Simplified.
| |
| 39 * | |
| 40 * | |
| 31 * The keys computed by the source [iterable] | 41 * The keys computed by the source [iterable] |
| 32 * do not need to be unique. The last | 42 * do not need to be unique. The last |
| 33 * occurrence of a key will simply overwrite any previous value. | 43 * occurrence of a key will simply overwrite any previous value. |
| 34 * | 44 * |
| 35 * If no values are specified for [key] and [value] the default is the | 45 * If no values are specified for [key] and [value] the default is the |
| 36 * identity function. | 46 * identity function. |
| 47 * | |
| 48 * newMap = new Map.fromIterable(list); | |
| 49 * list.map((key) => newMap[key]).join(', '); // '1, 2, 3, 4' | |
|
mem
2013/09/06 16:42:22
same as above.
shailentuli
2013/09/23 11:49:46
Also simplified.
| |
| 37 */ | 50 */ |
| 38 factory Map.fromIterable(Iterable iterable, | 51 factory Map.fromIterable(Iterable iterable, |
| 39 {K key(element), V value(element)}) = LinkedHashMap<K, V>.fromIterable; | 52 {K key(element), V value(element)}) = LinkedHashMap<K, V>.fromIterable; |
| 40 | 53 |
| 41 /** | 54 /** |
| 42 * Creates a Map instance associating the given [keys] to [values]. | 55 * Creates a Map instance associating the given [keys] to [values]. |
| 43 * | 56 * |
| 44 * This constructor iterates over [keys] and [values] and maps each element of | 57 * This constructor iterates over [keys] and [values] and maps each element of |
| 45 * [keys] to the corresponding element of [values]. | 58 * [keys] to the corresponding element of [values]. |
| 46 * | 59 * |
| 60 * List<String> letters = ['a', 'b', 'c']; | |
| 61 * List<String> things = ['axe', 'ball', 'cat']; | |
| 62 * Map<String, String> newMap = new Map.fromIterables(letters, things); | |
| 63 * letters.map((letter) => newMap[letter]).join(', '); // 'axe, ball, cat' | |
|
mem
2013/09/06 16:42:22
same as above.
shailentuli
2013/09/23 11:49:46
Also simplified.
| |
| 64 * | |
| 47 * If [keys] contains the same object multiple times, the last occurrence | 65 * If [keys] contains the same object multiple times, the last occurrence |
| 48 * overwrites the previous value. | 66 * overwrites the previous value. |
| 49 * | 67 * |
| 50 * It is an error if the two [Iterable]s don't have the same length. | 68 * It is an error if the two [Iterable]s don't have the same length. |
| 51 */ | 69 */ |
| 52 factory Map.fromIterables(Iterable<K> keys, Iterable<V> values) | 70 factory Map.fromIterables(Iterable<K> keys, Iterable<V> values) |
| 53 = LinkedHashMap<K, V>.fromIterables; | 71 = LinkedHashMap<K, V>.fromIterables; |
| 54 | 72 |
| 55 /** | 73 /** |
| 56 * Returns true if this map contains the given value. | 74 * Returns true if this map contains the given value. |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 69 * value, or use the [putIfAbsent] method. | 87 * value, or use the [putIfAbsent] method. |
| 70 */ | 88 */ |
| 71 V operator [](Object key); | 89 V operator [](Object key); |
| 72 | 90 |
| 73 /** | 91 /** |
| 74 * Associates the [key] with the given [value]. | 92 * Associates the [key] with the given [value]. |
| 75 */ | 93 */ |
| 76 void operator []=(K key, V value); | 94 void operator []=(K key, V value); |
| 77 | 95 |
| 78 /** | 96 /** |
| 79 * If [key] is not associated to a value, calls [ifAbsent] and | 97 * If [key] is not associated to a value, calls [ifAbsent] and |
|
mem
2013/09/06 16:42:22
to -> with
| |
| 80 * updates the map by mapping [key] to the value returned by | 98 * updates the map by mapping [key] to the value returned by |
| 81 * [ifAbsent]. Returns the value in the map. | 99 * [ifAbsent]. Returns the value in the map. |
| 82 * | 100 * |
| 101 * Map<String, int> scores = {'Bob': 36}; | |
| 102 * for (var key in ['Bob', 'Rohan', 'Jose']) { | |
| 103 * scores.putIfAbsent(key, () => 0); | |
| 104 * } | |
| 105 * scores['Bob']; // 36 | |
| 106 * scores['Rohan']; // 0 | |
| 107 * scores['Jose']; // 0 | |
|
mem
2013/09/06 16:42:22
where's sophena?
Also, use a value that is not 0
shailentuli
2013/09/23 11:49:46
She's there now. Done.
| |
| 108 * | |
| 83 * It is an error to add or remove keys from the map during the call to | 109 * It is an error to add or remove keys from the map during the call to |
| 84 * [ifAbsent]. | 110 * [ifAbsent]. |
|
mem
2013/09/06 16:42:22
An error occurs if you try to add or remove keys..
shailentuli
2013/09/23 11:49:46
Done.
shailentuli
2013/09/23 11:49:46
Done.
| |
| 85 */ | 111 */ |
| 86 V putIfAbsent(K key, V ifAbsent()); | 112 V putIfAbsent(K key, V ifAbsent()); |
| 87 | 113 |
| 88 /** | 114 /** |
| 89 * Adds all key-value pairs of [other] to this map. | 115 * Adds all key-value pairs of [other] to this map. |
| 90 * | 116 * |
| 91 * If a key of [other] is already in this map, its value is overwritten. | 117 * If a key of [other] is already in this map, its value is overwritten. |
| 92 * | 118 * |
| 93 * The operation is equivalent to doing `this[key] = value` for each key | 119 * The operation is equivalent to doing `this[key] = value` for each key |
| 94 * and associated value in other. It iterates over [other], which must | 120 * and associated value in other. It iterates over [other], which must |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 /** | 160 /** |
| 135 * Returns true if there is no {key, value} pair in the map. | 161 * Returns true if there is no {key, value} pair in the map. |
| 136 */ | 162 */ |
| 137 bool get isEmpty; | 163 bool get isEmpty; |
| 138 | 164 |
| 139 /** | 165 /** |
| 140 * Returns true if there is at least one {key, value} pair in the map. | 166 * Returns true if there is at least one {key, value} pair in the map. |
| 141 */ | 167 */ |
| 142 bool get isNotEmpty; | 168 bool get isNotEmpty; |
| 143 } | 169 } |
| OLD | NEW |