1

I've created a Hashtable with a String as key and a LinkedList of Strings as my value. Here is my implementation:

 Hashtable <String, LinkedList<String>> table = new Hashtable <String, LinkedList<String>>();

What I want to do is sort a file of words and store each sorted word into the hashtable (represents the key) and the store the ORIGINAL word as part of my LinkedList value.

For example, if word is

"cat"
 Sorted = "act"
 Store in Hashtable (key : act, value : cat);

Now I'm just getting confused how to essentially add to my LinkedList.

This is what I was thinking :

 LinkedList <String> temp = table.get(sortedWord) //if null, then nothing in list
 if(temp==null) 
     table.put(sortedWord,temp.add(originalWord));

 This is not working since its not following the library functions but I'm unsure of how I would do this.
halapgos1
  • 1,130
  • 4
  • 16
  • 34
  • If your value is single value so that instead of use LinkeList you can direct use Hashtable table = new Hashtable (); – Shiladittya Chakraborty Nov 20 '15 at 06:15
  • But what if the text file contains multiple words that when sorted is the same? That's why I want a LinkedList as the value – halapgos1 Nov 20 '15 at 06:18
  • FYI, we usually use `HashMap` instead of `Hashtable`, unless this is a multi-threaded program. See http://docs.oracle.com/javase/8/docs/api/java/util/Hashtable.html. – ajb Nov 20 '15 at 06:34

3 Answers3

3

Here is my solution. The solution is looping through the words, sorting the chars with Array.sort(). Checking if the Hashtable is populated with the sorted word, and from there either created the LinkedList and adding or adding the element to the already created LinkedList. Not sure why you choose LinkedList as your Datastructure.

Hashtable <String, LinkedList<String>> table = new Hashtable <String, LinkedList<String>>();

for(String s : new String[]{"cat","dog","mouse", "cat"})
{
     char[] chars = s.toCharArray();
     Arrays.sort(chars);
     String sorted = new String(chars);

     if(table.containsKey(sorted))
     {
         LinkedList<String> list = table.get(sorted);
         list.add(s);
     }
     else
     {
         LinkedList<String> list = new LinkedList<String>();
         list.add(s);
         table.put(sorted, list);
     }
}

Which will produce the following Hashtable.

{act=[cat, cat], emosu=[mouse], dgo=[dog]}

Used this question for Sorting the Chars.

Sort a single String in Java

Community
  • 1
  • 1
mrhn
  • 17,961
  • 4
  • 27
  • 46
  • That was a programming error, due to the wording "Now I'm just getting confused how to essentially add to my LinkedList." I don't think the question is that complicated yet. – mrhn Nov 20 '15 at 06:34
  • OK, thanks for fixing it. Please note that the **first** `table.put(sorted,list)` does not accomplish anything, though. The value in the hash map is already a reference to `list`, and you're just replacing it with itself. The **second** one is necessary, of course. – ajb Nov 20 '15 at 06:40
  • I know, but sometimes makes it more clearer to add the reference for readability. – mrhn Nov 20 '15 at 06:41
  • Exactly what I did too! Thank you so much! Def. cleared everything up! – halapgos1 Nov 20 '15 at 06:47
0

You can do:

if(!table.containsKey(sorted)) {
     table.put(new LinkedList<String>())
}
table.get(sorted).add(...)
Nyavro
  • 8,806
  • 2
  • 26
  • 33
0

The problem with this code:

 LinkedList <String> temp = table.get(sortedWord) //if null, then nothing in list
 if(temp==null) 
     table.put(sortedWord,temp.add(originalWord));

is that if temp is null, that means you don't have a LinkedList, but your statement is trying to add originalWord to a LinkedList that doesn't exist. If temp is null, then temp.add is guaranteed to get a NullPointerException.

Using temp.add is what you want to do if you do have a LinkedList (and you don't need another table.put when that happens). If you don't have one, you have to create a new LinkedList with one element. Here's one way:

if (temp == null) {
    LinkedList<String> newList = new LinkedList<>(Arrays.asList(originalword));
    table.put(sortedWord, newList);
} else {
    // you have a LinkedList, add the word to it

(Arrays.asList seems to be the simplest way to create a list with just one element. It won't be a LinkedList, though, so you need an extra constructor call to create the LinkedList.)

ajb
  • 31,309
  • 3
  • 58
  • 84