0

I'm trying to implement a hashtable using chaining and without any libraries (other than those already in my code), but I'm very stuck. For some reason, the data (100 lines of ints) isn't being added to the list, as seen when it's printed, except for one in the second position (which I assume I need a toString() method for.) Is there any tips or solutions I could get as to how to make this work?

Thanks in advance!

Main + array declaration:

static LinkedList<Node> hashTable[] = new LinkedList[100];

static class Node {

    int value;
    int key;
}

public static void main(String[] args) throws FileNotFoundException {

    File f = new File("Ex5.txt");

    Scanner scan = new Scanner(f);

    if (f.exists() == false) {
        System.out.println("File doesn't exist or could not be found.");
        System.exit(0);
    }

    while (scan.hasNextInt()) {
        int n = scan.nextInt();
        insert(1, hashFunction(n));
    }

    for (int i = 0; i < 100; ++i) {
        System.out.println(hashTable[i]);
    }
}

Insert Function:

public static void insert(int key, int value) {
    int index = key % 100;
    LinkedList<Node> items = hashTable[index];

    if (items == null) {
        items = new LinkedList<>();

        Node item = new Node();
        item.key = key;
        item.value = value;

        items.add(item);

        hashTable[index] = items;
    } else {
        for (Node item : items) {
            if (item.key == key) {
                item.value = value;
                return;
            }
        }

        Node item = new Node();
        item.key = key;
        item.value = value;

        items.add(item);
    }
}

Hashing function:

public static int hashFunction(int value) {
    int hashKey = value % 100;
    return hashKey;
}
4
  • You are passing the same key (1) to all insert() calls. You probably meant to pass different keys. Commented Sep 18, 2019 at 9:33
  • Sorry I forgot to say, I'm not sure what to use there for it to hash all of the values. Commented Sep 18, 2019 at 9:45
  • you are hashing the keys not the values, and a key can appear only once in the map, so adding the same key multiple times will override the value. In the end you'll have just one key and one value in the map Commented Sep 18, 2019 at 9:48
  • So if the ints being read aren't the values, what are? Commented Sep 18, 2019 at 9:50

1 Answer 1

1

You shall make two changes:

  1. You shall use hashfunction to get the key and keep value as is.

  2. Remove index=key%100 from insert, instead use passed key for traversing.

Hope this helps.

-------------------------- EDIT --------------------

For printing actual values in linked list, please override toString() method in your Node Class and return a string representation of Key-Value.

Sign up to request clarification or add additional context in comments.

1 Comment

I managed to get it working (I think), but how could I implement a toString() method for a linkedlist? The output when I try to print each line of the hashTable is like this (except they're on separate lines): null null [Ex5$Node@38af3868, Ex5$Node@77459877] null [Ex5$Node@5b2133b1] [Ex5$Node@72ea2f77, Ex5$Node@33c7353a, Ex5$Node@681a9515] [Ex5$Node@3af49f1c]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.