【问题标题】:Sorting all words in a file by length, in one read. (Java)在一次读取中按长度对文件中的所有单词进行排序。 (爪哇)
【发布时间】:2010-11-22 10:51:02
【问题描述】:

对于我的数据结构类,任务是找到从一个单词到另一个单词的最短路径。

即开始:流血 -> 混合 -> 金发 -> 结束:血液,成本为 3。

我得到了一个单词列表,我必须使用地图对其进行分组。地点:

Key:单词的长度,Value:具有该长度的所有单词的集合。

我已经完成了这个程序,但我认为如果我改变我在地图中存储集合的方式,我可以提高性能。现在我扫描文本文件并将每个单词存储到一个 ArrayList 中,然后我遍历 ArrayList 并将所有长度为 x 的单词存储到一个集合中,同时从列表中删除每个单词。我从 ArrayList 中的第一个元素开始,直到 List 为空。

我想知道是否可以在读取文件时进行这种排序,并完全避免使用 ArrayList。

这是我的代码:

ArrayList<String> wordList = new ArrayList<String>();
Map<Integer, Set> setMap = new HashMap<Integer, Set>();
Graph pathGraph = new Graph();

private void readFile(String file) {
    try {
        FileReader f = new FileReader(file);
        BufferedReader reader = new BufferedReader(f);
        String line = "";
        while ((line = reader.readLine()) != null) {
            wordList.add(line);
        }

    } catch (Exception e) { //Done in case of an exception
        System.out.println("No file found.");
    }
}

private void mapMaker() {
    int wordLength = 1;
    Set<String> wordSet = new HashSet<String>();
    while (!wordList.isEmpty()) {
        wordSet = setBuilder(wordLength);
        if (!wordSet.isEmpty()) {
            setMap.put(wordLength, wordSet);
        }
        wordLength++;
    }
}

private Set<String> setBuilder(int x) {
    Set<String> wordSet = new HashSet<String>();
    int counter = 0;
    while (counter < wordList.size()) {
        if (wordList.get(counter).length() == x) {
            wordSet.add(wordList.get(counter));
            wordList.remove(counter);
        } else {
            counter++;
        }
    }
    return wordSet;
}

感谢您的任何意见。

【问题讨论】:

    标签: java


    【解决方案1】:
    private void readFile(String file) {
        try {
            FileReader f = new FileReader(file);
            BufferedReader reader = new BufferedReader(f);
            String word = "";
            while ((word = reader.readLine()) != null) { 
                int length = word.length();
                if(setMap.containsKey(length)) {
                    setMap.get(length).add(word);
                } else {
                    Set set = new HashSet<String>();
                    set.add(word); 
                    setMap.put(length, set);
                }
            }
    
        } catch (Exception e) { //Done in case of an exception
            System.out.println("No file found.");
        }
    }
    

    【讨论】:

    • 谢谢,我没有意识到你可以像那样访问存储在地图中的集合。这就是为什么我有额外的方法。
    【解决方案2】:

    您可以使用Guava's MultiMap

    例子:

            String[] words={"world","hello","abc","bcd","abc"};
            SetMultimap<Integer,String> lenMap=HashMultimap.create();
            for(String str:words)//instead read word's from file in your case
                lenMap.put(str.length(),str);
    

    输出:

    {3=[abc, bcd], 5=[hello, world]}
    

    【讨论】:

      猜你喜欢
      • 2013-05-08
      • 2013-04-07
      • 1970-01-01
      • 1970-01-01
      • 2016-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-21
      相关资源
      最近更新 更多