14 Replies - 1397 Views - Last Post: 10 October 2012 - 02:56 PM Rate Topic: -----

#1 mastapro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

can you append to a hashtable value?

Posted 08 October 2012 - 04:41 PM

I'm making a hashtable which stores the sorted word as a key and anagrams of that word as a value. For example, the word pot would be sorted as "opt" and then that would be a key. Then the words pot and top would be the value of that key. So my code basically is like this...

read in a dictionary file
sort the word alphabetically by the characters
if(there is a key of that word)
  //add that word to the value of that key
else
  //make that sorted word a key

let user input a scrambled word
sort that word
check if there is a key that exists and print out the values



basically something simple like that. but i was wondering if it's possible to even append to the value of a hashtable? like if i found another word that matches a key, i want to add that string to the value that's already there... thanks guys.

This post has been edited by mastapro: 08 October 2012 - 04:44 PM


Is This A Good Question/Topic? 0
  • +

Replies To: can you append to a hashtable value?

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: can you append to a hashtable value?

Posted 08 October 2012 - 05:04 PM

Are you looking to replace the value of a key, or have more than one value per key? In either case, this pretty much defies the purpose of a HashMap, which is to hold a unique key/value pairing. There are ways around this such as using a List as the value, I personally dont like this option though. You would be better off redefining what you need, and then looking into a better option in achieving this, maybe use something like a MultiMap provided by Google, or even just use seperate tables.
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: can you append to a hashtable value?

Posted 08 October 2012 - 07:58 PM

This sentence does not make sense

View Postmastapro, on 08 October 2012 - 07:41 PM, said:

i was wondering if it's possible to even append to the value of a hashtable? like if i found another word that matches a key, i want to add that string to the value that's already there...

That will defeat the whole HashMap key concept
You can't find another word that matches the key you just find another occurence of the key
Was This Post Helpful? 0
  • +
  • -

#4 mastapro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

Re: can you append to a hashtable value?

Posted 08 October 2012 - 08:21 PM

View Postpbl, on 08 October 2012 - 07:58 PM, said:

This sentence does not make sense

View Postmastapro, on 08 October 2012 - 07:41 PM, said:

i was wondering if it's possible to even append to the value of a hashtable? like if i found another word that matches a key, i want to add that string to the value that's already there...

That will defeat the whole HashMap key concept
You can't find another word that matches the key you just find another occurence of the key


i wrote it in a very confusing way now that i read it again... sorry about that. here's an example of what i mean. one of the words in the dictionary is pot, so i sort the chars to make the word "opt" and set "opt" as a key. then pot will be a value to that key. then when the iterator gets to top, it will be sorted to "opt", and i want to update the key so that pot and top will be there. idk if that's a better explanation but it's hard to express what i want to do
Was This Post Helpful? 0
  • +
  • -

#5 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: can you append to a hashtable value?

Posted 09 October 2012 - 05:15 AM

Your explantion is still pretty hard to follow, but it still sounds like your wanting to go against the HashMap concept. A hashmap should have a set key/value pairing which is unique. What it sounds like you want to do is change the key/value pairing. The api is a good source of information, and in doing a quick scan, I cant see a method which would allow you to make a change, and this is totally understandable. From your explanation and what I understand, I really dont think a HashMap is what you want here, especially the way you want to use it. I would suggest considering a using a new Collection which does not rely on unique relationships, or create seperate HashMaps for your different key/value pairings.
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: can you append to a hashtable value?

Posted 09 October 2012 - 06:11 AM

People are getting hung up on their definition of HashMap and ignoring the problem you presented... :P

Just make the "value" part of the key value pairs a List. So:
read in a dictionary file
apply hash function to word: sort the word alphabetically by the characters
if there is a key of that word
   if there is a value for that word in the list
      do nothing
   else
      add word to list
else
   create a list with one value, your word, in it
   add key and list to hashtable

let user input a scrambled word
apply hash function to word
if there is a key that exists
   print out the values in the list


