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:
==Map Abstract Data Type==
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.


The map abstract data type must support the following basic operations:
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.
* m[k] or m.get(k) - get the value v associated with key k in map m, returns key error
* m[k] = v or m.set(k,v) or m.put(k,v) - set the value v associated with key k in map m
* del m[k] or m.remove(k) - remove the element with key k in map m
* len(m) or m.size() - return the number of elements (unique keys) in the map
* iter(m) - iterator over keys of map


Other convenience methods:
==Basic operations==
* m.contains(k) or k in m - perform search to see if map m contains key k
* m.get(k, d=None) - return m[k] if key k exists in the map, otherwise return a default value in place of a key error
* m.setdefault(k,d) - if key k exists in map, return m[k]. otherwise, set m[k] to default value d and return value
* m.pop(k, d=None) - remove item associated with key k from map and return associated value v
* m.popItem() - remove arbitrary key-vlue pair from the map, and return a (k,v) tuple representing the removed pair. raise key error if empty map.
* m.clear() - remove key-value pairs from map
* m.keys() - return set-like view of keys
* m.value() - return set-like view of values
* m.items() - return set-like view of (k,v) tuples for entries of m
* m.update(m2) - assign m[k] = v for every (k,v) pair in map m2
* m.equals(m2) m.compareTo(m2) etc.


==Java Variation==
These five operations constitute the essential interface of any map ADT:


Basic operations:
{| class="wikitable"
* size()
|-
* isEmpty()
! Operation !! Pseudocode signatures !! Description
* get(k)
|-
* put(k,v)
| '''get''' || <code>m[k]</code>, <code>m.get(k)</code>
* remove(k)
| Return the value associated with key <code>k</code>; raise a ''key error'' if <code>k</code> is absent.
* keySet()
|-
* values()
| '''set''' || <code>m[k] = v</code>, <code>m.put(k, v)</code>
* entrySet()
| 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>


==Flags==
===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&lt;K,V&gt;]</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&lt;K&gt;</code> view of the keys.
|-
| <code>values()</code> || Return a <code>Collection&lt;V&gt;</code> view of the values.
|-
| <code>entrySet()</code> || Return a <code>Set&lt;Map.Entry&lt;K,V&gt;&gt;</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 key k exists in the map; returns boolean
  • m.get(k, d) — return m[k] if key k exists; otherwise return default value d
  • m.setdefault(k, d) — if key k exists return m[k]; otherwise set m[k] = d and return d

Modification

  • m.pop(k, d) — remove key k and return its associated value; return d if key is absent
  • m.popitem() — remove and return an arbitrary (k, v) pair; raise a key error if the map is empty
  • m.clear() — remove all key-value pairs from the map

Views

  • m.keys() — return a set-like view of all keys
  • m.values() — return a set-like view of all values
  • m.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 map m2, set m[k] = v
  • m.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