简体   繁体   中英

How do I get a V from a (K,V) node in a hashtable back to the calling method in another class?

I'm in a data structures class and we have an assignment that includes creating a hash table for a latin dictionary and on the getDefinition method in the LatinDictionary class (basically a wrapper) it asks for string back after an input of a string like so...

import java.util.Iterator;
import data_structures.*;

public class LatinDictionary {
private DictionaryADT<String,String> dictionary;
private int maxSize = DictionaryReader.entries.length;
....
public String getDefinition(String latinWord) {
    return dictionary.getValue(latinWord);
}

The getValue method is as follows:

package data_structures;
import java.util.Iterator;

public class HashTable <K,V> implements DictionaryADT<K,V>{

private int maxSize,currentSize,tableSize;
private UnorderedList<DictionaryNode<K,V>>[] list;
DictionaryADT<String, String> dictionary;
...
public V getValue(K key){
    int code = hashCode((String)key);
    DictionaryNode temp = new DictionaryNode(key,null);
    if(!list[code].contains(temp))return null;
    DictionaryNode temp2 = new DictionaryNode(null, list[code].find(temp));
    return (V) temp2.value;
}

and the find method from the unordered list class is as follows:

package data_structures;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class UnorderedList<E> implements Iterable<E>{
public E find(E object) {
    Node<E> current=head;
    Node<E> previous = head;
    Node<E> temp=null;
    while(current!=null && ((Comparable<E>)object).compareTo((current.data))!=0){
        previous=current;
        current=current.next;
        temp=current;
        }

    return temp.data;
    }

The dictionaryADT is an interface provided and a specification is that the LatinDictionary will only reference the ADT object and not the hashtable itself. Additionally, I cannot import java.util.*.
No matter what I try I keep getting a "cannot cast" error or something else depending on what I try but I can't see how to get from a V to a string. Unfortunately I can't find ANYTHING on the internet relating to self-built hashtables let alone the sort of implementation I have to use here. Anything that has to do with hashtables uses the built-in version and not a self written one, which is of very little use. I have this project due in like 2 weeks but there are 3 other implementations to do after this one! Any help is greatly appreciated. Thanks.

If you can't do as @Shakedown, then you could do this:

public class LatinDictionary implements HomeworkHashtable<String, String> {

    public String getValue(String key){
        int code = hashCode(key);
        DictionaryNode temp = new DictionaryNode(key,null);
        if(!list[code].contains(temp))return null;
        DictionaryNode temp2 = new DictionaryNode(null, list[code].find(temp));
        return temp2.value;
    }

}

I'd need to see the definition of the Map class, and the unordered list class, but presumably the dictionary is defined as follows

public class MyMap<K, V> {
  public V getValue(K key) ...
}

If that's the case, in your LatinDictionary the back end map ( dictionary ) should be declared with generic types of String .

public class LatinDictionary {
  private MyMap<String, String> dictionary ...
}

In that case, you don't need to convert to String , the correct class is gaurenteed to be returned.

[Edit] On another note, if you are writing the implementation of the map, you need to put some thought into hash conflicts.

You could also think about using some standard Java methods, they will generally save you trouble.

//This
int code = hashCode((String) key);
//Could become this
int code = key.hashCode();    //Don't need any casts!

I got it....instead of

public V getValue(K key){
.....
DictionaryNode temp2 = new DictionaryNode(null, list[code].find(temp));

it should be...

public V getValue(K key){
....
DictionaryNode temp2 = new DictionaryNode(null, list[code].find(temp).value);
return (V) temp2.value;
}

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