|
| 1 | +package com.thealgorithms.datastructures.hashmap.hashing; |
| 2 | + |
| 3 | +/** |
| 4 | + * Immutable HashMap implementation using separate chaining. |
| 5 | + * |
| 6 | + * <p>This HashMap does not allow modification of existing instances. |
| 7 | + * Any update operation returns a new ImmutableHashMap. |
| 8 | + * |
| 9 | + * @param <K> key type |
| 10 | + * @param <V> value type |
| 11 | + */ |
| 12 | +public final class ImmutableHashMap<K, V> { |
| 13 | + |
| 14 | + private static final int DEFAULT_CAPACITY = 16; |
| 15 | + |
| 16 | + private final Node<K, V>[] table; |
| 17 | + private final int size; |
| 18 | + |
| 19 | + /** |
| 20 | + * Private constructor to enforce immutability. |
| 21 | + */ |
| 22 | + private ImmutableHashMap(Node<K, V>[] table, int size) { |
| 23 | + this.table = table; |
| 24 | + this.size = size; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Creates an empty ImmutableHashMap. |
| 29 | + * |
| 30 | + * @param <K> key type |
| 31 | + * @param <V> value type |
| 32 | + * @return empty ImmutableHashMap |
| 33 | + */ |
| 34 | + @SuppressWarnings({"unchecked", "rawtypes"}) |
| 35 | + public static <K, V> ImmutableHashMap<K, V> empty() { |
| 36 | + Node<K, V>[] table = (Node<K, V>[]) new Node[DEFAULT_CAPACITY]; |
| 37 | + return new ImmutableHashMap<>(table, 0); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Returns a new ImmutableHashMap with the given key-value pair added. |
| 42 | + * |
| 43 | + * @param key key to add |
| 44 | + * @param value value to associate |
| 45 | + * @return new ImmutableHashMap instance |
| 46 | + */ |
| 47 | + public ImmutableHashMap<K, V> put(K key, V value) { |
| 48 | + Node<K, V>[] newTable = table.clone(); |
| 49 | + int index = hash(key); |
| 50 | + |
| 51 | + newTable[index] = new Node<>(key, value, newTable[index]); |
| 52 | + return new ImmutableHashMap<>(newTable, size + 1); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Retrieves the value associated with the given key. |
| 57 | + * |
| 58 | + * @param key key to search |
| 59 | + * @return value if found, otherwise null |
| 60 | + */ |
| 61 | + public V get(K key) { |
| 62 | + int index = hash(key); |
| 63 | + Node<K, V> current = table[index]; |
| 64 | + |
| 65 | + while (current != null) { |
| 66 | + if ((key == null && current.key == null) || (key != null && key.equals(current.key))) { |
| 67 | + return current.value; |
| 68 | + } |
| 69 | + current = current.next; |
| 70 | + } |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Checks whether the given key exists in the map. |
| 76 | + * |
| 77 | + * @param key key to check |
| 78 | + * @return true if key exists, false otherwise |
| 79 | + */ |
| 80 | + public boolean containsKey(K key) { |
| 81 | + return get(key) != null; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Returns the number of key-value pairs. |
| 86 | + * |
| 87 | + * @return size of the map |
| 88 | + */ |
| 89 | + public int size() { |
| 90 | + return size; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Computes hash index for a given key. |
| 95 | + */ |
| 96 | + private int hash(K key) { |
| 97 | + return key == null ? 0 : (key.hashCode() & Integer.MAX_VALUE) % table.length; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Node class for separate chaining. |
| 102 | + */ |
| 103 | + private static final class Node<K, V> { |
| 104 | + |
| 105 | + private final K key; |
| 106 | + private final V value; |
| 107 | + private final Node<K, V> next; |
| 108 | + |
| 109 | + private Node(K key, V value, Node<K, V> next) { |
| 110 | + this.key = key; |
| 111 | + this.value = value; |
| 112 | + this.next = next; |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments