Skip to content

Commit b9914f2

Browse files
author
Koushik Sai
committed
Add immutable HashMap implementation
1 parent d183803 commit b9914f2

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/main/java/com/thealgorithms/datastructures/hashmap/hashing/ImmutableHashMap.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
/**
44
* Immutable HashMap implementation using separate chaining.
5-
* <p>
6-
* This HashMap does not allow modification of existing instances. Any update
7-
* operation returns a new ImmutableHashMap.
8-
* </p>
5+
*
6+
* <p>This HashMap does not allow modification of existing instances.
7+
* Any update operation returns a new ImmutableHashMap.
98
*
109
* @param <K> key type
1110
* @param <V> value type
@@ -32,9 +31,10 @@ private ImmutableHashMap(Node<K, V>[] table, int size) {
3231
* @param <V> value type
3332
* @return empty ImmutableHashMap
3433
*/
35-
@SuppressWarnings("unchecked")
34+
@SuppressWarnings({"unchecked", "rawtypes"})
3635
public static <K, V> ImmutableHashMap<K, V> empty() {
37-
return new ImmutableHashMap<>(new Node[DEFAULT_CAPACITY], 0);
36+
Node<K, V>[] table = (Node<K, V>[]) new Node[DEFAULT_CAPACITY];
37+
return new ImmutableHashMap<>(table, 0);
3838
}
3939

4040
/**
@@ -95,9 +95,9 @@ public int size() {
9595
* Computes hash index for a given key.
9696
*/
9797
private int hash(K key) {
98-
return (key == null
98+
return key == null
9999
? 0
100-
: (key.hashCode() & Integer.MAX_VALUE) % table.length);
100+
: (key.hashCode() & Integer.MAX_VALUE) % table.length;
101101
}
102102

103103
/**

0 commit comments

Comments
 (0)