OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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.collection; |
6 | 6 |
7 /** | |
8 * A [Map] is an associative container, mapping a key to a value. | |
9 * Null values are supported, but null keys are not. | |
10 */ | |
11 abstract class Map<K, V> { | |
12 /** | |
13 * Creates a map with the default implementation. | |
14 */ | |
15 factory Map() => new _HashMapImpl<K, V>(); | |
16 | |
17 /** | |
18 * Creates a [Map] that contains all key value pairs of [other]. | |
19 */ | |
20 factory Map.from(Map<K, V> other) => new _HashMapImpl<K, V>.from(other); | |
21 | |
22 | |
23 /** | |
24 * Returns whether this map contains the given [value]. | |
25 */ | |
26 bool containsValue(V value); | |
27 | |
28 /** | |
29 * Returns whether this map contains the given [key]. | |
30 */ | |
31 bool containsKey(K key); | |
32 | |
33 /** | |
34 * Returns the value for the given [key] or null if [key] is not | |
35 * in the map. Because null values are supported, one should either | |
36 * use containsKey to distinguish between an absent key and a null | |
37 * value, or use the [putIfAbsent] method. | |
38 */ | |
39 V operator [](K key); | |
40 | |
41 /** | |
42 * Associates the [key] with the given [value]. | |
43 */ | |
44 void operator []=(K key, V value); | |
45 | |
46 /** | |
47 * If [key] is not associated to a value, calls [ifAbsent] and | |
48 * updates the map by mapping [key] to the value returned by | |
49 * [ifAbsent]. Returns the value in the map. | |
50 */ | |
51 V putIfAbsent(K key, V ifAbsent()); | |
52 | |
53 /** | |
54 * Removes the association for the given [key]. Returns the value for | |
55 * [key] in the map or null if [key] is not in the map. Note that values | |
56 * can be null and a returned null value does not always imply that the | |
57 * key is absent. | |
58 */ | |
59 V remove(K key); | |
60 | |
61 /** | |
62 * Removes all pairs from the map. | |
63 */ | |
64 void clear(); | |
65 | |
66 /** | |
67 * Applies [f] to each {key, value} pair of the map. | |
68 */ | |
69 void forEach(void f(K key, V value)); | |
70 | |
71 /** | |
72 * The keys of [this]. | |
73 */ | |
74 // TODO(floitsch): this should return a [Set]. | |
75 Iterable<K> get keys; | |
76 | |
77 /** | |
78 * The values of [this]. | |
79 */ | |
80 Iterable<V> get values; | |
81 | |
82 /** | |
83 * The number of {key, value} pairs in the map. | |
84 */ | |
85 int get length; | |
86 | |
87 /** | |
88 * Returns true if there is no {key, value} pair in the map. | |
89 */ | |
90 bool get isEmpty; | |
91 } | |
92 | 7 |
93 /** | 8 /** |
94 * Hash map version of the [Map] interface. A [HashMap] does not | 9 * Hash map version of the [Map] interface. A [HashMap] does not |
95 * provide any guarantees on the order of keys and values in [keys] | 10 * provide any guarantees on the order of keys and values in [keys] |
96 * and [values]. | 11 * and [values]. |
97 */ | 12 */ |
98 abstract class HashMap<K, V> extends Map<K, V> { | 13 abstract class HashMap<K, V> extends Map<K, V> { |
99 /** | 14 /** |
100 * Creates a map with the default implementation. | 15 * Creates a map with the default implementation. |
101 */ | 16 */ |
(...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
550 | 465 |
551 void clear() { | 466 void clear() { |
552 _map.clear(); | 467 _map.clear(); |
553 _list.clear(); | 468 _list.clear(); |
554 } | 469 } |
555 | 470 |
556 String toString() { | 471 String toString() { |
557 return Maps.mapToString(this); | 472 return Maps.mapToString(this); |
558 } | 473 } |
559 } | 474 } |
560 | |
OLD | NEW |