【问题标题】:How to store positional indexes and document id's in a Hashmap<Hashmap<ArrayList>>如何在 Hashmap<Hashmap<ArrayList>> 中存储位置索引和文档 ID
【发布时间】:2012-01-13 12:40:27
【问题描述】:

我的目录中有文本文件。我需要做的是;
---对于所有文件中的每个单词
---查找文件中每个单词的位置索引
---查找单词通过的每个文件

为了做到这一点;

HashMap<String, HashMap<Integer, ArrayList<Integer>>>

我想使用上述结构。

String word;
        String pattern = "[[^\\w\\süÜıİöÖşŞğĞçÇ]\\d]+";
        while ((word = infile.readLine()) != null) {
            String[] wordList = word.replaceAll(pattern, " ").split("\\s+");

            for (int j = 0; j < wordList.length; j++) {
                if(!wordList[j].isEmpty()){
                        if(!refinedDict.containsKey(wordList[j])){
                            refinedDict.put(wordList[j], 1);
                        }
                        else{
                            refinedDict.put(wordList[j], refinedDict.get(wordList[j])+1);
                        }
                    }//end of for
                 }//end if
                else{
                 //do something   
                }
            }//end for
        }//end while

 Set<String> keys=refinedDict.keySet();
 List<String> list=sortList(keys);
 Iterator<String> it=list.iterator();
 while(it.hasNext()){
       String key=it.next();
       outfile.write(key + "\t" + refinedDict.get(key) + "\n");



如何在 HashMap 中使用 HashMap 中的 ArrayList

编辑
应用toto2的解决方案实施工作后。但是,为了将其写入文件 --->
word[fileId{positions}, fileId{positions}...]
可以做什么?
实现可序列化对于这样的设计没有用处。

【问题讨论】:

    标签: java text arraylist hashmap


    【解决方案1】:

    为了清楚起见,我定义了两个新类 FileId 和 PositionInFile 而不是 Integers。

    Map<String, Map<FileId, List<PositionInFile>>> wordsWithLocations;
    
    for (int j = 0; j < wordList.length; j++) {
       if (!wordList[j].isEmpty()){
          if (!wordsWithLocations.containsKey(wordList[j])) {
             Map<FileId, List<PositionInFile>> map = new HashMap<>();
             List<PositionInFile> list = new ArrayList<>();
             list.add(wordPosition[j]);
             map.put(fileId, list);
             wordsWithLocations.put(wordList[j], map);
           } else {
              Map<FileId, List<PositionInFile>> map = 
                              wordsWithLocation.get(wordList[j]);
              if (map.contains(fileId)) {
                 map.get(fileId).add(wordPosition[j]);
              } else {
                 List<PositionInFile> list = new ArrayList<>();
                 list.add(wordPosition[j]);
                 map.put(fileId, list);
              }
           }
        }
    }
    
    ...
    
    for (String word : wordsWithLocation) {
       int nAppearances = 0;
       for (List<PositionInFile> positions :      
                                wordsWithLocation.get(word).values()) {
          nAppearances += positions.size();
       }
       System.out.println(word + " appears " + nAppearances + " times.");
    }
    

    但我认为定义起来会更简单、更清晰:

    public class WordLocation {
       FileId fileId;
       PositionInFile position;
    
       ... 
    }
    

    然后有一个Map&lt;String, List&lt;WordLocation&gt;&gt;。缺点是您没有对文件的明确映射。但是信息仍然存在,List&lt;WordLocation&gt; 甚至应该以与处理文件相同的顺序列出位置。

    【讨论】:

    • 有效!但是,我非常努力地将其写入文件,但失败了。
    【解决方案2】:

    不确定。 但这是我用于 Map 的一种通用方式,它的值是 Collection 类型。

    Map<String, Collection<something>> map ...
    for ... do some job
       if map.containsKey(keyFound) {
          map.get(foundKey).add(foundValue);
       } else {
          Collection <- create collection
          Collection.add(foundValue);
          map.put(foundKey, collection)
       }
    

    您还可以查看 Google Guava 多地图。

    希望对您有所帮助...

    【讨论】:

      【解决方案3】:

      嵌套地图可以工作。但是我会为此创建一个类,即

      class WordsInFile{
      
      String fileName;
      Map<String, List<Integer>> wordIdxMap;
      
      }
      

      这实际上与嵌套地图没有太大区别。但更具可读性,您可以添加诸如 findWord(...)... 之类的方法,以避免通过调用两次地图的 get(object) 方法而迷失方向。它让你知道你将要得到什么。

      我不知道这是不是个好主意...

      【讨论】:

        【解决方案4】:

        假设您已按上述方式定义 HashMap 并添加如下条目:

        HashMap<String, HashMap<Integer, ArrayList<Integer>>> outer = ...
        HashMap<Integer, ArrayList<Integer>> inner = ...
        inner.put(1, new ArrayList<Integer>());
        outer.put("key1", inner);
        

        您可以将ArrayList 检索为:

        ArrayList<Integer> arr = outer.get("key1").get(1);
        

        【讨论】:

        • 你真的需要演员表吗?
        • @toto2:你是对的,对于通用版本,它们不是必需的。我已经编辑了。
        猜你喜欢
        • 2013-11-01
        • 2023-04-08
        • 1970-01-01
        • 2016-11-04
        • 1970-01-01
        • 1970-01-01
        • 2012-06-18
        • 1970-01-01
        • 2017-12-23
        相关资源
        最近更新 更多