Hash Map
最終更新
ハッシュマップはキーと値のペアを格納し、キーによって値を平均 O(1) で検索できます。ハッシュテーブルとまったく同じように動作しますが、各バケットのエントリはキーとそれに対応する値の両方を保持します。ペアを格納または取得するには、キーをバケットのインデックスにハッシュし、そのバケットのチェーンを走査して一致するキーを探します。上の再生を押すと、ペアがハッシュされたバケットに配置され、値がキーで取得される様子を見られます。
これは Python の dict、Java の HashMap、JavaScript の Map/オブジェクトの背後にある構造です。衝突はハッシュテーブルと同じ方法で処理され、ここでは分離連鎖法で扱われます。そのため性能は、良いハッシュ関数と低い負荷率に依存します。
時間計算量
| 操作 | 平均 | 最悪の場合 |
|---|---|---|
| 挿入/更新 (put) | O(1) | O(n) |
| 検索 (get) | O(1) | O(n) |
| 削除 | O(1) | O(n) |
| 空間 | O(n) | O(n) |
主要言語でのハッシュマップ
| 言語 | 型 |
|---|---|
| Python | dict |
| Java | HashMap |
| JavaScript | Map / オブジェクト |
| C++ | std::unordered_map |
| Go | map |
具体例
hash(key) % 8 を使って、ペア ("cat", 3)、("dog", 5)、("cat", 9)、("emu", 7) を 8 個のバケットを持つマップに挿入します。hash("cat") % 8 = 2、hash("dog") % 8 = 5、hash("emu") % 8 = 2 と仮定します:
| ステップ | 構造 | 動作 |
|---|---|---|
Put ("cat", 3) | バケット 2: [("cat", 3)] | バケット 2 にハッシュ。チェーンが空なのでペアを追加。 |
Put ("dog", 5) | バケット 2: [("cat", 3)], バケット 5: [("dog", 5)] | バケット 5 にハッシュ。チェーンが空なのでペアを追加。 |
Put ("cat", 9) | バケット 2: [("cat", 9)], バケット 5: [("dog", 5)] | バケット 2 にハッシュ。キー "cat" は既にチェーン内にあるので、値を 9 に更新。 |
Put ("emu", 7) | バケット 2: [("cat", 9), ("emu", 7)], バケット 5: [("dog", 5)] | バケット 2 にハッシュ。"cat" と衝突するがキーは見つからないので、ペアを追加。 |
Get "emu" | バケット 2: [("cat", 9), ("emu", 7)] | バケット 2 にハッシュ。チェーンを走査し、"cat" をスキップして "emu" に一致、7 を返す。 |
ハッシュマップを使うべきとき
| 使うべきとき | 避けるべきとき |
|---|---|
正確なキーによる平均 O(1) の検索・挿入・削除が必要なとき。 | キーをソート順に保つ、または範囲クエリが必要なとき — 平衡BSTやソート済みマップを使う。 |
| キーがハッシュ可能で等価性が明確に定義されているとき(文字列、整数、タプル)。 | キーが可変で挿入後に変わり得るとき。そのバケットが壊れる。 |
| 識別子でカウント、重複排除、キャッシュ、インデックス付けをしているとき。 | 償却された平均計算量ではなく、最悪の場合 O(log n) の保証が必要なとき。 |
| 反復順序が重要でない、または挿入順マップで十分なとき。 | メモリが非常に限られているとき — バケット、負荷率の余裕、ポインタがオーバーヘッドを加える。 |
Hash Mapのコード
Python, JavaScript, Java, C++, Cによるクリーンで実行可能なHash Mapの実装です。言語を選んでコードをコピーするか、Coddyプレイグラウンドに読み込んだ状態で開けます。
PythonでのHash Mapのコード
1# Python's built-in dict is a hash map: O(1) average2# insert, lookup, and delete.3inventory = {"apple": 3, "banana": 7}4
5# Insert and update6inventory["cherry"] = 57inventory["apple"] += 28
9# Lookup, with .get for a safe default on missing keys10print("apple: ", inventory["apple"])11print("mango: ", inventory.get("mango", 0))12
13# Membership test and delete14print("banana in stock:", "banana" in inventory)15del inventory["banana"]16print("banana in stock:", "banana" in inventory)17
18# Iterate over key-value pairs19for fruit, count in sorted(inventory.items()):20 print(f"{fruit}: {count}")21
22# Classic hash map use case: counting frequencies23words = "the quick brown fox jumps over the lazy dog the end".split()24freq = {}25for word in words:26 freq[word] = freq.get(word, 0) + 127
28print("Occurrences of the:", freq["the"])29print("Most common word: ", max(freq, key=freq.get))JavaScriptでのHash Mapのコード
1// The built-in Map is the idiomatic hash map in JavaScript:2// any key type, insertion-ordered iteration, O(1) average lookups.3const ages = new Map();4
5ages.set("Alice", 30);6ages.set("Bob", 25);7ages.set("Carol", 35);8ages.set("Bob", 26); // set on an existing key updates the value9
10console.log("Bob:", ages.get("Bob"));11console.log("Has Dave?", ages.has("Dave"));12console.log("Size:", ages.size);13
14ages.delete("Carol");15console.log("Size after delete:", ages.size);16
17for (const [name, age] of ages) {18 console.log(`${name} is ${age}`);19}20
21console.log("Keys:", [...ages.keys()].join(", "));JavaでのHash Mapのコード
1import java.util.HashMap;2import java.util.Map;3
4public class Main {5 public static void main(String[] args) {6 Map<String, Integer> ages = new HashMap<>();7 ages.put("Alice", 30);8 ages.put("Bob", 25);9 ages.put("Carol", 35);10
11 System.out.println("Bob -> " + ages.get("Bob"));12 System.out.println("contains Carol: " + ages.containsKey("Carol"));13 System.out.println("Dave or default: " + ages.getOrDefault("Dave", -1));14
15 // getOrDefault makes frequency counting a one-liner16 String[] words = {"apple", "banana", "apple", "cherry", "apple"};17 Map<String, Integer> counts = new HashMap<>();18 for (String w : words) {19 counts.put(w, counts.getOrDefault(w, 0) + 1);20 }21 System.out.println("apple count: " + counts.get("apple"));22
23 ages.remove("Bob");24 System.out.println("size after remove: " + ages.size());25
26 for (Map.Entry<String, Integer> entry : ages.entrySet()) {27 System.out.println(entry.getKey() + " is " + entry.getValue());28 }29 }30}C++でのHash Mapのコード
1#include <algorithm>2#include <iostream>3#include <string>4#include <unordered_map>5#include <vector>6
7int main() {8 std::unordered_map<std::string, int> ages;9
10 // Insert and update11 ages["alice"] = 30; // operator[] inserts or overwrites12 ages.insert({"bob", 25}); // insert keeps an existing value13 ages.emplace("carol", 41);14 ages["alice"] = 31;15
16 // Lookup17 auto it = ages.find("bob");18 if (it != ages.end()) {19 std::cout << "bob is " << it->second << "\n";20 }21 std::cout << "has dave: " << ages.count("dave") << "\n";22
23 // Remove24 ages.erase("carol");25
26 // Iterate (sort keys first: unordered_map has no defined order)27 std::vector<std::string> keys;28 for (const auto& entry : ages) keys.push_back(entry.first);29 std::sort(keys.begin(), keys.end());30 for (const std::string& name : keys) {31 std::cout << name << " = " << ages[name] << "\n";32 }33 std::cout << "size: " << ages.size() << "\n";34 return 0;35}CでのHash Mapのコード
1#include <stdbool.h>2#include <stdio.h>3#include <stdlib.h>4#include <string.h>5
6#define BUCKETS 167
8typedef struct Node {9 char key[24];10 int value;11 struct Node* next;12} Node;13
14// A small string -> int hash map with a reusable put/get/remove API15typedef struct {16 Node* buckets[BUCKETS];17 int size;18} HashMap;19
20unsigned int hashKey(const char* key) {21 unsigned int h = 5381;22 for (; *key; key++) h = h * 33 + (unsigned char)*key;23 return h % BUCKETS;24}25
26void mapPut(HashMap* map, const char* key, int value) {27 unsigned int i = hashKey(key);28 for (Node* n = map->buckets[i]; n != NULL; n = n->next) {29 if (strcmp(n->key, key) == 0) {30 n->value = value;31 return;32 }33 }34 Node* n = malloc(sizeof(Node));35 strcpy(n->key, key);36 n->value = value;37 n->next = map->buckets[i];38 map->buckets[i] = n;39 map->size++;40}41
42bool mapGet(const HashMap* map, const char* key, int* out) {43 for (Node* n = map->buckets[hashKey(key)]; n != NULL; n = n->next) {44 if (strcmp(n->key, key) == 0) {45 *out = n->value;46 return true;47 }48 }49 return false;50}51
52void mapRemove(HashMap* map, const char* key) {53 Node** link = &map->buckets[hashKey(key)];54 while (*link != NULL) {55 if (strcmp((*link)->key, key) == 0) {56 Node* old = *link;57 *link = old->next;58 free(old);59 map->size--;60 return;61 }62 link = &(*link)->next;63 }64}65
66int main(void) {67 HashMap map = {0};68 mapPut(&map, "alice", 30);69 mapPut(&map, "bob", 25);70 mapPut(&map, "carol", 41);71 mapPut(&map, "alice", 31); // updates the existing key72 int age;73 if (mapGet(&map, "bob", &age)) printf("bob is %d\n", age);74 printf("has dave: %s\n", mapGet(&map, "dave", &age) ? "yes" : "no");75 mapRemove(&map, "carol");76 printf("size: %d\n", map.size);77 return 0;78}ハッシュマップのよくある質問
ハッシュマップとハッシュテーブルの違いは何ですか?
ハッシュマップの時間計算量はどのくらいですか?
O(1) です。すべてのキーが1つのバケットに衝突する病的なケースでは O(n) に劣化するため、リサイズと良いハッシュが重要になります。ハッシュマップは同じバケットにハッシュされる2つのキーをどう扱いますか?
ハッシュマップと二分探索木のどちらを使うべきですか?
O(1) の操作を求める場合はハッシュマップを使います。ソートされたキー、範囲クエリ、前者/後者の検索が必要な場合は、平衡二分探索木(TreeMap や std::map など)を使い、これらは O(log n) です。BST は、ハッシュマップが提供できない順序保証と引き換えに少し速度を犠牲にします。負荷率とは何で、なぜリサイズを引き起こすのですか?
0.75 のようなしきい値を超えるとリサイズします(通常はバケットを倍にしてすべてを再ハッシュ)。リサイズは O(n) ですが稀なので償却され、平均操作を O(1) に保ちます。リストのような可変オブジェクトをハッシュマップのキーに使えますか?
list に対して TypeError: unhashable type を送出します。挿入後にキーを変更するとハッシュが変わり、マップは正しいバケットでそれを見つけられなくなり、静かにエントリを失います。文字列、数値、タプルのような不変のキーを使いましょう。