简体   繁体   中英

Iterating over a custom hashtable

I have a custom hashtable implementation in java.

public class HashSet<T> implements HashTableInterface<T> {

private static int DEFAULT_ARRAY_SIZE = 10;

private T[] items;

public HashSet() {
    final T[] items = (T[]) new Object[DEFAULT_ARRAY_SIZE];
    this.items = items;
}

@Override
public boolean add(T item) {
    int index = getIndex(item);
    do {
        if (items[index] != null) {
            index = (index + 1) % DEFAULT_ARRAY_SIZE;
        } else {
            items[index] = item;
            break;
        }
    } while (index != getIndex(item));

    return true;
}

@Override
public boolean remove(T item) {
    if (contains(item)) {
        items[getIndex(item)] = null;
        return true;
    } else {
        return false;
    }
}

@Override
public boolean contains(T item) {
    T itemArray = items[getIndex(item)];
    if (item.equals(itemArray)) {
        return true;
    } else {
        int index = (getIndex(item) + 1) % DEFAULT_ARRAY_SIZE;
        do {
            if (items[index] != null) {
                if (items[index].equals(item)) {
                    return true;
                } else {
                    index = (index + 1) % DEFAULT_ARRAY_SIZE;
                }
            } else {
                break;
            }
        } while (index != getIndex(item));
    }
    return items[getIndex(item)] != null;
}

@Override
public int getIndex(T item) {
    return item.hashCode() % DEFAULT_ARRAY_SIZE;
}

@Override
public int size() {
    int count = 0;
    for (T item : items) {
        if (item != null) {
            count++;
        }
    }
    return count;
}

@Override
public String toString() {
    return items.toString();
}}

In my add method I want to check if the place where the item would be stored is free, if not it should go to the next index. Until it finds an empty place.

My code works but I think, there could be a better way to do this.

public boolean add(T item) {
    int index = getIndex(item);
    do {
        if (items[index] != null) {
            index = (index + 1) % DEFAULT_ARRAY_SIZE;
        } else {
            items[index] = item;
            break;
        }
    } while (index != getIndex(item));

    return true;
}

I have the same problem in the contains method

public boolean contains(T item) {
    T itemArray = items[getIndex(item)];
    if (item.equals(itemArray)) {
        return true;
    } else {
        int index = (getIndex(item) + 1) % DEFAULT_ARRAY_SIZE;
        do {
            if (items[index] != null) {
                if (items[index].equals(item)) {
                    return true;
                } else {
                    index = (index + 1) % DEFAULT_ARRAY_SIZE;
                }
            } else {
                break;
            }
        } while (index != getIndex(item));
    }
    return items[getIndex(item)] != null;
}

There a many different ways to do collision avoidance, what you did is called "Linear Probing".

There is also (reference here )

Quadratic probing

H + 1 ^ {2},H + 2 ^ {2},H + 3 ^ {2},H + 4 ^ {2},...,H + k ^ {2}

Double hashing

h(i,k)=(h_1(k)+ i \\ cdot h_2(k))mod | T |。

And schemes that use linked lists for colliding values.

All of these have different tradeoffs which you should inform yourself on to make an informed decision.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM