【问题标题】:Pre-processing txt document into a hashmap with String as key, and Integer ArrayList as value将 txt 文档预处理为以 String 为键,以 Integer ArrayList 为值的 hashmap
【发布时间】:2016-03-26 08:27:24
【问题描述】:

对于一个作业,我被要求将一个 txt 文档预处理为一个 hashmap,以便拥有一个高效的单词搜索功能。 “为了实现高效的单词搜索,您的代码对文档进行预处理,并将文档中找到的所有单词以及在哈希表中找到它们的行存储起来(单词是键,行是值)。”我一生都无法弄清楚,为什么我无法用密钥的新值替换密钥中的旧值。这是预处理文档的构造函数。

HashMap<String, ArrayList<Integer>> map = new HashMap<String, ArrayList<Integer>>();

public wordSearch() {
    char charChecker;
    String word;
    ArrayList<Integer> count = new ArrayList<Integer>();
    try{
        URL url = new URL("http://homes.soic.indiana.edu/classes/spring2016/csci/c343-yye/docu.txt");
        Scanner in = new Scanner(url.openStream());
        int lineNumber = 1;
        while(in.hasNext()) {
            String str = in.nextLine();
            for(int i = 0; i < str.length(); i++) {
                for(int j = i+1; j < str.length(); j++) {
                    charChecker = str.charAt(j);
                    //check for word 
                    if(charChecker == ' ' ||
                       charChecker == ',' || 
                       charChecker == '.' ||
                       charChecker == '\n') {
                            word = str.substring(i, j);
                            //if word is already in HashMap
                            if(this.map.containsKey(word)) {
                                count = this.map.get(word);
                                count.add(lineNumber);
                                //System.out.println("[" + word + ", " + count.toString() + "]");
                                this.map.put(word, count);
                            }
                            //otherwise add word to HashMap
                            else {
                                count.add(lineNumber);
                                System.out.println(count.toString());
                                this.map.put(word, count);
                                //System.out.println("[" + word + ", " + count.toString() + "]");
                            }
                            i = j+1;
                            count.clear();
                    }
                }
            }
            lineNumber+=1;
        }
    in.close();
    }catch(IOException e) {
        System.out.println(e.getMessage());
    }
    System.out.println(this.map.toString());
}

任何关于从这里去哪里的建议将不胜感激。

【问题讨论】:

    标签: java arraylist hashmap


    【解决方案1】:

    count = new ArrayList&lt;Integer&gt;();代替count.clear();

    现有代码对所有键都有相同的计数实例

    【讨论】:

    • 哇,这太容易了。非常感谢
    【解决方案2】:

    另一种方法是检查一个单词的行号是否存在。

    HashMap<String, List<Integer>> wordMap = new HashMap<>();
    List<Integer> lineNumbers;
    
    if(...){
        word = str.substring(i, j);
        if ((lineNumbers = wordMap.get(word)) != null){
            lineNumbers.add(lineNumber);
        }else {
            lineNumbers = new ArrayList<>();
            lineNumbers.add(lineNumber);
            wordMap.put(word, lineNumbers);
        }
        i = j+1
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-11
      • 1970-01-01
      • 1970-01-01
      • 2016-11-20
      • 1970-01-01
      • 2021-02-21
      相关资源
      最近更新 更多