Skip to content

Commit cf5c744

Browse files
committed
GP-145 Hash Table Exercise
* create a Map interface * create a HashTable that implements a Map * add special method like resizeTable and calculateIndex * create a HashTableTest * update pom.xml to configure compiler to preserve parameter names during compilation
1 parent a31db73 commit cf5c744

File tree

7 files changed

+878
-0
lines changed

7 files changed

+878
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>Hash Table
2+
Learn a Hash Table data structures and build strong skills creating a hash table-based implementation of a Map interface 💪
3+
4+
### Objectives (at the end of the training you will be able to...)
5+
* understand the **main idea** behind the **Hash Table** data structure ✅
6+
* explain why do we need a `hashCode` method and how it speeds up the search ✅
7+
* implement key-value class `Node<K,V>`
8+
* implement a simple `HashTable` based on the array of linked `Node` objects
9+
* realize the limitations of the Hash Table ✅
10+
* understand why do we need the resizing logic and how does it work ✅
11+
12+
---
13+
#### 🆕 First time here? – [See Introduction](https://github.com/bobocode-projects/java-fundamentals-course/tree/main/0-0-intro#introduction)
14+
#### ➡️ Have any feedback? – [Please fill the form ](https://forms.gle/7U9XZHuTtT5xpjXR6)
15+
16+
##
17+
<div align="center"><img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/animation/GitHub%20Star_3.gif" height=50/></div>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>2-0-data-structures-and-algorithms</artifactId>
7+
<groupId>com.bobocode</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>2-2-7-hash-table</artifactId>
13+
14+
15+
</project>
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
package com.bobocode.cs;
2+
3+
import com.bobocode.util.ExerciseNotCompletedException;
4+
5+
/**
6+
* {@link HashTable} is a simple Hashtable-based implementation of {@link Map} interface with some additional methods.
7+
* It is based on the array of {@link Node} objects. Both {@link HashTable} and {@link Node} have two type parameters:
8+
* K and V, which represent key and value.
9+
* <p>
10+
* Elements are stored int the table by their key. A table is basically an array, and fast access is possible due to
11+
* array capabilities. (You can access an array element by its index in O(1) time). In order to find an index for any
12+
* given key, it uses calculateIndex method which is based on the element's hash code.
13+
* <p>
14+
* If two elements (keys) have the same array index, they form a linked list. That's why class {@link Node} requires
15+
* a reference to the next field.
16+
* <p>
17+
* Since you don't always know the number of elements in advance, the table can be resized. You can do that manually by
18+
* calling method resizeTable, or it will be done automatically once the table reach resize threshold.
19+
* <p>
20+
* The initial array size (initial capacity) is 8.
21+
*
22+
* @param <K> - key type
23+
* @param <V> - value type
24+
* @author Taras Boychuk
25+
*/
26+
public class HashTable<K, V> implements Map<K, V> {
27+
28+
/**
29+
* This method is a critical part of the hast table. The main idea is that having a key, you can calculate its index
30+
* in the array using the hash code. Since the computation is done in constant time (O(1)), it's faster than
31+
* any other kind search.
32+
* <p>
33+
* It's a function that accepts a key and calculates its index using a hash code. Please note that index cannot be
34+
* equal or greater than array size (table capacity).
35+
* <p>
36+
* This method is used for all other operations (put, get, remove).
37+
*
38+
* @param key
39+
* @param tableCapacity underlying array size
40+
* @return array index of the given key
41+
*/
42+
public static int calculateIndex(Object key, int tableCapacity) {
43+
throw new ExerciseNotCompletedException(); // todo:
44+
}
45+
46+
/**
47+
* Adds an element to the hash table. Does not support duplicate elements.
48+
*
49+
* @param element
50+
* @return true if it was added
51+
*/
52+
@Override
53+
public V put(K element, V value) {
54+
throw new ExerciseNotCompletedException(); // todo:
55+
}
56+
57+
/**
58+
* Retrieves a value by the given key. It uses calculateIndex method to find the corresponding array index.
59+
* Then it iterates though all elements that are stored by that index, and uses equals to compare its keys.
60+
*
61+
* @param key
62+
* @return value stored in the table by the given key or null if there is no such key
63+
*/
64+
@Override
65+
public V get(K key) {
66+
throw new ExerciseNotCompletedException(); // todo:
67+
}
68+
69+
/**
70+
* Checks if the table contains a given key.
71+
*
72+
* @param key
73+
* @return true is there is such key in the table or false otherwise
74+
*/
75+
@Override
76+
public boolean containsKey(K key) {
77+
throw new ExerciseNotCompletedException(); // todo:
78+
}
79+
80+
/**
81+
* Checks if the table contains a given value.
82+
*
83+
* @param value
84+
* @return true is there is such value in the table or false otherwise
85+
*/
86+
@Override
87+
public boolean containsValue(V value) {
88+
throw new ExerciseNotCompletedException(); // todo:
89+
}
90+
91+
/**
92+
* Return a number of elements in the table.
93+
*
94+
* @return size
95+
*/
96+
@Override
97+
public int size() {
98+
throw new ExerciseNotCompletedException(); // todo:
99+
}
100+
101+
/**
102+
* Checks is the table is empty.
103+
*
104+
* @return true is table size is zero or false otherwise
105+
*/
106+
@Override
107+
public boolean isEmpty() {
108+
throw new ExerciseNotCompletedException(); // todo:
109+
}
110+
111+
/**
112+
* Removes an element by its key and returns a removed value. If there is no such key in the table, it returns null.
113+
*
114+
* @param key
115+
* @return removed value or null
116+
*/
117+
@Override
118+
public V remove(K key) {
119+
throw new ExerciseNotCompletedException(); // todo:
120+
}
121+
122+
/**
123+
* It's a special toString method dedicated to help you visualize a hash table. It creates a string that represents
124+
* an underlying array as a table. It has multiples rows. Every row starts with an array index followed by ": ".
125+
* Then it adds every key and value (key=value) that have a corresponding index. Every "next" reference is
126+
* represented as an arrow like this " -> ".
127+
* <p>
128+
* E.g. imagine a table, where the key is a string username, and the value is the number of points of that user.
129+
* Is this case method toString can return something like this:
130+
* <pre>
131+
* 0: johnny=439
132+
* 1:
133+
* 2: madmax=833 -> leon=886
134+
* 3:
135+
* 4: altea=553
136+
* 5:
137+
* 6:
138+
* 7:
139+
* </pre>
140+
*
141+
* @return
142+
*/
143+
@Override
144+
public String toString() {
145+
throw new ExerciseNotCompletedException(); // todo:
146+
}
147+
148+
/**
149+
* Creates a new underlying table with a given size and adds all elements to the new table.
150+
* <p>
151+
* In order to allow a fast access, this hash table needs to have a sufficient capacity.
152+
* (You can imagine a hash table, with a default capacity of 8 that stores hundreds of thousands of elements.
153+
* In that case it's just 8 huge linked lists. That's why we need this method.)
154+
* <p>
155+
* PLEASE NOTE that such method <strong>should not be a part of the public API</strong>, but it was made public
156+
* for learning purposes. You can create a table, print it using toString, then resizeTable and print it again.
157+
* It will help you to understand how it works.
158+
*
159+
* @param newCapacity a size of the new underlying array
160+
*/
161+
public void resizeTable(int newCapacity) {
162+
throw new ExerciseNotCompletedException(); // todo:
163+
}
164+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.bobocode.cs;
2+
3+
/**
4+
* A {@link Map} is a simplified interface of so-called dictionary. It maps keys to values and provides an API for data
5+
* access and manipulation. Please note that a map does not support duplicate keys.
6+
* <p>
7+
* It was added as a simple contact on top of a {@link HashTable} class.
8+
*
9+
* @param <K> key type
10+
* @param <V> value type
11+
* @author Taras Boychuk
12+
*/
13+
public interface Map<K, V> {
14+
/**
15+
* Creates or updates a mapping for a given key and value. If the key is new, it creates a mapping and return null.
16+
* If the key exists, it updates the value and returns the old value.
17+
*
18+
* @param key
19+
* @param value
20+
* @return an old value or null
21+
*/
22+
V put(K key, V value);
23+
24+
/**
25+
* Returns the value that is mapped to the given key, or null if there is no such key.
26+
*
27+
* @param key
28+
* @return the value that is mapped to the given key, or null if there is no such key
29+
*/
30+
V get(K key);
31+
32+
/**
33+
* Returns true if a given key exist or false otherwise.
34+
*
35+
* @param key
36+
* @return true if a given key exist or false otherwise
37+
*/
38+
boolean containsKey(K key);
39+
40+
/**
41+
* Returns true if a given value exist or false otherwise.
42+
*
43+
* @param value
44+
* @return true if a given value exist or false otherwise
45+
*/
46+
boolean containsValue(V value);
47+
48+
/**
49+
* Returns the number of entries (key-value mappings).
50+
*
51+
* @return the number of entries
52+
*/
53+
int size();
54+
55+
/**
56+
* Returns true if there is no entries or false otherwise.
57+
*
58+
* @return true if there is no entries or false otherwise
59+
*/
60+
boolean isEmpty();
61+
62+
/**
63+
* Removes a mapping for a given key, and returns a removed value. If there is no such key, it just returns null.
64+
*
65+
* @param key
66+
* @return a removed value or null
67+
*/
68+
V remove(K key);
69+
}

0 commit comments

Comments
 (0)