Reimplementation of a Hashtable and hash function package com.raja.util;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class Hashtable {
    private Object[] slots;
    private int capacity;

    Hashtable(int size) {
        capacity = size;
        slots = new String[size];
        Arrays.fill(slots, "-1");
    }

    // Simple hash function that puts values in the randomly generated index that generated within the limits of the given Hashtable size
    public void myHashFunction(Object[] elements, Object[] slots) {
        Random random = new Random();
        for (int n = 0; n &lt; elements.length; n++) {
            int randomIndex = randomIndex(0, capacity, random);
            if(randomIndex &lt; capacity){
                while((String)slots[randomIndex] != "-1" &amp;&amp; randomIndex != capacity){
                    randomIndex = randomIndex(0, capacity, random);
                }
                slots[randomIndex] = elements[n];
            }
        }
    }

    //Generates a random number in the limits of begin and end
    private static int randomIndex(int begin, int end, Random aRandom){
        if (begin &gt; end) {
          throw new IllegalArgumentException("Start cannot exceed End.");
        }
        long range = (long)end - (long)begin + 1;
        long fraction = (long)(range * aRandom.nextDouble());
        int randomNumber =  (int)(fraction + begin);    
        if(randomNumber == end)
            randomNumber--;
        return randomNumber;
      }

    // Returns the value stored in the Hash slots
    public Object getValue(int key) {
        if(getKeys().contains(key)){
            return slots[key];
        } else return null;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("{ ");
        for(int i = 0; i &lt; slots.length; i++){
            if(slots[i] != "-1")
                sb.append(i+"-&gt;" + slots[i] + ", ");
        }
        sb.append("}");
        int lastIndex = sb.lastIndexOf(",");
        sb.replace(lastIndex, lastIndex + 1, "");
        return sb.toString();
    }

    // Returns the key indexes where the elements got stored.
    public Set&lt;Integer&gt; getKeys(){
        Set&lt;Integer&gt; keys = new HashSet&lt;Integer&gt;();
        if(slots != null){
            for(int index = 0; index &lt; slots.length; index++){
                if(slots[index] != "-1"){
                    keys.add(index);
                }
            }
        }
        return keys;
    }

    //Testing : Storing A-Z alphabets in Hashtable
    public static void main(String[] args) {
        Object[] elements = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
        Hashtable myHashtable = new Hashtable(1000);  //Big size (i.e. 1000) is used to illustrate the random hash keys
        myHashtable.myHashFunction(elements, myHashtable.slots);
        System.out.println("Keys = "+ myHashtable.getKeys());
        if(myHashtable.getKeys().size() != elements.length) { 
            System.out.println("Oops!, some element(s) were not stored in your hashtable");
        }
        System.out.println("Your Hashtable = " + myHashtable);
    }
}


The output of this program would be something like:

Keys = [611, 954, 199, 406, 892, 25, 214, 633, 265, 391, 759, 102, 307, 236, 990, 36, 432, 918, 433, 40, 435, 522, 591, 775, 250, 726]
Your Hashtable = { 25-&gt;O, 36-&gt;S, 40-&gt;I, 102-&gt;H, 199-&gt;D, 214-&gt;N, 236-&gt;X, 250-&gt;Y, 265-&gt;K, 307-&gt;B, 391-&gt;F, 406-&gt;A, 432-&gt;M, 433-&gt;Q, 435-&gt;C, 522-&gt;V, 591-&gt;U, 611-&gt;G, 633-&gt;P, 726-&gt;J, 759-&gt;W, 775-&gt;R, 892-&gt;Z, 918-&gt;E, 954-&gt;T, 990-&gt;L }
Your container isn't what most people would call a hash table.  It doesn't use a deterministic hash function of its contents, and it doesn't give the expected performance.  That said, here are some other comments on your "container".

myHashtable.myHashFunction(elements, myHashtable.slots);


This is a strange name for the function that adds elements to your container.

private Object[] slots;
slots = new String[size];
public void myHashFunction(Object[] elements, Object[] slots)


Your class acts like it can work on any Object, but it will fail if given anything other than a String.

Arrays.fill(slots, "-1")


This is a strange string to use as your empty sentinel.  What happens if someone wants to add "-1" to your container?  What's wrong with null as an empty marker?