التجزئة (Hashing)
في هذه الصفحة 8 موضوعات
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
نظرة عامة
الموضوعات
- دوال التجزئة
- جداول التجزئة (hash tables)
- التصادمات (collisions)
- المجموعات المبنية على التجزئة
تعلّم أولًا
55 دقيقة قراءة · 3 ساعة تدريب (تقدير)
مخرجات التعلم
- Explain how a hash table maps keys to buckets.
- Describe collision handling (chaining, open addressing) and load-factor resizing.
- Explain why a bad
hashCodedegradesHashMapperformance and why mutable keys break it.
الدروس
المختبر والتمارين
Lab
BEG-041-LAB1 · MyHashMap
Outcomes: 1–3 · Time: ~3 h
- Implement a chaining-based
MyHashMap<K,V>withput,get,remove,size, and load-factor resizing. - Test it, including replacing an existing key and a missing key.
- Create a key class with a constant
hashCode; time 20,000 inserts and lookups with it and with a goodhashCode; record both timings.
Acceptance: tests pass; timing evidence.
Exercises (assessment: implementation plus quiz)
| ID | Outcome | Task |
|---|---|---|
| BEG-041-EX01 | 1 | With 5 buckets and index = hash % 5, place keys with hashes 12, 7, 22, 3, 17 and say where the collisions are. |
| BEG-041-EX02 | 1 | Explain in three sentences how get finds a value without scanning the whole table. |
| BEG-041-EX03 | 2 | Compare chaining and open addressing in two sentences each. |
| BEG-041-EX04 | 2 | A table has 16 buckets and load factor 0.75. At which entry count does it resize, and to what size? |
| BEG-041-EX05 | 3 | A Point class overrides equals but not hashCode. Describe what goes wrong when you put it in a HashSet. |
| BEG-041-EX06 | 3 | A key is stored in a HashMap, then one of its fields changes. What happens to get(key), and how do you avoid it? |
أظهر الحلول
- EX01 Indexes: 12 -> 2, 7 -> 2, 22 -> 2, 3 -> 3, 17 -> 2. Keys 12, 7, 22, 17 collide in bucket 2; 3 is alone in bucket 3.
- EX02
getcomputes the key's hash, turns it into a bucket index, and opens only that bucket. It compares keys withequalsalong the short chain there. The rest of the table is never touched. - EX03 Chaining: each bucket holds a list of entries, and colliding keys are added to it; easy to implement. Open addressing: all entries live in the array itself, and a collision probes for the next free slot; saves node objects but needs care when removing.
- EX04 Resize when entries exceed 12 (16 x 0.75); new capacity 32.
- EX05 Two equal points can have different hash codes, so they land in different buckets and the set may hold duplicates or fail
contains. Always override both methods together. - EX06 The hash changes, so
getlooks in a different bucket and returnsnull(the entry is still inside, unreachable). Use immutable keys such asString,Integer, or records.
الاختبار
Outcomes: O1 hash tables and buckets · O2 collisions and resizing · O3 bad hash codes and mutable keys.
اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.
الجزء العملي
Implementation of MyHashMap (lab BEG-041-LAB1) with timing evidence, plus this quiz and exercises BEG-041-EX01 to EX06. Pass: tests pass, timing shows the constant-hash slowdown, and at least 8 of 10 quiz answers correct.
أخطاء شائعة
Mutating a key after insertion; assuming HashMap iteration order.
التقييم
Implementation plus quiz.
الصلة بالمسار الوظيفي
Caching (MID-037), partitioning (MID-028), sharding (SRC-007).