Maps/ADT: Difference between revisions
From charlesreid1
No edit summary |
(Overhaul: add introduction, organize operations into clear categories with tables, fix typos, expand Java section with full interface, add expected complexity table, add See also links. (via update-page on MediaWiki MCP Server)) |
||
| Line 1: | Line 1: | ||
The '''Map Abstract Data Type''' (ADT) defines the interface contract that all [[Maps|map]] implementations must satisfy. It specifies ''what'' operations a map supports without prescribing ''how'' they are implemented — the hallmark of an ADT. A concrete map implementation may use a hash table, a balanced tree, a sorted array, or any other suitable data structure. | |||
A map stores key-value pairs. Each key maps to exactly one value (the map is a [[Wikipedia:Function (mathematics)|function]] from a domain of keys to a codomain of values). Keys must be unique; distinct keys may map to the same value. | |||
==Basic operations== | |||
These five operations constitute the essential interface of any map ADT: | |||
{| class="wikitable" | |||
|- | |||
! Operation !! Pseudocode signatures !! Description | |||
|- | |||
| '''get''' || <code>m[k]</code>, <code>m.get(k)</code> | |||
| Return the value associated with key <code>k</code>; raise a ''key error'' if <code>k</code> is absent. | |||
|- | |||
| '''set''' || <code>m[k] = v</code>, <code>m.put(k, v)</code> | |||
| Associate value <code>v</code> with key <code>k</code>, overwriting any previous mapping. | |||
|- | |||
| '''remove''' || <code>del m[k]</code>, <code>m.remove(k)</code> | |||
| Remove the entry for key <code>k</code>; raise a key error if absent. | |||
|- | |||
| '''size''' || <code>len(m)</code>, <code>m.size()</code> | |||
| Return the number of key-value pairs in the map. | |||
|- | |||
| '''iterate''' || <code>iter(m)</code> | |||
| Return an iterator over the keys of the map. | |||
|} | |||
==Convenience operations== | |||
These operations can each be expressed in terms of the basic five above, but are provided for programmer convenience. | |||
===Lookup=== | |||
* <code>m.contains(k)</code> / <code>k '''in''' m</code> — test whether key <code>k</code> exists in the map; returns boolean | |||
* <code>m.get(k, d)</code> — return <code>m[k]</code> if key <code>k</code> exists; otherwise return default value <code>d</code> | |||
* <code>m.setdefault(k, d)</code> — if key <code>k</code> exists return <code>m[k]</code>; otherwise set <code>m[k] = d</code> and return <code>d</code> | |||
== | ===Modification=== | ||
* <code>m.pop(k, d)</code> — remove key <code>k</code> and return its associated value; return <code>d</code> if key is absent | |||
* <code>m.popitem()</code> — remove and return an arbitrary <code>(k, v)</code> pair; raise a key error if the map is empty | |||
* <code>m.clear()</code> — remove all key-value pairs from the map | |||
===Views=== | |||
* <code>m.keys()</code> — return a set-like view of all keys | |||
* <code>m.values()</code> — return a set-like view of all values | |||
* <code>m.items()</code> — return a set-like view of all <code>(k, v)</code> tuples | |||
Views are live: changes to the underlying map are reflected in the view, and vice versa. | |||
===Bulk operations=== | |||
* <code>m.update(m2)</code> — for every <code>(k, v)</code> pair in map <code>m2</code>, set <code>m[k] = v</code> | |||
* <code>m.equals(m2)</code> — return true if the two maps contain the same key-value pairs | |||
==Expected complexity== | |||
While the ADT itself is implementation-agnostic, useful map implementations achieve the following amortized bounds: | |||
{| class="wikitable" | |||
|- | |||
! Implementation !! <code>get</code> !! <code>put</code> !! <code>remove</code> !! <code>contains</code> | |||
|- | |||
| Hash table || O(1) || O(1) || O(1) || O(1) | |||
|- | |||
| Balanced BST || O(log n) || O(log n) || O(log n) || O(log n) | |||
|- | |||
| Unsorted array || O(n) || O(n) || O(n) || O(n) | |||
|- | |||
| Sorted array || O(log n) || O(n) || O(n) || O(log n) | |||
|} | |||
==Java Map interface== | |||
The Java Collections Framework specifies the map ADT through the <code>[http://docs.oracle.com/javase/8/docs/api/java/util/Map.html java.util.Map<K,V>]</code> interface. | |||
===Core methods=== | |||
{| class="wikitable" | |||
|- | |||
! Method !! Description | |||
|- | |||
| <code>size()</code> || Return the number of key-value mappings. | |||
|- | |||
| <code>isEmpty()</code> || Return <code>true</code> if the map contains no mappings. | |||
|- | |||
| <code>get(Object key)</code> || Return the value to which <code>key</code> is mapped, or <code>null</code> if absent. | |||
|- | |||
| <code>put(K key, V value)</code> || Associate <code>value</code> with <code>key</code>; return the previous value, or <code>null</code>. | |||
|- | |||
| <code>remove(Object key)</code> || Remove the mapping for <code>key</code>; return the previous value, or <code>null</code>. | |||
|- | |||
| <code>containsKey(Object key)</code> || Return <code>true</code> if the map contains <code>key</code>. | |||
|- | |||
| <code>containsValue(Object value)</code> || Return <code>true</code> if the map maps one or more keys to <code>value</code>. | |||
|- | |||
| <code>keySet()</code> || Return a <code>Set<K></code> view of the keys. | |||
|- | |||
| <code>values()</code> || Return a <code>Collection<V></code> view of the values. | |||
|- | |||
| <code>entrySet()</code> || Return a <code>Set<Map.Entry<K,V>></code> view of the entries. | |||
|} | |||
===Bulk and default methods=== | |||
{| class="wikitable" | |||
|- | |||
! Method !! Description | |||
|- | |||
| <code>putAll(Map m)</code> || Copy all mappings from <code>m</code> into this map. | |||
|- | |||
| <code>clear()</code> || Remove all mappings. | |||
|- | |||
| <code>getOrDefault(Object key, V def)</code> || Return value for <code>key</code>, or <code>def</code> if absent. (Java 8) | |||
|- | |||
| <code>putIfAbsent(K key, V value)</code> || Insert only if <code>key</code> is not already present. (Java 8) | |||
|- | |||
| <code>replace(K key, V old, V new)</code> || Replace value only if currently mapped to <code>old</code>. (Java 8) | |||
|- | |||
| <code>computeIfAbsent(K key, Function f)</code> || If <code>key</code> is absent, compute and insert a value via <code>f</code>. (Java 8) | |||
|- | |||
| <code>merge(K key, V val, BiFunction remap)</code> || Merge <code>val</code> with existing value using <code>remap</code>. (Java 8) | |||
|} | |||
Standard implementations: <code>HashMap</code> (hash table, O(1) expected), <code>TreeMap</code> (red-black tree, O(log n)), <code>LinkedHashMap</code> (hash table with insertion-order iteration). | |||
==See also== | |||
* [[Maps]] — main Maps page with implementation notes | |||
* [[Maps/AbstractMap]] — abstract base class | |||
* [[Maps/UnsortedArrayMap]] — array-based concrete implementation | |||
* [[Maps/SortedArrayMap]] — sorted-array concrete implementation | |||
* [[Hash Maps]] — hash-table implementations (chaining and probing) | |||
* [[Maps/Operations and Performance]] — timing benchmarks | |||
{{MapsFlag}} | {{MapsFlag}} | ||
Revision as of 04:09, 8 June 2026
The Map Abstract Data Type (ADT) defines the interface contract that all map implementations must satisfy. It specifies what operations a map supports without prescribing how they are implemented — the hallmark of an ADT. A concrete map implementation may use a hash table, a balanced tree, a sorted array, or any other suitable data structure.
A map stores key-value pairs. Each key maps to exactly one value (the map is a function from a domain of keys to a codomain of values). Keys must be unique; distinct keys may map to the same value.
Basic operations
These five operations constitute the essential interface of any map ADT:
| Operation | Pseudocode signatures | Description |
|---|---|---|
| get | m[k], m.get(k)
|
Return the value associated with key k; raise a key error if k is absent.
|
| set | m[k] = v, m.put(k, v)
|
Associate value v with key k, overwriting any previous mapping.
|
| remove | del m[k], m.remove(k)
|
Remove the entry for key k; raise a key error if absent.
|
| size | len(m), m.size()
|
Return the number of key-value pairs in the map. |
| iterate | iter(m)
|
Return an iterator over the keys of the map. |
Convenience operations
These operations can each be expressed in terms of the basic five above, but are provided for programmer convenience.
Lookup
m.contains(k)/k in m— test whether keykexists in the map; returns booleanm.get(k, d)— returnm[k]if keykexists; otherwise return default valuedm.setdefault(k, d)— if keykexists returnm[k]; otherwise setm[k] = dand returnd
Modification
m.pop(k, d)— remove keykand return its associated value; returndif key is absentm.popitem()— remove and return an arbitrary(k, v)pair; raise a key error if the map is emptym.clear()— remove all key-value pairs from the map
Views
m.keys()— return a set-like view of all keysm.values()— return a set-like view of all valuesm.items()— return a set-like view of all(k, v)tuples
Views are live: changes to the underlying map are reflected in the view, and vice versa.
Bulk operations
m.update(m2)— for every(k, v)pair in mapm2, setm[k] = vm.equals(m2)— return true if the two maps contain the same key-value pairs
Expected complexity
While the ADT itself is implementation-agnostic, useful map implementations achieve the following amortized bounds:
| Implementation | get |
put |
remove |
contains
|
|---|---|---|---|---|
| Hash table | O(1) | O(1) | O(1) | O(1) |
| Balanced BST | O(log n) | O(log n) | O(log n) | O(log n) |
| Unsorted array | O(n) | O(n) | O(n) | O(n) |
| Sorted array | O(log n) | O(n) | O(n) | O(log n) |
Java Map interface
The Java Collections Framework specifies the map ADT through the java.util.Map<K,V> interface.
Core methods
| Method | Description |
|---|---|
size() |
Return the number of key-value mappings. |
isEmpty() |
Return true if the map contains no mappings.
|
get(Object key) |
Return the value to which key is mapped, or null if absent.
|
put(K key, V value) |
Associate value with key; return the previous value, or null.
|
remove(Object key) |
Remove the mapping for key; return the previous value, or null.
|
containsKey(Object key) |
Return true if the map contains key.
|
containsValue(Object value) |
Return true if the map maps one or more keys to value.
|
keySet() |
Return a Set<K> view of the keys.
|
values() |
Return a Collection<V> view of the values.
|
entrySet() |
Return a Set<Map.Entry<K,V>> view of the entries.
|
Bulk and default methods
| Method | Description |
|---|---|
putAll(Map m) |
Copy all mappings from m into this map.
|
clear() |
Remove all mappings. |
getOrDefault(Object key, V def) |
Return value for key, or def if absent. (Java 8)
|
putIfAbsent(K key, V value) |
Insert only if key is not already present. (Java 8)
|
replace(K key, V old, V new) |
Replace value only if currently mapped to old. (Java 8)
|
computeIfAbsent(K key, Function f) |
If key is absent, compute and insert a value via f. (Java 8)
|
merge(K key, V val, BiFunction remap) |
Merge val with existing value using remap. (Java 8)
|
Standard implementations: HashMap (hash table, O(1) expected), TreeMap (red-black tree, O(log n)), LinkedHashMap (hash table with insertion-order iteration).
See also
- Maps — main Maps page with implementation notes
- Maps/AbstractMap — abstract base class
- Maps/UnsortedArrayMap — array-based concrete implementation
- Maps/SortedArrayMap — sorted-array concrete implementation
- Hash Maps — hash-table implementations (chaining and probing)
- Maps/Operations and Performance — timing benchmarks
| Maps and Dictionaries Part of Computer Science Notes
Series on Data Structures
Maps/Dictionaries Maps · Maps/ADT · Maps in Java · Maps/OOP · Maps/Operations and Performance Map implementations: Maps/AbstractMap · Maps/UnsortedArrayMap · Maps/SortedArrayMap Dictionary implementations: Dictionaries/LinkedDict · Dictionaries/ArrayDict
Hashes Hash Maps/OOP · Hash Maps/Operations and Performance Hash Maps/Dynamic Resizing · Hash Maps/Collision Handling with Chaining Hash functions: Hash Functions · Hash Functions/Cyclic Permutation Hash map implementations: Hash Maps/AbstractHashMap · Hash Maps/ChainedHashMap
Skip Lists · Java/ConcurrentSkipList · Java implementations: SkipList
Sets Sets · Sets/ADT · Sets in Java · Sets/OOP · Multisets
|