Как удвоить хеширование с несколькими значениями Java - PullRequest
0 голосов
/ 23 октября 2018

Я ищу способ создания двойного хеширования с несколькими значениями.Я смог создать следующее двойное хеширование.Теперь я хочу сделать это с несколькими значениями.Например, что-то вроде имени и фамилии, а затем возможность извлекать значения, которые переданы в метод get или find

public class HashTableDoubleHashing {

String[] hashArray;
int arraySize;
int size = 0;

public HashTableDoubleHashing(int numTotaal) {

    if (isPrime(numTotaal)) {
        hashArray = new String[numTotaal];
        arraySize = numTotaal;
    } else {
        int primeCount = getNextPrime(numTotaal);
        hashArray = new String[primeCount];
        arraySize = primeCount;
       // System.out.println("Hash table size given " + numTotaal + " in not a prime.");
      //  System.out.println("Hash table size is changed to: " + primeCount);
    }
}

private boolean isPrime(int num) {
    for (int i = 2; i * i <= num; i++) {
        if (num % i == 0) {
            return false;
        }
    }
    return true;
}

private int getNextPrime(int minNumber) {
    for (int i = minNumber; true; i++) {
        if (isPrime(i)) {
            return i;
        }
    }
}

private int hashFunction1(String word) {
    int hashVal = word.hashCode();
    hashVal = hashVal % arraySize;
    if (hashVal < 0) {
        hashVal += arraySize;
    }
    return hashVal;
}

private int hasFuntion2(String word) {
    int hashVal = word.hashCode();
    hashVal = hashVal % arraySize;
    if (hashVal < 0) {
        hashVal += arraySize;
    }

    return 3 - hashVal % 3; 
}

public void insert(String word) {
    int hashValue = hashFunction1(word);
    int stepSize = hasFuntion2(word);

    while (hashArray[hashValue] != null) {
        hashValue = hashValue + stepSize;
        hashValue = hashValue % arraySize;
    }

    hashArray[hashValue] = word;
    System.out.println("inserted word: " + word);
    size++;
}

public String find(String word) {
    int hashValue = hashFunction1(word);
    int stepSize = hasFuntion2(word);

    while (hashArray[hashValue] != null) {
        if (hashArray[hashValue].equals(word)) {
            return hashArray[hashValue];
        }
        hashValue = hashValue + stepSize;
        hashValue = hashValue % arraySize;
    }

    return hashArray[hashValue];

}

public static void main(String[] args) {
    HashTableDoubleHashing table = new HashTableDoubleHashing(10);
    table.insert("Rice");
    table.insert("Cola");
    table.insert("Doritos");
    table.insert("Sushi");
    table.insert("Pancakes");

    System.out.println("------------------Find food------------------");
    System.out.println(table.find("Rice"));
    System.out.println(table.find("Pizza"));
    System.out.println(table.find("Sushi"));
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...