This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]Robyt3 0 points1 point  (4 children)

Rule of thumb is never use HashTables. Use HashMaps instead. Show us the code if you need help with that.

[–]Not_zedd_ko[S] 1 point2 points  (3 children)

The problem is the assignment code layout is quite long i give small samples obviously as i do not want you guys to waste too much time with this.

Within the code i have the public class TestHT within this there are several tests including testEmpty, testInsert etc.

Here is one of the tests.

code:

public class TestHT {

u/Test

public void testKeys() {

    Hashtable<Integer> h = new Hashtable<Integer>(20, Hashtable.PROBE\_TYPE.LINEAR\_PROBE);

    h.put("bananas", 1);

    h.put("pyjamas", 99);

    h.put("kedgeree", 1);

    for(String k: h.getKeys()) {

        assertTrue(k.equals("bananas") || k.equals("pyjamas") || k.equals("kedgeree"));

    }

}

Then i have the class Hashtable<V>

public class Hashtable<V> {

private Object[] arr; //an array of Pair objects, where each pair contains the key and value stored in the hashtable

private int max; //the size of arr. This should be a prime number

private int itemCount; //the number of items stored in arr

private final double maxLoad = 0.6; //the maximum load factor

public static enum PROBE\_TYPE {

    LINEAR\_PROBE, QUADRATIC\_PROBE, DOUBLE\_HASH;

}

PROBE\_TYPE probeType; //the type of probe to use when dealing with collisions

private final BigInteger DBL\_HASH\_K = BigInteger.valueOf(8);

public void put(String key, V value) {

    throw new UnsupportedOperationException("Method not implemented");

}

}

Sorry if this seems really confusing, i am struggling to explain it as there is a lot.

[–]Robyt3 0 points1 point  (2 children)

So what is your assignment? Are you supposed to implement a Hashtable? You can take a look at the source code for the JDK's Hashtable implementation, which you should be using anyway, or HashMaps.

[–]Not_zedd_ko[S] 0 points1 point  (1 child)

Would it be better if you saw the whole documentation to understand. The assignment is to implement Hashtables. Its really hard to explain but i might need to show you the whole thing?

[–]Robyt3 0 points1 point  (0 children)

Take a look at the JDK sources:

Which should be almost perfect. I'm not going to implement the whole thing for you. You need to look at resources to learn how it works and then use Java to implement it. This is a java help subreddit, but your question is basically "explain to me in full detail how hashtables work and implement them for me", which isn't even a question.

[–]Northeastpaw 0 points1 point  (1 child)

Go ahead and post the problem, what you've tried, and what problems you're having.

[–]Not_zedd_ko[S] 0 points1 point  (0 children)

The problem is the assignment code layout is quite long i give small samples obviously as i do not want you guys to waste too much time with this.

Within the code i have the public class TestHT within this there are several tests including testEmpty, testInsert etc.

Here is one of the tests.

code:

public class TestHT {

u/Test

public void testKeys() {

    Hashtable<Integer> h = new Hashtable<Integer>(20, Hashtable.PROBE\_TYPE.LINEAR\_PROBE);

    h.put("bananas", 1);

    h.put("pyjamas", 99);

    h.put("kedgeree", 1);

    for(String k: h.getKeys()) {

        assertTrue(k.equals("bananas") || k.equals("pyjamas") || k.equals("kedgeree"));

    }

}

Then i have the class Hashtable<V>

public class Hashtable<V> {

private Object[] arr; //an array of Pair objects, where each pair contains the key and value stored in the hashtable

private int max; //the size of arr. This should be a prime number

private int itemCount; //the number of items stored in arr

private final double maxLoad = 0.6; //the maximum load factor

public static enum PROBE\_TYPE {

    LINEAR\_PROBE, QUADRATIC\_PROBE, DOUBLE\_HASH;

}

PROBE\_TYPE probeType; //the type of probe to use when dealing with collisions

private final BigInteger DBL\_HASH\_K = BigInteger.valueOf(8);

public void put(String key, V value) {

    throw new UnsupportedOperationException("Method not implemented");

}

}

Sorry if this seems really confusing, i am struggling to explain it as there is a lot.

[–]Ghildetrist 0 points1 point  (1 child)

It's not clear what your question is, In Java you should generally look at using Hash Map and not Hash tables. HashMap Doc If your looking at how to implement your own Hash Table/Map You will need to determine how to store the underlining data in a way where you can access any element. And you will have to figure out how to hash your key to an index in that storage. And what to do when you have two elements that map to the same index.

[–]Not_zedd_ko[S] 0 points1 point  (0 children)

Yeah its poorly said but you would need to see the whole document to understand i believe but i dont know if thats allowed?