java - Why doesn't my get-method return the assigned value to my key? -


i've been assigned following task in datastructures , algorithms course, , have no idea why not work.

the task implement linked hashtable. have created 2 classes, 1 acts key <-> value pair, representing element (entry class), , hashtable class has methods (currently put , get), can't seem of them work. hashfunction method provided our teachers, can't answer questions regarding that.

whenever execute program, no errors, list returns empty. here can guide towards right direction of doing wrong? assume error lies within put method, can't seem figure out issue might be.

best regards, victor

 package laboration2;  /**  * class works container key , value.  * 'entry' become element in our hashtable  *  * @author victor marante  * @version 1.0  * @since 2016-09-22  */ public class entry {      private object key;     private object value;     private entry next;      public entry(object key, object value) {         this.key = key;         this.value = value;     }      @override     public boolean equals(object obj) {         entry keytocompare = new entry(obj, null);         return key.equals(keytocompare.key);     }      public object getvalue() {         return value;     }      public void setvalue(object value) {         this.value = value;     }      public object getkey() {         return key;     }      public entry getnext() {         return next;     }      public void setnext(entry next) {         this.next = next;     } } 

class holds methods hashtable itself:

package laboration2;  import javax.swing.*; import java.util.iterator; import java.util.linkedlist;  /**  * created victor on 22/09/16.  */ public class hashtable {      private linkedlist<object> insertionorder = new linkedlist<object>();     private linkedlist<entry>[] table;      // constructor initiates hashtable     public hashtable(int size) {         table = (linkedlist<entry>[]) new linkedlist<?>[size];         (int = 0; < size; i++) {             table[i] = new linkedlist<entry>();         }     }      // hashfunction     private int hashindex(object key) {         int hashcode = key.hashcode();         hashcode = hashcode % table.length;         return (hashcode < 0) ? -hashcode : hashcode;     }      public object get(object key) {         int hashindex = hashindex(key);         linkedlist<entry> entries = table[hashindex];         iterator<entry> = entries.listiterator();         while (it.hasnext()) {             entry entry = it.next();             if (entry.equals(key)) {                 return entry.getvalue();             }         }         return null;     }      public void put(object key, object value) {         int hashindex = hashindex(key);         linkedlist<entry> entries = table[hashindex];         iterator<entry> = entries.listiterator();          while (it.hasnext()) {             entry entry = it.next();             if (entry.equals(key)) {                 entry.setvalue(value);                 insertionorder.add(value);             } else {                 entry.setnext(new entry(key, value));                 insertionorder.add(value);             }         }     }      public static void main(string[] args) {          hashtable table = new hashtable(15);         table.put("hej", "hello");         table.put("nej", "no");         table.put("senare", "later");         table.put("idag", "today");         table.put("igår", "yesterday");          table.get("hej");     } } 

edit1 (for krishas comment):

public void put(object key, object value) {         int hashindex = hashindex(key);         linkedlist<entry> entries = table[hashindex];         iterator<entry> = entries.listiterator();          if (table[hashindex] == null) {             table[hashindex] = new linkedlist<entry>(key, value);          } else {              while (it.hasnext()) {                 entry entry = it.next();                 if (entry.equals(key)) {                     entry.setvalue(value);                     insertionorder.add(value);                 } else {                     entry.setnext(new entry(key, value));                     insertionorder.add(value);                 }             }         }     } 

what wrong

that indeed because of put method. call setnext in wrong place, , has 2 consequences:

  • since list empty, it.hasnext() return false, , never add list
  • even if managed add list, calling setnext if first key in list doesn't match. discarding second element.

i think of clarity comes confusing 2 kinds of lists handling here: 1 lists inside table, purpose handle collisions, meaning different keys ending in same index of table. other 1 global list, purpose record order of insertion.

for first type, don't need 'setnext', need 'add' it. setnext meant secon type (see below).

what should do

you should add new entry if after processing whole list there no match (which includes case list empty), meaning after while loop.

other notes on code

  • you can simplify iteration on list using for-each statement. instead of writing

    iterator<entry> = list.iterator(); entry entry; while(it.hasnext()){    entry = it.next(); 

    you can write

    for(entry entry : list){ 
  • it seems me code posted doesn't compile, since redefining variable entry several times. should define outside loop, , assign values in loop.

  • as mentioned others, equals method of entry class uselessly complex. can replace code by:

    return key.equals(obj); 
  • you don't need insertionorder list. whole point of having next field in entry class, able link entries can iterate on them based on insertion order. need record head of list (the first entry tail of (the latest inserted entry), can link it.

end result put method

public void put(object key, object value) {     int hashindex = hashindex(key);     linkedlist<entry> entries = table[hashindex];      for(entry entry : entries) {         if (entry.equals(key)) {             entry.setvalue(value);             // might want update listtail here             return;         }     }     entry newentry = new entry(key, value);     entries.add(newentry);     listtail.setnext(newentry);     listtail = newentry; } 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -