简体   繁体   English

遍历自定义哈希表

[英]Iterating over a custom hashtable

I have a custom hashtable implementation in java. 我在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. 在我的add方法中,我想检查存储该项目的位置是否空闲,如果没有,应该转到下一个索引。 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 我在contains方法中有同样的问题

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. 所有这些都有不同的权衡,您应该告知自己以做出明智的决定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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