How to implement the "chain method"?
Chaining method
Collision resolution using chains. Each cell of the array H is a pointer to a linked list (chain) of key-value pairs corresponding to the same key hash value. Collisions simply lead to the fact that there are chains with a length of more than one element.
Operations of searching or deleting an element require viewing all the elements of the corresponding chain in order to find an element with a given key in it. To add an element, you need to add an element to the end or the beginning of the corresponding list, and if the fill factor becomes too large, increase the size of the array H and rebuild the table.
Assuming that each element can fall into any position of the table H with equal probability, and regardless of where any other element falls, the average operation time of the element search operation is Θ (1 + α), where α is the table fill factor.
import java.util.Map; import java.util.HashMap; public class hashmap { public static void main(String[] args) { Map<String, String> hashmap = new HashMap<String, String>(); hashmap.put("key1", "value1"); hashmap.put("key1", "value2"); } }