Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(219)

Side by Side Diff: sdk/lib/core/map.dart

Issue 23462019: Added examples to map.dart (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Merge. Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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, from which you retrieve a value
9 * from which you retrieve a value by using its associated key. 9 * by using its associated key.
10 * 10 *
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 an identity map with the default implementation. 25 * Creates an identity map with the default implementation.
26 */ 26 */
27 factory Map.identity() = LinkedHashMap<K, V>.identity; 27 factory Map.identity() = LinkedHashMap<K, V>.identity;
28 28
29 /** 29 /**
30 * Creates a Map instance 30 * Creates a Map instance in which the keys and values are computed from the
31 * where the keys and values are computed from the [iterable]. 31 * [iterable].
32 * 32 *
33 * For each element of the [iterable] this constructor computes a key-value 33 * For each element of the [iterable] this constructor computes a key-value
34 * pair, by applying [key] and [value] respectively. 34 * pair, by applying [key] and [value] respectively.
35 * 35 *
36 * The keys computed by the source [iterable] 36 * The example below creates a new Map from a List. The keys of `map` are
37 * do not need to be unique. The last 37 * `list` values converted to strings, and the values of the `map` are the
38 * occurrence of a key will simply overwrite any previous value. 38 * squares of the `list` values:
39 *
40 * List<int> list = [1, 2, 3];
41 * Map<String, int> map = new Map.fromIterable(list,
42 * key: (item) => item.toString(),
43 * value: (item) => item * item));
44 *
45 * map['1'] + map['2']; // 1 + 4
46 * map['3'] - map['2']; // 9 - 4
39 * 47 *
40 * If no values are specified for [key] and [value] the default is the 48 * If no values are specified for [key] and [value] the default is the
41 * identity function. 49 * identity function.
50 *
51 * In the following example, the keys and corresponding values of `map`
52 * are `list` values:
53 *
54 * map = new Map.fromIterable(list);
55 * map[1] + map[2]; // 1 + 2
56 * map[3] - map[2]; // 3 - 2
57 *
58 * The keys computed by the source [iterable] do not need to be unique. The
59 * last occurrence of a key will simply overwrite any previous value.
42 */ 60 */
43 factory Map.fromIterable(Iterable iterable, 61 factory Map.fromIterable(Iterable iterable,
44 {K key(element), V value(element)}) = LinkedHashMap<K, V>.fromIterable; 62 {K key(element), V value(element)}) = LinkedHashMap<K, V>.fromIterable;
45 63
46 /** 64 /**
47 * Creates a Map instance associating the given [keys] to [values]. 65 * Creates a Map instance associating the given [keys] to [values].
48 * 66 *
49 * This constructor iterates over [keys] and [values] and maps each element of 67 * This constructor iterates over [keys] and [values] and maps each element of
50 * [keys] to the corresponding element of [values]. 68 * [keys] to the corresponding element of [values].
51 * 69 *
70 * List<String> letters = ['b', 'c'];
71 * List<String> words = ['bad', 'cat'];
72 * Map<String, String> map = new Map.fromIterables(letters, words);
73 * map['b'] + map['c']; // badcat
74 *
52 * If [keys] contains the same object multiple times, the last occurrence 75 * If [keys] contains the same object multiple times, the last occurrence
53 * overwrites the previous value. 76 * overwrites the previous value.
54 * 77 *
55 * It is an error if the two [Iterable]s don't have the same length. 78 * It is an error if the two [Iterable]s don't have the same length.
56 */ 79 */
57 factory Map.fromIterables(Iterable<K> keys, Iterable<V> values) 80 factory Map.fromIterables(Iterable<K> keys, Iterable<V> values)
58 = LinkedHashMap<K, V>.fromIterables; 81 = LinkedHashMap<K, V>.fromIterables;
59 82
60 /** 83 /**
61 * Returns true if this map contains the given value. 84 * Returns true if this map contains the given value.
(...skipping 16 matching lines...) Expand all
78 /** 101 /**
79 * Associates the [key] with the given [value]. 102 * Associates the [key] with the given [value].
80 */ 103 */
81 void operator []=(K key, V value); 104 void operator []=(K key, V value);
82 105
83 /** 106 /**
84 * If [key] is not associated to a value, calls [ifAbsent] and 107 * If [key] is not associated to a value, calls [ifAbsent] and
85 * updates the map by mapping [key] to the value returned by 108 * updates the map by mapping [key] to the value returned by
86 * [ifAbsent]. Returns the value in the map. 109 * [ifAbsent]. Returns the value in the map.
87 * 110 *
88 * It is an error to add or remove keys from the map during the call to 111 * Map<String, int> scores = {'Bob': 36};
89 * [ifAbsent]. 112 * for (var key in ['Bob', 'Rohan', 'Sophena']) {
113 * scores.putIfAbsent(key, () => key.length);
114 * }
115 * scores['Bob']; // 36
116 * scores['Rohan']; // 5
117 * scores['Sophena']; // 7
118 *
119 * The code that [ifAbsent] executes must not add or remove keys.
90 */ 120 */
91 V putIfAbsent(K key, V ifAbsent()); 121 V putIfAbsent(K key, V ifAbsent());
92 122
93 /** 123 /**
94 * Adds all key-value pairs of [other] to this map. 124 * Adds all key-value pairs of [other] to this map.
95 * 125 *
96 * If a key of [other] is already in this map, its value is overwritten. 126 * If a key of [other] is already in this map, its value is overwritten.
97 * 127 *
98 * The operation is equivalent to doing `this[key] = value` for each key 128 * The operation is equivalent to doing `this[key] = value` for each key
99 * and associated value in other. It iterates over [other], which must 129 * and associated value in other. It iterates over [other], which must
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 /** 169 /**
140 * Returns true if there is no {key, value} pair in the map. 170 * Returns true if there is no {key, value} pair in the map.
141 */ 171 */
142 bool get isEmpty; 172 bool get isEmpty;
143 173
144 /** 174 /**
145 * Returns true if there is at least one {key, value} pair in the map. 175 * Returns true if there is at least one {key, value} pair in the map.
146 */ 176 */
147 bool get isNotEmpty; 177 bool get isNotEmpty;
148 } 178 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698