Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9323339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T04:19:09+00:00 2026-06-19T04:19:09+00:00

So I have a HashTable implementation here that I wrote using only Arrays and

  • 0

So I have a HashTable implementation here that I wrote using only Arrays and had a little bit of help with the code. Unfortunately, I don’t quite understand one of the lines someone added while running the “get” or “put” method. What exactly is happening in the while loop below? It is a method for linear probing correct? Also why is the loop checking the conditions it’s checking?

Specifically,

int hash = hashThis(key);

    while(data[hash] != AVAILABLE && data[hash].key() != key) {

        hash = (hash + 1) % capacity;
    }

Here’s the whole Java class below for full reference.

public class Hashtable2 {

private Node[] data;
private int capacity;
private static final Node AVAILABLE = new Node("Available", null);

public Hashtable2(int capacity) {

    this.capacity = capacity; 
    data = new Node[capacity];
    for(int i = 0; i < data.length; i++) {

        data[i] = AVAILABLE;
    }
}

public int hashThis(String key) {

    return key.hashCode() % capacity; 
}

public Object get(String key) {

    int hash = hashThis(key);

    while(data[hash] != AVAILABLE && data[hash].key() != key) {

        hash = (hash + 1) % capacity;
    }
    return data[hash].element();
}

public void put(String key, Object element) {

    if(key != null) {
        int hash = hashThis(key);
        while(data[hash] != AVAILABLE && data[hash].key() != key) {

            hash = (hash + 1) % capacity;
        }

        data[hash] = new Node(key, element);

    }

}



public String toString(){

    String s="<";

    for (int i=0;i<this.capacity;i++)
    {
        s+=data[i]+", ";    

    }

    s+=">";

    return s;
    }

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T04:19:11+00:00Added an answer on June 19, 2026 at 4:19 am

    I just rewrote some part of the code and added the findHash-method – try to avoid code-duplication!

    private int findHash(String key) {
        int hash = hashThis(key);
    
        // search for the next available element or for the next matching key
        while(data[hash] != AVAILABLE && data[hash].key() != key) {
    
            hash = (hash + 1) % capacity;
        }
        return hash;
    }
    
    public Object get(String key) {
    
        return data[findHash(key)].element();
    }
    
    public void put(String key, Object element) {
    
        data[findHash(key)] = new Node(key, element); 
    }
    

    What you asked for is – what exactly does this findHash-loop? The data was initialized with AVAILABLE – meaning: the data does not (yet) contain any actual data. Now – when we add an element with put – first a hashValue is calculated, that is just an index in the data array where to put the data. Now – if we encounter that the position has already been taken by another element with the same hash value but a different key, we try to find the next AVAILABLE position. And the get method essentially works the same – if a data element with a different key is detected, the next element is probed and so on.
    The data itself is a so called ring-buffer. That is, it is searched until the end of the array and is next search again at the beginning, starting with index 0. This is done with the modulo % operator.

    Alright?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code which populates a hashtable with a question as the key
I have a list of string values that I want add to a hashtable
I have one key but different values for that key. I tried HashTable but
I'm looking for a hashtable implementation in C that stores its objects in (twodimensional)
I have developed an array based implementation of a hashTable with several stock names,
I need the internal implementation of HashTable in java code. Further can you explain
I am working on a BlackBerry j2me Java implementation that does not have the
I am using SQL Server 2008 and have a hashtable which contains data about
I have to rewrite windows-code into crossplatform view. Here is the example: std::unordered_set<Type>::iterator it
I have a hashtable . values() method returns values in some order different from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.