From 70d69d9beb07c787bbdf5f6bbb0b21f7af06274f Mon Sep 17 00:00:00 2001 From: Andrii Tkachuk Date: Sun, 24 May 2026 20:13:47 +0200 Subject: [PATCH 1/3] solution --- src/main/java/core/basesyntax/MyHashMap.java | 82 +++++++++++++++++++- 1 file changed, 80 insertions(+), 2 deletions(-) diff --git a/src/main/java/core/basesyntax/MyHashMap.java b/src/main/java/core/basesyntax/MyHashMap.java index d9beffedf..a4e092cd9 100644 --- a/src/main/java/core/basesyntax/MyHashMap.java +++ b/src/main/java/core/basesyntax/MyHashMap.java @@ -1,19 +1,97 @@ package core.basesyntax; +import java.util.Objects; + public class MyHashMap implements MyMap { + private static final int DEFAULT_CAPACITY = 16; + private static final float LOAD_FACTOR = 0.75f; + private static final int GROWTH_FACTOR = 2; + + private Node[] table; + private int size; + + public MyHashMap() { + table = new Node[DEFAULT_CAPACITY]; + } @Override public void put(K key, V value) { + if (size >= table.length * LOAD_FACTOR) { + resize(); + } + + int index = getIndex(key); + Node current = table[index]; + if (current == null) { + table[index] = new Node<>(key, value); + size++; + return; + } + + while (true) { + if (Objects.equals(current.key, key)) { + current.value = value; + return; + } + + if (current.next == null) { + break; + } + + current = current.next; + } + + current.next = new Node<>(key, value); + size++; } @Override public V getValue(K key) { + int index = getIndex(key); + Node current = table[index]; + + while (current != null) { + if (Objects.equals(current.key, key)) { + return current.value; + } + + current = current.next; + } + return null; } @Override public int getSize() { - return 0; + return size; + } + + private int getIndex(K key) { + return key == null ? 0 : Math.abs(key.hashCode() % table.length); + } + + private void resize() { + Node[] oldTable = table; + table = new Node[oldTable.length * GROWTH_FACTOR]; + size = 0; + + for (Node node : oldTable) { + while (node != null) { + put(node.key, node.value); + node = node.next; + } + } + } + + private static class Node { + private K key; + private V value; + private Node next; + + private Node(K key, V value) { + this.key = key; + this.value = value; + } } -} +} \ No newline at end of file From 06ac9ba7e64b0d717ee2f9a72914849999e273ea Mon Sep 17 00:00:00 2001 From: Andrii Tkachuk Date: Sun, 24 May 2026 20:18:35 +0200 Subject: [PATCH 2/3] solution2 --- src/main/java/core/basesyntax/MyHashMap.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/MyHashMap.java b/src/main/java/core/basesyntax/MyHashMap.java index a4e092cd9..da7a5a26c 100644 --- a/src/main/java/core/basesyntax/MyHashMap.java +++ b/src/main/java/core/basesyntax/MyHashMap.java @@ -88,7 +88,7 @@ private static class Node { private K key; private V value; private Node next; - + private Node(K key, V value) { this.key = key; this.value = value; From f597cfb08fa52b47f9b2b3f204dfdf7af7f66b63 Mon Sep 17 00:00:00 2001 From: Andrii Tkachuk Date: Sun, 24 May 2026 20:24:31 +0200 Subject: [PATCH 3/3] solution3 --- src/main/java/core/basesyntax/MyHashMap.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/core/basesyntax/MyHashMap.java b/src/main/java/core/basesyntax/MyHashMap.java index da7a5a26c..293b15b85 100644 --- a/src/main/java/core/basesyntax/MyHashMap.java +++ b/src/main/java/core/basesyntax/MyHashMap.java @@ -88,10 +88,10 @@ private static class Node { private K key; private V value; private Node next; - + private Node(K key, V value) { this.key = key; this.value = value; } } -} \ No newline at end of file +}