Need help big time with hashtable!

Well, i have a problem facing me. I've to design a predictive text program which use's a keypad exactly the same on a phone. I'm in the middle of it now. i'm using a hashtable to store my dictionary and have a major problemi cant manage to get around.

// the 4669 stands for the letters on the phone. 4=ghi,6=mno,9=def

Hashtable pt_dictionary = new Hashtable();

void put()

{

pt_dictionary.put("4996", "good");

pt_dictionary.put("4996", "home");

pt_dictionary.put("4996", "gome");

pt_dictionary.put("4996", "hood");

pt_dictionary.put("4996", "hoof");

pt_dictionary.put("4996", "hone");

pt_dictionary.put("4996", "goof");

}

These are all the clashes when the user selects 4996. Above is the typical sequence they come out on a phone. But if a user use's home more times than good, home should be the first to be output. The only answer to this, i think, is as follows :

public class word_node//creates a node with amount of usuage for sorting

{

private String word;

private int usuage = 1;

public void node_word(String w, int u)//Constructor

{

word = w;

usuage = u;

}

public void word_node(word_node w_n)//Constructor

{

word = w_n.word;

usuage = w_n.usuage;

}

public void update_usuage()//updates the usuage

{

usuage++;

}

}

public void create_nodes()

{

word_node good(String good, int i=1);

word_node gone(String gone, int i=1);

word_node hood(String hood, int i=1);

word_node hoof(String hoof, int i=1);

word_node hone(String hone, int i=1);

word_node goof(String goof, int i=1);

}

public void create_Hashtable_&_put_in_values()

{

Hashtable pt_dictionary = new Hashtable();

pt_dictionary.put("4669", good);

pt_dictionary.put("4669", home);

pt_dictionary.put("4669", gone);

pt_dictionary.put("4669", hood);

pt_dictionary.put("4669", hoof);

pt_dictionary.put("4669", hone);

pt_dictionary.put("4669", goof);

}

Now, all word_node's in 4669 buckets have there usuage equal to 1. Now lets say the user uses home, "home.update_usuage()" updates the usuage and the next time 4669 is selected, home should be first option seen!. I can't for the life of me get the code to work for me. If anyone could help in anyway i would be so grateful,if its even possible!Cheers

King regards,

John Stafford

[2605 byte] By [johnwstafforda] at [2007-11-26 14:38:49]
# 1

I admit to not having read everything you wrote... there's just too much of it. But...

Hashtable (and HashMap, which you should really be using unless you have some external constraint requiring the use of Hashtable) map each key to a single value object.

That is, you can map from the key string "4996" to one single other object. And then you can also map from a different key to a different object.

In you put() method, you add a mapping from "4996" to "good", and then you replace that mapping with one from "4996" to "home". There is only ever one value associated with a given key.

To map multiple values to a single key, you need the value object to be a collection of some kind (or an array). So, you could map your string "4996" to a List, and that list could contain the strings "good", "home", etc.

dannyyatesa at 2007-7-8 8:19:55 > top of Java-index,Core,Core APIs...
# 2

To further expand on dannyyates's idea,

Maybe you can can define a collection class PriorityCollection and have your dictionary be Map<String, PriorityCollection><String>>. The PriortyCollection<T> can have method bumpPriority(T) and provides an iterator that iterates the elements in decreasing order of priority. PriorityCollection can be implemented using a LinkedList so as to move elements when their priorities change.

hemalpandyaa at 2007-7-8 8:19:55 > top of Java-index,Core,Core APIs...
# 3

Ok, i've learned alot from that thanks lads. I was under the assumption that you could map a key and have many different variables in the "bucket". So the way in which I can this working now is to have "4669" map to a linklist which inturn keeps my data in the corrert order. I'll go at that and see how I get on. Thanks for the help. I love linkedlists so this could be a good thing! I'll report back on how i'm getting on later if interested!

Kind regards,

John Stafford

johnwstafforda at 2007-7-8 8:19:55 > top of Java-index,Core,Core APIs...
# 4

> Ok, i've learned alot from that thanks lads. I was

> under the assumption that you could map a key and

> have many different variables in the "bucket". So the

> way in which I can this working now is to have "4669"

> map to a linklist which inturn keeps my data in the

> corrert order. I'll go at that and see how I get on.

> Thanks for the help. I love linkedlists so this could

> be a good thing! I'll report back on how i'm getting

> on later if interested!

> Kind regards,

> John Stafford

You are welcome. Best of luck. I for one am interested in the solution you finally settle on.

hemalpandyaa at 2007-7-8 8:19:55 > top of Java-index,Core,Core APIs...
# 5
Dear John.I have the same problem with you.I have done similar program with ArrayList. but I still have problem to do it with Hashtable.Please can you help me.by showing me how you came up to do itThanksTheotime
theotimea at 2007-7-8 8:19:55 > top of Java-index,Core,Core APIs...
# 6
Perhaps you could read the above like he did and learn how to do it for yourself. More useful as a general technique, don't you think?
ejpa at 2007-7-8 8:19:56 > top of Java-index,Core,Core APIs...