This is one way on how to create a working HashMap implementation in Java. This is commonly an interview question. In this tutorial, I show you how to code the put, get and remove operations. pastebin.com/X...
Excellent video. Many Thanks! One question: shouldn’t we use equals() for comparison in this example? If we define the hashTable with then it may need to be adjusted.
in the while in the put fonction we can do this to no ? while (current != null) { if (current.key.equals(key)) { current.value = value; return; } if (current.next == null){ break; } current = current.next; } current.next = entry;
What is this code below purpose in remove method? Entry prev = e; e = e.next; while (e != null) { if (e.getKey() == key) { prev.next = e.next; e.next = null; return e; } prev = e; e = e.next; } I delete this code, and turn out fine without it. I can still remove data on the middle.
A couple of questions: How big is the array in the standard HashMap class provided in Java? I would think that 8 is rather small and you are just using 8 as a simple example. Can the standard array size be changed? Thanks for the video!
docs.oracle.com/javase/8/docs/api/java/util/HashMap.html HashMap() Constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75). you dont have to worry about changing the side, it happens automatically in the class
@@MrKracx All good. Im asking like when you create a LinkedList Node private class I dont explicity put "private class Node {" but I can access the generic in the constructor. I was wondering why this is not the case for the Entry class in a HastMap.
@@mattg9601 hmm if you have a different setup than me and it works you can keep it. its hard to see what your doing without code, if you want you can create a pastebin and paste it there and send me a link . i will look at it