0

I am implementing a basic HashTable data structure in Java without using collection framework and Arrays.

I intend to use individual nodes whose reference I would store in an array.

I am doing it this way:

Define a Node:

class Node {

    private int data;
    private String key;
    private Node next;
    // Other helper methods
}

Create an array which would hold reference to pre-created Node objects:

public class MyHashTable {

    private Node[] nodeArray = new Node[100];

    public MyHashTable() {
        for(int i=0 ; i<100; i++) {
            nodeArray[i] = new Node();
        }
    }

    private int getIndex(String key) {
        long hashCode = key.hashCode();
        return  (int )hashCode%100;
    }

    // Other helper methods...
}

For getting the hash-code, I am using the Java's inbuilt method -> hashCode().

It seems to be working fine, however for the case in which the key is "second", it returns a negative hash code and because of which the programs terminates with an exception.

My question is:

Is there any standard hashing algorithm which I can use for hashing and which is widely used? I am writing for learning purpose.

CuriousMind
  • 8,301
  • 22
  • 65
  • 134
  • 2
    `hashCode()` is widely used. You can use `Math.abs()` to get a positive value from the `hashCode()` if it is returning negative. – Rohan Dec 06 '17 at 19:41
  • @Rohan: Thanks for your response. Is there any other hashing algorithm other than hashCode() which we can use? – CuriousMind Dec 06 '17 at 19:47
  • 1
    The Java `HashMap` and `HashSet` classes use `hashCode()` internally. I don't know of any other standard methods used to create a hash code value. You can always look for some external library that contains one. – Rohan Dec 06 '17 at 19:50
  • 1
    @Rohan `hashCode` isn't an algorithm, it is merely a way to access some implementation of hashing. – Andy Turner Dec 06 '17 at 19:52
  • @AndyTurner: could your please provide the details? – CuriousMind Dec 06 '17 at 20:14
  • @Rohan You shouldn't use Math.abs(int) that way, as it doesn't always return a positive value (see the Javadocs). You can use `abs(hash % x)` but not `abs(hash) % x`. – Thomas Mueller Dec 08 '17 at 09:01
  • Can anyone suggest me which hashing algorithm to use? – CuriousMind Dec 08 '17 at 09:02
  • A good hashing algorithm also depends on the nature of the data you want to hash. That's why you can override `hashCode()`. – Henry Dec 08 '17 at 09:11
  • @Henry : Writing hash function definitely would require mathematical skills, and hence I am asking for a hash function which have been tested and proven. I can't write :( – CuriousMind Dec 08 '17 at 09:13
  • 1
    @CuriousMind what I am saying is there is no one best solution. You want to distribute the values pretty good over the hash range and at the same time you want it to be fast to compute. It is a compromise that also depends on the distribution of the things you feed into it. – Henry Dec 08 '17 at 09:16
  • @Henry : I got it, however do we have any open source hash algorithms which I can use. Thanks for your help – CuriousMind Dec 08 '17 at 09:19
  • `String.hashCode()` should be fine. Check the earlier comments how to deal with negative results. – Henry Dec 08 '17 at 09:21

1 Answers1

1

You could use for example:

@Override
public int hashCode() {
    return 31 * data + ((key == null) ? 0 : key.hashCode());
}

But you also need to implement equals.

Thomas Mueller
  • 48,905
  • 14
  • 116
  • 132