Skip to content

Refactoring part in adding process in Separate chaning feature #47

Open
@Qorxi

Description

https://github.com/williamfiset/data-structures/blob/b63739f38cbc405d5d2ebcbae2ce02f37bde35fe/com/williamfiset/datastructures/hashtable/HashTableSeparateChaining.java#L152

I think improvement is needed in the bucketInsertEntry () method, because by going through this situation according to the bucket value being null in 152 rows, the same condition may appear as return null in the bucketSeekEntry method, accordingly we understand that there is no addition in that index according to the bucketIndex-given in the condition of correctness of the condition in 152 rows. Therefore, I think there is no need for additional checking, so it should not be logged into the bucketSeekEntry () method, we can do such a refactoring by considering these (I sampled the change over your codes):

private V bucketInsertEntry (int bucketIndex, Entry <K, V> entry) {

    LinkedList <Entry <K, V >> bucket = table [bucketIndex];

    if (bucket == null) {
        table [bucketIndex] = bucket = new LinkedList<>();
        bucket.add (entry);
        if (++ size> threshold) resizeTable ();
        return null; // Use null to indicate that there was no previous entry
    }

    Entry <K, V> existentEntry = bucketSeekEntry (bucketIndex, entry.key);
    if (!existentEntry == null) {
      V oldVal = existentEntry.value;
      existentEntry.value = entry.value;
      return oldVal;
    }
     
    return null;
  } 

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions