halapgos1
halapgos1

Reputation: 1195

LinkedList as value for Hashtable

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.

Upvotes: 1

Views: 3826

Answers (3)

mrhn
mrhn

Reputation: 18926

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

Upvotes: 3

ajb
ajb

Reputation: 31699

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.)

Upvotes: 0

Nyavro
Nyavro

Reputation: 8866

You can do:

if(!table.containsKey(sorted)) {
     table.put(new LinkedList<String>())
}
table.get(sorted).add(...)

Upvotes: 0

Related Questions