From cfa8c62372b0cf93b6e809eac24c880a0a37aae1 Mon Sep 17 00:00:00 2001 From: Iryna Finenko Date: Sat, 9 Aug 2025 21:56:53 +0300 Subject: [PATCH 1/4] my HashMap --- src/main/java/core/basesyntax/MyHashMap.java | 23 +++---- src/main/java/core/basesyntax/MyMap.java | 70 ++++++++++++++++++-- 2 files changed, 76 insertions(+), 17 deletions(-) diff --git a/src/main/java/core/basesyntax/MyHashMap.java b/src/main/java/core/basesyntax/MyHashMap.java index d9beffedf..93247debd 100644 --- a/src/main/java/core/basesyntax/MyHashMap.java +++ b/src/main/java/core/basesyntax/MyHashMap.java @@ -1,19 +1,16 @@ package core.basesyntax; -public class MyHashMap implements MyMap { +import org.w3c.dom.Node; - @Override - public void put(K key, V value) { +public class MyHashMap { + private static final int DEFAULT_CAPACITY = 16; + private static final float LOAD_FACTOR = 0.75f; - } - - @Override - public V getValue(K key) { - return null; - } + private Node[] table; + private int size; - @Override - public int getSize() { - return 0; + @SuppressWarnings("unchecked") + public MyHashMap() { + table = (Node[]) new Node[DEFAULT_CAPACITY]; + size = 0; } -} diff --git a/src/main/java/core/basesyntax/MyMap.java b/src/main/java/core/basesyntax/MyMap.java index 4fb3d8032..65469c41d 100644 --- a/src/main/java/core/basesyntax/MyMap.java +++ b/src/main/java/core/basesyntax/MyMap.java @@ -1,9 +1,71 @@ package core.basesyntax; -public interface MyMap { - void put(K key, V value); - V getValue(K key); + public void put(K key, V value) { + if ((float) size / table.length >= LOAD_FACTOR) { + resize(); + } + int index = getIndex(key); + Node current = table[index]; - int getSize(); + while (current != null) { + if (Objects.equals(current.key, key)) { + current.value = value; + return; + } + current = current.next; + } + + Node newNode = new Node<>(key, value, table[index]); + table[index] = newNode; + size++; + } + + 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; + } + + public int getSize() { + return size; + } + + private int getIndex(K key) { + return (key == null) ? 0 : Math.abs(key.hashCode() % table.length); + } + + @SuppressWarnings("unchecked") + private void resize() { + Node[] oldTable = table; + table = (Node[]) new Node[oldTable.length * 2]; + size = 0; + + for (Node node : oldTable) { + while (node != null) { + put(node.key, node.value); + node = node.next; + } + } + } + + private static class Node { + K key; + V value; + Node next; + + Node(K key, V value, Node next) { + this.key = key; + this.value = value; + this.next = next; + } + } } From ed3b6585e40e92b48319beb58510211d6117f3ba Mon Sep 17 00:00:00 2001 From: Iryna Finenko Date: Mon, 11 Aug 2025 22:16:57 +0300 Subject: [PATCH 2/4] my HashMap --- src/main/java/core/basesyntax/MyHashMap.java | 9 +++----- src/main/java/core/basesyntax/MyMap.java | 22 +++++++++++++++----- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/main/java/core/basesyntax/MyHashMap.java b/src/main/java/core/basesyntax/MyHashMap.java index 93247debd..4c70c5826 100644 --- a/src/main/java/core/basesyntax/MyHashMap.java +++ b/src/main/java/core/basesyntax/MyHashMap.java @@ -6,11 +6,8 @@ public class MyHashMap { private static final int DEFAULT_CAPACITY = 16; private static final float LOAD_FACTOR = 0.75f; - private Node[] table; - private int size; - - @SuppressWarnings("unchecked") public MyHashMap() { - table = (Node[]) new Node[DEFAULT_CAPACITY]; - size = 0; + Node[] table = new Node[DEFAULT_CAPACITY]; + int size = 0; + } } diff --git a/src/main/java/core/basesyntax/MyMap.java b/src/main/java/core/basesyntax/MyMap.java index 65469c41d..0e85c4b35 100644 --- a/src/main/java/core/basesyntax/MyMap.java +++ b/src/main/java/core/basesyntax/MyMap.java @@ -1,5 +1,18 @@ package core.basesyntax; +import java.util.Objects; +public class MyHashMap { + private static final int DEFAULT_CAPACITY = 16; + private static final float LOAD_FACTOR = 0.75f; + + private Node[] table; + private int size; + + @SuppressWarnings("unchecked") + public MyHashMap() { + table = (Node[]) new Node[DEFAULT_CAPACITY]; + size = 0; + } public void put(K key, V value) { if ((float) size / table.length >= LOAD_FACTOR) { @@ -10,14 +23,13 @@ public void put(K key, V value) { while (current != null) { if (Objects.equals(current.key, key)) { - current.value = value; + current.value = value; // оновлюємо існуюче значення return; } current = current.next; } - Node newNode = new Node<>(key, value, table[index]); - table[index] = newNode; + table[index] = new Node<>(key, value, table[index]); size++; } @@ -40,14 +52,14 @@ public int getSize() { } private int getIndex(K key) { - return (key == null) ? 0 : Math.abs(key.hashCode() % table.length); + return (key == null) ? 0 : (key.hashCode() & 0x7fffffff) % table.length; } @SuppressWarnings("unchecked") private void resize() { Node[] oldTable = table; table = (Node[]) new Node[oldTable.length * 2]; - size = 0; + size = 0; // заново перерахуємо при rehash for (Node node : oldTable) { while (node != null) { From 833f49953f2b942a2028fa7ac64a02772b3ffd0d Mon Sep 17 00:00:00 2001 From: Iryna Finenko Date: Tue, 12 Aug 2025 21:42:58 +0300 Subject: [PATCH 3/4] fixed bugs --- src/main/java/core/basesyntax/MyHashMap.java | 81 +++++++++++++++++-- src/main/java/core/basesyntax/MyMap.java | 85 ++------------------ 2 files changed, 81 insertions(+), 85 deletions(-) diff --git a/src/main/java/core/basesyntax/MyHashMap.java b/src/main/java/core/basesyntax/MyHashMap.java index 4c70c5826..a95041ccd 100644 --- a/src/main/java/core/basesyntax/MyHashMap.java +++ b/src/main/java/core/basesyntax/MyHashMap.java @@ -1,13 +1,84 @@ package core.basesyntax; -import org.w3c.dom.Node; +import java.util.Objects; -public class MyHashMap { +public class MyHashMap implements MyMap { private static final int DEFAULT_CAPACITY = 16; private static final float LOAD_FACTOR = 0.75f; + private Node[] table; + private int size; + + @SuppressWarnings("unchecked") public MyHashMap() { - Node[] table = new Node[DEFAULT_CAPACITY]; - int size = 0; - } + table = (Node[]) new Node[DEFAULT_CAPACITY]; + size = 0; + } + + @Override + public void put(K key, V value) { + if ((float) size / table.length >= LOAD_FACTOR) { + resize(); + } + int index = getIndex(key); + Node current = table[index]; + while (current != null) { + if (Objects.equals(current.key, key)) { + current.value = value; + return; + } + current = current.next; + } + table[index] = new Node<>(key, value, table[index]); + 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 size; + } + + private int getIndex(K key) { + return (key == null) ? 0 : (key.hashCode() & 0x7fffffff) % table.length; + } + + @SuppressWarnings("unchecked") + private void resize() { + Node[] oldTable = table; + table = (Node[]) new Node[oldTable.length * 2]; + size = 0; + for (Node node : oldTable) { + while (node != null) { + put(node.key, node.value); + node = node.next; + } + } + } + + private static class Node { + K key; + V value; + Node next; + + Node(K key, V value, Node next) { + this.key = key; + this.value = value; + this.next = next; + } } +} + + diff --git a/src/main/java/core/basesyntax/MyMap.java b/src/main/java/core/basesyntax/MyMap.java index 0e85c4b35..7fa94d30d 100644 --- a/src/main/java/core/basesyntax/MyMap.java +++ b/src/main/java/core/basesyntax/MyMap.java @@ -1,83 +1,8 @@ package core.basesyntax; -import java.util.Objects; -public class MyHashMap { - private static final int DEFAULT_CAPACITY = 16; - private static final float LOAD_FACTOR = 0.75f; - - private Node[] table; - private int size; - - @SuppressWarnings("unchecked") - public MyHashMap() { - table = (Node[]) new Node[DEFAULT_CAPACITY]; - size = 0; - } - - public void put(K key, V value) { - if ((float) size / table.length >= LOAD_FACTOR) { - resize(); - } - int index = getIndex(key); - Node current = table[index]; - - while (current != null) { - if (Objects.equals(current.key, key)) { - current.value = value; // оновлюємо існуюче значення - return; - } - current = current.next; - } - - table[index] = new Node<>(key, value, table[index]); - size++; - } - - 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; - } - - public int getSize() { - return size; - } - - private int getIndex(K key) { - return (key == null) ? 0 : (key.hashCode() & 0x7fffffff) % table.length; - } - - @SuppressWarnings("unchecked") - private void resize() { - Node[] oldTable = table; - table = (Node[]) new Node[oldTable.length * 2]; - size = 0; // заново перерахуємо при rehash - - for (Node node : oldTable) { - while (node != null) { - put(node.key, node.value); - node = node.next; - } - } - } - - private static class Node { - K key; - V value; - Node next; - - Node(K key, V value, Node next) { - this.key = key; - this.value = value; - this.next = next; - } - } +public interface MyMap { + void put(K key, V value); + V getValue(K key); + int getSize(); } + From ba1740168f28c9182521f02f83163336fbf15458 Mon Sep 17 00:00:00 2001 From: Iryna Finenko Date: Tue, 12 Aug 2025 21:46:00 +0300 Subject: [PATCH 4/4] fixed bugs --- src/main/java/core/basesyntax/MyHashMap.java | 70 +++++++++++++++----- src/main/java/core/basesyntax/MyMap.java | 3 + 2 files changed, 55 insertions(+), 18 deletions(-) diff --git a/src/main/java/core/basesyntax/MyHashMap.java b/src/main/java/core/basesyntax/MyHashMap.java index a95041ccd..71ce18217 100644 --- a/src/main/java/core/basesyntax/MyHashMap.java +++ b/src/main/java/core/basesyntax/MyHashMap.java @@ -1,7 +1,5 @@ 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; @@ -17,31 +15,45 @@ public MyHashMap() { @Override public void put(K key, V value) { - if ((float) size / table.length >= LOAD_FACTOR) { + if (size >= table.length * LOAD_FACTOR) { resize(); } int index = getIndex(key); + Node newNode = new Node<>(key, value, null); + + if (table[index] == null) { + table[index] = newNode; + size++; + return; + } + Node current = table[index]; - while (current != null) { - if (Objects.equals(current.key, key)) { - current.value = value; + while (true) { + if ((current.getKey() == null && key == null) + || (current.getKey() != null && current.getKey().equals(key))) { + current.setValue(value); return; } - current = current.next; + if (current.getNext() == null) { + current.setNext(newNode); + size++; + return; + } + current = current.getNext(); } - table[index] = new Node<>(key, value, table[index]); - 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; + if ((current.getKey() == null && key == null) + || (current.getKey() != null && current.getKey().equals(key))) { + return current.getValue(); } - current = current.next; + current = current.getNext(); } return null; } @@ -52,7 +64,7 @@ public int getSize() { } private int getIndex(K key) { - return (key == null) ? 0 : (key.hashCode() & 0x7fffffff) % table.length; + return (key == null) ? 0 : Math.abs(key.hashCode() % table.length); } @SuppressWarnings("unchecked") @@ -62,23 +74,45 @@ private void resize() { size = 0; for (Node node : oldTable) { while (node != null) { - put(node.key, node.value); - node = node.next; + put(node.getKey(), node.getValue()); + node = node.getNext(); } } } private static class Node { - K key; - V value; - Node next; + private final K key; + private V value; + private Node next; Node(K key, V value, Node next) { this.key = key; this.value = value; this.next = next; } + + public K getKey() { + return key; + } + + public V getValue() { + return value; + } + + public Node getNext() { + return next; + } + + public void setValue(V value) { + this.value = value; + } + + public void setNext(Node next) { + this.next = next; + } } } + + diff --git a/src/main/java/core/basesyntax/MyMap.java b/src/main/java/core/basesyntax/MyMap.java index 7fa94d30d..11d1a06fe 100644 --- a/src/main/java/core/basesyntax/MyMap.java +++ b/src/main/java/core/basesyntax/MyMap.java @@ -2,7 +2,10 @@ public interface MyMap { void put(K key, V value); + V getValue(K key); + int getSize(); } +