如何使用HashMap解读单词列表?

时间:2016-10-11 23:12:10

标签: java arraylist methods hashmap

我将获得两个文件,我需要阅读我的程序。一个文件将是真实单词的列表,而另一个文件将是不按顺序排列的相同单词的列表。我需要按字母顺序输出加扰的单词,并在其旁边打印真实的单词,我需要使用Hashmap来执行此操作。我的问题是我可以在其旁边打印出乱码字和1个真实字,但在某些情况下,每个混乱字可能会有不止一个真实字。

例如,我的程序可以这样做:

cta cat

stpo post

但我需要它能够做到这一点:

cta cat

stpo post stop

我需要对代码进行哪些更改才能为每个加扰字提供多个字典单词?谢谢您的帮助。我的代码如下:

import java.io.*;
import java.util.*;

public class Project5
{
    public static void main (String[] args) throws Exception
    {

        BufferedReader dictionaryList = new BufferedReader( new FileReader( args[0] ) );
        BufferedReader scrambleList = new BufferedReader( new FileReader( args[1] ) );

        HashMap<String, String> dWordMap = new HashMap<String, String>(); 

        while (dictionaryList.ready())
        {
            String word = dictionaryList.readLine();
            dWordMap.put(createKey(word), word);
        }
        dictionaryList.close();

        ArrayList<String> scrambledList = new ArrayList<String>();

        while (scrambleList.ready())
        {
            String scrambledWord = scrambleList.readLine();

            scrambledList.add(scrambledWord);
        }
        scrambleList.close();

        Collections.sort(scrambledList);

        for (String words : scrambledList)
        {
            String dictionaryWord = dWordMap.get(createKey(words));
            System.out.println(words + " " + dictionaryWord);
        }

    }   

    private static String createKey(String word)
    {
        char[] characterWord = word.toCharArray(); 
        Arrays.sort(characterWord);
        return new String(characterWord);
    }  
}

3 个答案:

答案 0 :(得分:1)

您需要做一些更改。最大的问题是dWordMap只能保留一个String - 它需要保存在乱码文件中找到的单词列表。

下一个变化是能够操纵该列表。我已经添加了一个未经测试的样本解决方案,但应该为您提供一个好的起点。

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;

public class Projects {
    public static void main (String[] args) throws Exception
    {
        BufferedReader dictionaryList = new BufferedReader( new FileReader( args[0] ) );
        BufferedReader scrambleList = new BufferedReader( new FileReader( args[1] ) );

        Map<String, List<String>> dWordMap = new HashMap<>();

        while (dictionaryList.ready()) {
            String word = dictionaryList.readLine();
            dWordMap.put(createKey(word), new ArrayList<>());
        }

        dictionaryList.close();

        while (scrambleList.ready()) {
            String scrambledWord = scrambleList.readLine();
            String key = createKey(scrambledWord);
            List<String> list = dWordMap.get(key);
            list.add(scrambledWord);
        }

        scrambleList.close();

        for (Map.Entry<String, List<String>> entry : dWordMap.entrySet()) {
            String word = entry.getKey();
            List<String> words = entry.getValue();
            Collections.sort(words);
            System.out.println(concatList(words, " ") + " " + word );
        }
    }

    private static String createKey(String word) {
        char[] characterWord = word.toCharArray();
        Arrays.sort(characterWord);
        return new String(characterWord);
    }

    private static String concatList(List<String> list, String delimiter) {
        StringJoiner joiner = new StringJoiner(delimiter);
        list.forEach(joiner::add);
        return joiner.toString();
    }
}

我还会做一些其他的更改 - 第一个是将dictionaryList.close();scrambleList.close();的调用放在finally子句的try...catch部分中确保无论发生什么情况,资源最终都会被释放。您还可以考虑使用Java 8的Streams来使代码更新。如果这不符合您的需求或者您还有其他问题,我会很乐意提供更多提示。祝你好运!

答案 1 :(得分:0)

如果你想记录每个加扰字的字谜词典列表,那么你需要有一个列表的地图:

Map<String, List<String>> anagrams = new HashMap<>();

然后,对于每个加扰的单词,您将向地图添加字典单词列表:

anagrams.put(scrambled, allAnagrams(scrambled));

allAnagrams的样子:

private List<String> allAnagrams(String scrambled) {
    List<String> anagrams = new ArrayList<>();
    for (String word: dictionary) {
        if (isAnagram(word, scrambled))
            anagrams.add(word);
    }
    Collections.sort(anagrams);
    return anagrams;
}

如果你有Java 8并且熟悉流,那么这可能是:

private List<String> allAnagrams(String scrambled) {
    return dictionary.stream()
        .filter(word -> isAnagram(scrambled, word))
        .sorted()
        .collect(Collectors.toList());
}

答案 2 :(得分:0)

改进@ sprinter的Map<String, List<String>>示例:

private final Map<String, List<String>> lookup = new HashMap<>();

public List<String> getList(String word) {
    //can also make #computeIfAbsent use an "initializer" for the key
    return lookup.computeIfAbsent(word, k -> new ArrayList<>());
}

然后与之交互很简单:

List<String> words = getList("tspo"); //spot, post, stop, etc...

您可以从那里进行解扰,如果您想节省空间并找到将密钥编入索引作为特定字符列表的方法,可以更进一步(以便sotptpos只做一次查找。)