Was This Post Helpful? 0
  • +
  • -

#7 mastapro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

Re: can you append to a hashtable value?

Posted 09 October 2012 - 11:20 AM

View Postbaavgai, on 09 October 2012 - 06:11 AM, said:

People are getting hung up on their definition of HashMap and ignoring the problem you presented... :P

Just make the "value" part of the key value pairs a List. So:
read in a dictionary file
apply hash function to word: sort the word alphabetically by the characters
if there is a key of that word
   if there is a value for that word in the list
      do nothing
   else
      add word to list
else
   create a list with one value, your word, in it
   add key and list to hashtable

let user input a scrambled word
apply hash function to word
if there is a key that exists
   print out the values in the list


yes thank you. this is making a bit more sense. but now i realize a problem. if i want to allow spaces in the answer... how would i go about doing that? for example if a user inputted "yihou"... it would print out "hi you" and "you hi" as anagrams of that word even if it is a phrase with spaces that is made makes no sense.. as long as the letters are all there, it will be printed out... idk if a hashmap can rly do this
Was This Post Helpful? 0
  • +
  • -

#8 mastapro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

Re: can you append to a hashtable value?

Posted 09 October 2012 - 01:51 PM

Ok i was able to figure out how to append to a value, i used a hashmap of <string, string> and then if i found a duplicate and wanted to add to the value, i would just take the value and add the 2nd value and put it back under the same key, this would overwrite the old one. i'm still stuck on how to get an anagram of a phrase tho
Was This Post Helpful? 0
  • +
  • -

#9 mastapro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

Re: can you append to a hashtable value?

Posted 10 October 2012 - 10:19 AM

this is the part i'm stuck with. if a person entered "iterehh", it would recognize "hi there" and "there hi" as possible anagrams for that scrambled word.. i can only find anagrams that are 1 word, not phrases.. how would i go about doing this?
Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: can you append to a hashtable value?

Posted 10 October 2012 - 12:09 PM

Well, you could look at all your words together. However, you've then raised your search set by an order of magnitude.

e.g.
For the sentence: Hi there I saw you there hi.

Your hash would look like:
aeeeehhhhiiiorrsttuwy : "hi there i saw you there hi"
aeeeehhhiiorrsttuwy : "hi there i saw you there" "there i saw you there hi"
aeeeehhiorrsttuwy : "there i saw you there"
aeehhiiorstuwy : "hi there i saw you" "i saw you there hi"
aeehhiirstw : "hi there i saw"
aeehhiorstuwy : "saw you there hi"
aeehiorstuwy : "there i saw you" "i saw you there"
aeehirstw : "there i saw"
aeehorstuwy : "saw you there"
aiosuwy : "i saw you"
aisw : "i saw"
aosuwy : "saw you"
asw : "saw"
eehhiirt : "hi there i"
eehhiortuy : "you there hi"
eehhirt : "hi there" "there hi"
eehirt : "there i"
eehortuy : "you there"
eehrt : "there"
hi : "hi"
i : "i"
ouy : "you"


Was This Post Helpful? 1
  • +
  • -

#11 mastapro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

Re: can you append to a hashtable value?

Posted 10 October 2012 - 12:43 PM

View Postbaavgai, on 10 October 2012 - 12:09 PM, said:

Well, you could look at all your words together. However, you've then raised your search set by an order of magnitude.

e.g.
For the sentence: Hi there I saw you there hi.

Your hash would look like:
aeeeehhhhiiiorrsttuwy : "hi there i saw you there hi"
aeeeehhhiiorrsttuwy : "hi there i saw you there" "there i saw you there hi"
aeeeehhiorrsttuwy : "there i saw you there"
aeehhiiorstuwy : "hi there i saw you" "i saw you there hi"
aeehhiirstw : "hi there i saw"
aeehhiorstuwy : "saw you there hi"
aeehiorstuwy : "there i saw you" "i saw you there"
aeehirstw : "there i saw"
aeehorstuwy : "saw you there"
aiosuwy : "i saw you"
aisw : "i saw"
aosuwy : "saw you"
asw : "saw"
eehhiirt : "hi there i"
eehhiortuy : "you there hi"
eehhirt : "hi there" "there hi"
eehirt : "there i"
eehortuy : "you there"
eehrt : "there"
hi : "hi"
i : "i"
ouy : "you"



Sorry could you explain this more? I'm not understanding it lol. Do you mean that I should add all that to the original dictionary txt file?
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: can you append to a hashtable value?

Posted 10 October 2012 - 01:01 PM

You can't do it manually you will have for hours

With all possible word interpolation

extract all the letters from them
sort them
make your key out of it


eehrt : "there"

t h e r e sorted == e e h r t your key

This post has been edited by pbl: 10 October 2012 - 01:03 PM

Was This Post Helpful? 0
  • +
  • -

#13 mastapro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

Re: can you append to a hashtable value?

Posted 10 October 2012 - 01:12 PM

View Postpbl, on 10 October 2012 - 01:01 PM, said:

You can't do it manually you will have for hours

With all possible word interpolation

extract all the letters from them
sort them
make your key out of it


eehrt : "there"

t h e r e sorted == e e h r t your key

ok so i extract all the letters from the dictionary txt file. i sort them. and i make my key out of that? like the key "eehrt" holds the value "there"? Is that what you're saying? The way I did it was I had a Hashmap<String,String> and whenever a sorted word matched a key, i would put the unsorted version of that word onto the end of the value that was already there. sorry i'm having a hard time grasping wut ur trying to explain
Was This Post Helpful? 0
  • +
  • -

#14 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: can you append to a hashtable value?

Posted 10 October 2012 - 01:17 PM

View Postmastapro, on 10 October 2012 - 03:43 PM, said:

Sorry could you explain this more? I'm not understanding it lol. Do you mean that I should add all that to the original dictionary txt file?


Actually, I'm just explaining permutations. Those are all the permutations of a single, simple, sentence. If you want to do a lookup on more than one word, where do you end? Two words, three, five? You essentially have to look over and over again to find all your combinations.

To be more clear, the sentence "hi there i saw you there hi" has these permutations:

hi
hi there
hi there i
hi there i saw
hi there i saw you
hi there i saw you there
hi there i saw you there hi
there
there i
there i saw
there i saw you
there i saw you there
there i saw you there hi
i
i saw
i saw you
i saw you there
i saw you there hi
saw
saw you
saw you there
saw you there hi
you
you there
you there hi
there
there hi
hi
Was This Post Helpful? 1
  • +
  • -

#15 mastapro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

Re: can you append to a hashtable value?

Posted 10 October 2012 - 02:56 PM

View Postbaavgai, on 10 October 2012 - 01:17 PM, said:

View Postmastapro, on 10 October 2012 - 03:43 PM, said:

Sorry could you explain this more? I'm not understanding it lol. Do you mean that I should add all that to the original dictionary txt file?


Actually, I'm just explaining permutations. Those are all the permutations of a single, simple, sentence. If you want to do a lookup on more than one word, where do you end? Two words, three, five? You essentially have to look over and over again to find all your combinations.

To be more clear, the sentence "hi there i saw you there hi" has these permutations:

hi
hi there
hi there i
hi there i saw
hi there i saw you
hi there i saw you there
hi there i saw you there hi
there
there i
there i saw
there i saw you
there i saw you there
there i saw you there hi
i
i saw
i saw you
i saw you there
i saw you there hi
saw
saw you
saw you there
saw you there hi
you
you there
you there hi
there
there hi
hi

ah ah ok i see what you're saying. but most of those can't be a permutation for "hi i saw you there" because they have too few letters. for example, hi isn't a permutation because it doesn't utilize all the letters that the user inputs if he were to input something like "ihiwasyouheret". also, the dictionary txt file isn't too big so there won't be a crazy amount of permutations
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1