简体   繁体   English

LinkedList作为Hashtable的值

[英]LinkedList as value for Hashtable

I've created a Hashtable with a String as key and a LinkedList of Strings as my value. 我创建了一个Hashtable,将String作为键,并将StringedLinkedList作为值。 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. 我想要做的是对一个单词文件进行排序,并将每个排序后的单词存储到哈希表中(表示键),并将原始单词存储为我的LinkedList值的一部分。

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. 现在我很困惑如何从本质上添加到我的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.

Here is my solution. 这是我的解决方案。 The solution is looping through the words, sorting the chars with Array.sort() . 解决方案是遍历单词,使用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. 检查哈希表中是否填充有已排序的单词,然后从中创建LinkedList并将元素添加或添加到已创建的LinkedList中。 Not sure why you choose LinkedList as your Datastructure. 不确定为什么选择LinkedList作为数据结构。

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 用Java排序单个字符串

You can do: 你可以做:

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

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. 就是说,如果tempnull ,则意味着您没有LinkedList ,但是您的语句试图将originalWord添加到不存在的LinkedList中。 If temp is null , then temp.add is guaranteed to get a NullPointerException . 如果tempnull ,则可以保证temp.add获得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). 使用temp.add是你想要做的,如果有一个什么样LinkedList (你不需要另一个table.put当发生这种情况)。 If you don't have one, you have to create a new LinkedList with one element. 如果您没有,则必须使用一个元素创建一个新的LinkedList 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 .) Arrays.asList似乎是创建仅包含一个元素的列表的最简单方法。尽管它不会是LinkedList ,所以您需要额外的构造函数调用来创建LinkedList 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM