From 93d87aef22bc067782ab762214c2d20ea2ea1ff7 Mon Sep 17 00:00:00 2001 From: 3009594 Date: Sun, 25 Aug 2024 23:39:13 +0200 Subject: [PATCH] remove method --- .../src/Hashmap/MyGenericHashMap.java | 35 +++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/Programmierung2/src/Hashmap/MyGenericHashMap.java b/Programmierung2/src/Hashmap/MyGenericHashMap.java index 9e64713..05b94c1 100644 --- a/Programmierung2/src/Hashmap/MyGenericHashMap.java +++ b/Programmierung2/src/Hashmap/MyGenericHashMap.java @@ -37,10 +37,6 @@ public class MyGenericHashMap { return key; } - public void setKey(K key) { - this.key = key; - } - public V getValue() { return value; } @@ -101,6 +97,33 @@ public class MyGenericHashMap { } } + + public V removeValue(K key) { + + int hash = key.hashCode() % SIZE; + Entry temp = table[hash]; + if (temp == null) + return null; + // speicher das prevs Knote von temp + Entry prevs = null; + while(temp != null) { + V merker = temp.getValue(); + // falls der Key gefunden wäre + if (temp.getKey().equals(key)) { + //falls ja, dann ist meine Knote gleich der Head + // und es gibt keine weiter Knoten + if (prevs == null) + table[hash] = temp.next; + else + prevs.next = temp.next; + return merker; + } + prevs = temp; + temp =temp.next; + } + return null; + + } @@ -112,8 +135,8 @@ public class MyGenericHashMap { // Fügt das key-value Paar (3009594, "obai") zur HashMap hinzu. t1.put(3009594, "obai"); t1.put(3129594, "omar"); - t1.put(3129593, "abd"); - t1.put(3129393, "abd"); + t1.put(3129592, "abd"); + t1.put(3129392, "basel"); t1.printAll(); }