From e9680d110003a2d95ccaec01e4c678d3b659496b Mon Sep 17 00:00:00 2001 From: Brynkiewicz Date: Wed, 8 Apr 2026 22:38:25 +0200 Subject: [PATCH 1/6] first solution --- src/main/java/core/basesyntax/MyHashMap.java | 94 +++++++++++++++++++- 1 file changed, 92 insertions(+), 2 deletions(-) diff --git a/src/main/java/core/basesyntax/MyHashMap.java b/src/main/java/core/basesyntax/MyHashMap.java index d9beffedf..ddf13d9d1 100644 --- a/src/main/java/core/basesyntax/MyHashMap.java +++ b/src/main/java/core/basesyntax/MyHashMap.java @@ -1,19 +1,109 @@ package core.basesyntax; +import java.util.Map; +import java.util.Objects; + public class MyHashMap implements MyMap { + private static final int DEFAULT_INITIAL_CAPACITY = 16; + private static final float DEFAULT_LOAD_FACTOR = 0.75f; + static class Node implements Map.Entry { + private final int hash; + private final K key; + private V value; + private Node next; + Node(int hash, K key, V value, Node next) { + this.hash = hash; + this.key = key; + this.value = value; + this.next = next; + } + + @Override + public K getKey() { + return key; + } + + @Override + public V getValue() { + return value; + } + + @Override + public V setValue(V value) { + V oldValue = this.value; + this.value = value; + return oldValue; + } + } + + private Node[] table; + private int size; + private int threshold; + public MyHashMap() { + table = new Node[DEFAULT_INITIAL_CAPACITY]; + threshold = (int) (DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR); + } + + private int hash(K key) { + return key == null ? 0 : key.hashCode(); + } @Override public void put(K key, V value) { + if (size >= threshold) { + resize(); + } + int hash = hash(key); + int index = (hash & 0x7fffffff) % table.length; + Node current = table[index]; + while (current != null) { + // Porównujemy hash oraz klucze (Objects.equals radzi sobie z null) + if (current.hash == hash && Objects.equals(current.key, key)) { + current.value = value; + return; + } + current = current.next; + } + Node newNode = new Node<>(hash, key, value, table[index]); + table[index] = newNode; + size++; } @Override public V getValue(K key) { + int hash = hash(key); + int index = (hash & 0x7fffffff) % table.length; + Node current = table[index]; + while (current != null) { + if (current.hash == hash && Objects.equals(current.key, key)) { + return current.value; + } + current = current.next; + } return null; } - @Override public int getSize() { - return 0; + return size; + } + + private void resize() { + int newCapacity = table.length * 2; + Node[] newTable = new Node[newCapacity]; + for (Node node : table) { + while (node != null) { + Node next = node.next; + int index = (node.hash & 0x7fffffff) % newCapacity; + node.next = newTable[index]; + newTable[index] = node; + node = next; + } + } + table = newTable; + threshold = (int) (newCapacity * DEFAULT_LOAD_FACTOR); } } + + + From 607cd32a3bed1cc93a29fb51e5845183c6f0d4d1 Mon Sep 17 00:00:00 2001 From: Brynkiewicz Date: Wed, 8 Apr 2026 22:42:02 +0200 Subject: [PATCH 2/6] checkstyle fixes --- src/main/java/core/basesyntax/MyHashMap.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/core/basesyntax/MyHashMap.java b/src/main/java/core/basesyntax/MyHashMap.java index ddf13d9d1..17022f112 100644 --- a/src/main/java/core/basesyntax/MyHashMap.java +++ b/src/main/java/core/basesyntax/MyHashMap.java @@ -6,11 +6,13 @@ public class MyHashMap implements MyMap { private static final int DEFAULT_INITIAL_CAPACITY = 16; private static final float DEFAULT_LOAD_FACTOR = 0.75f; + static class Node implements Map.Entry { private final int hash; private final K key; private V value; private Node next; + Node(int hash, K key, V value, Node next) { this.hash = hash; this.key = key; @@ -39,6 +41,7 @@ public V setValue(V value) { private Node[] table; private int size; private int threshold; + public MyHashMap() { table = new Node[DEFAULT_INITIAL_CAPACITY]; threshold = (int) (DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR); @@ -83,6 +86,7 @@ public V getValue(K key) { } return null; } + @Override public int getSize() { return size; From f03378a11278af39a7b5a6a0ce62e13ca13979da Mon Sep 17 00:00:00 2001 From: Brynkiewicz Date: Wed, 8 Apr 2026 23:00:55 +0200 Subject: [PATCH 3/6] after fixes --- src/main/java/core/basesyntax/MyHashMap.java | 28 ++++---------------- 1 file changed, 5 insertions(+), 23 deletions(-) diff --git a/src/main/java/core/basesyntax/MyHashMap.java b/src/main/java/core/basesyntax/MyHashMap.java index 17022f112..ba42e4530 100644 --- a/src/main/java/core/basesyntax/MyHashMap.java +++ b/src/main/java/core/basesyntax/MyHashMap.java @@ -7,35 +7,18 @@ public class MyHashMap implements MyMap { private static final int DEFAULT_INITIAL_CAPACITY = 16; private static final float DEFAULT_LOAD_FACTOR = 0.75f; - static class Node implements Map.Entry { + private class Node { private final int hash; private final K key; private V value; private Node next; - Node(int hash, K key, V value, Node next) { + public Node(int hash, K key, V value, Node next) { this.hash = hash; this.key = key; this.value = value; this.next = next; } - - @Override - public K getKey() { - return key; - } - - @Override - public V getValue() { - return value; - } - - @Override - public V setValue(V value) { - V oldValue = this.value; - this.value = value; - return oldValue; - } } private Node[] table; @@ -57,10 +40,9 @@ public void put(K key, V value) { resize(); } int hash = hash(key); - int index = (hash & 0x7fffffff) % table.length; + int index = (hash & Integer.MAX_VALUE) % table.length; Node current = table[index]; while (current != null) { - // Porównujemy hash oraz klucze (Objects.equals radzi sobie z null) if (current.hash == hash && Objects.equals(current.key, key)) { current.value = value; return; @@ -76,7 +58,7 @@ public void put(K key, V value) { @Override public V getValue(K key) { int hash = hash(key); - int index = (hash & 0x7fffffff) % table.length; + int index = (hash & Integer.MAX_VALUE) % table.length; Node current = table[index]; while (current != null) { if (current.hash == hash && Objects.equals(current.key, key)) { @@ -98,7 +80,7 @@ private void resize() { for (Node node : table) { while (node != null) { Node next = node.next; - int index = (node.hash & 0x7fffffff) % newCapacity; + int index = (node.hash & Integer.MAX_VALUE) % newCapacity; node.next = newTable[index]; newTable[index] = node; node = next; From f43dfe4a0113fa6ede07d0eff9f0b0437a608f99 Mon Sep 17 00:00:00 2001 From: Brynkiewicz Date: Wed, 8 Apr 2026 23:02:02 +0200 Subject: [PATCH 4/6] after fixes2 --- src/main/java/core/basesyntax/MyHashMap.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/core/basesyntax/MyHashMap.java b/src/main/java/core/basesyntax/MyHashMap.java index ba42e4530..aa1a486e8 100644 --- a/src/main/java/core/basesyntax/MyHashMap.java +++ b/src/main/java/core/basesyntax/MyHashMap.java @@ -1,6 +1,5 @@ package core.basesyntax; -import java.util.Map; import java.util.Objects; public class MyHashMap implements MyMap { From 5e508f82bf79bb2b58f2ddcb621ad344ec8f1df4 Mon Sep 17 00:00:00 2001 From: Brynkiewicz Date: Wed, 8 Apr 2026 23:10:24 +0200 Subject: [PATCH 5/6] after fixes3 --- src/main/java/core/basesyntax/MyHashMap.java | 50 ++++++++++---------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/src/main/java/core/basesyntax/MyHashMap.java b/src/main/java/core/basesyntax/MyHashMap.java index aa1a486e8..e25f18aba 100644 --- a/src/main/java/core/basesyntax/MyHashMap.java +++ b/src/main/java/core/basesyntax/MyHashMap.java @@ -5,41 +5,20 @@ public class MyHashMap implements MyMap { private static final int DEFAULT_INITIAL_CAPACITY = 16; private static final float DEFAULT_LOAD_FACTOR = 0.75f; - - private class Node { - private final int hash; - private final K key; - private V value; - private Node next; - - public Node(int hash, K key, V value, Node next) { - this.hash = hash; - this.key = key; - this.value = value; - this.next = next; - } - } - - private Node[] table; - private int size; - private int threshold; + private static final int HASH_MASK = Integer.MAX_VALUE; public MyHashMap() { table = new Node[DEFAULT_INITIAL_CAPACITY]; threshold = (int) (DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR); } - private int hash(K key) { - return key == null ? 0 : key.hashCode(); - } - @Override public void put(K key, V value) { if (size >= threshold) { resize(); } int hash = hash(key); - int index = (hash & Integer.MAX_VALUE) % table.length; + int index = (hash & HASH_MASK) % table.length; Node current = table[index]; while (current != null) { if (current.hash == hash && Objects.equals(current.key, key)) { @@ -57,7 +36,7 @@ public void put(K key, V value) { @Override public V getValue(K key) { int hash = hash(key); - int index = (hash & Integer.MAX_VALUE) % table.length; + int index = (hash & HASH_MASK) % table.length; Node current = table[index]; while (current != null) { if (current.hash == hash && Objects.equals(current.key, key)) { @@ -79,7 +58,7 @@ private void resize() { for (Node node : table) { while (node != null) { Node next = node.next; - int index = (node.hash & Integer.MAX_VALUE) % newCapacity; + int index = (node.hash & HASH_MASK) % newCapacity; node.next = newTable[index]; newTable[index] = node; node = next; @@ -88,7 +67,26 @@ private void resize() { table = newTable; threshold = (int) (newCapacity * DEFAULT_LOAD_FACTOR); } -} + private int hash(K key) { + return key == null ? 0 : key.hashCode(); + } + private class Node { + private final int hash; + private final K key; + private V value; + private Node next; + public Node(int hash, K key, V value, Node next) { + this.hash = hash; + this.key = key; + this.value = value; + this.next = next; + } + } + + private Node[] table; + private int size; + private int threshold; +} From e0b7101e6fb55f0bfc6e5da7af70ca665eba1484 Mon Sep 17 00:00:00 2001 From: Michal Brynkiewicz Date: Tue, 2 Jun 2026 19:04:27 +0200 Subject: [PATCH 6/6] added missing test components --- config/checkstyle.xml | 250 +++++++++++++++++++ pom.xml | 2 +- src/main/java/core/basesyntax/MyHashMap.java | 31 ++- 3 files changed, 278 insertions(+), 5 deletions(-) create mode 100644 config/checkstyle.xml diff --git a/config/checkstyle.xml b/config/checkstyle.xml new file mode 100644 index 000000000..ea0a5295c --- /dev/null +++ b/config/checkstyle.xml @@ -0,0 +1,250 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pom.xml b/pom.xml index ae43693ff..bd8385ffa 100644 --- a/pom.xml +++ b/pom.xml @@ -40,7 +40,7 @@ - ${maven.checkstyle.plugin.configLocation} + config/checkstyle.xml true true false diff --git a/src/main/java/core/basesyntax/MyHashMap.java b/src/main/java/core/basesyntax/MyHashMap.java index e25f18aba..0563a86de 100644 --- a/src/main/java/core/basesyntax/MyHashMap.java +++ b/src/main/java/core/basesyntax/MyHashMap.java @@ -1,5 +1,6 @@ package core.basesyntax; +import java.util.Arrays; import java.util.Objects; public class MyHashMap implements MyMap { @@ -7,11 +8,37 @@ public class MyHashMap implements MyMap { private static final float DEFAULT_LOAD_FACTOR = 0.75f; private static final int HASH_MASK = Integer.MAX_VALUE; + private Node[] table; + private int size; + private int threshold; + public MyHashMap() { table = new Node[DEFAULT_INITIAL_CAPACITY]; threshold = (int) (DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR); } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + + if (!(o instanceof MyHashMap myHashMap)) { + return false; + } + + return getSize() == myHashMap.getSize() + && threshold == myHashMap.threshold + && Arrays.equals(table, myHashMap.table); + } + + @Override + public int hashCode() { + int result = Objects.hash(getSize(), threshold); + result = 31 * result + Arrays.hashCode(table); + return result; + } + @Override public void put(K key, V value) { if (size >= threshold) { @@ -85,8 +112,4 @@ public Node(int hash, K key, V value, Node next) { this.next = next; } } - - private Node[] table; - private int size; - private int threshold; }