簡體   English   中英

如何處理清單<Tuple>

[英]how to deal with List<Tuple>

晚上好,我使用HashMap進行了倒排索引來存儲帶有包含該單詞的文件名的單詞: Map <String,List<String>>例如:

Home : doc1,doc2,doc3 

現在我想做位置索引,將單詞存儲為key ,這一次不僅存儲文件名,而且文件名和#of單詞出現在文件中,並且出現在文件中的位置

<fname,freq,postion1,position2,position3>

例如:

java : doc1,3,4,8,30 

Java在doc1中3次出現在位置4 8 30中,所以我在Google上做過這樣的事情

我發現了Tuple class所以我的hashMap就像

 public Map <String,List<Tuple>> positionIn=new HashMap<>();

和我的Tuple class

public class Tuple {
        private String filepath;
        private int wordfreq;
        private List <Integer> position;

        private Tuple(String filepath, int wordfreq,List position) {
            this.filepath = filepath;
            this.wordfreq=wordfreq;
            this.position = position;
        }

        public List<Integer> getPosition(){return position; }
        public String getfilePath(){return filepath;}
        public int getfreq(){return wordfreq;}

        @Override
        private boolean equals(Object o) {
             if (!(o instanceof Tuple)) return false;
             Tuple t = (Tuple) o;
             return this.filepath.equals(t.fname());
        }
}

我的問題是:

如何處理此元組列表,例如list中的contains方法,是否正確執行此list.contains(fname)list.contains(position)

以及如何分別獲取其元素?

根據評論中的要求,我將其添加為答案:

您希望能夠通過以下方式區分元組:

  • 文檔名稱
  • 出現次數(因為不同的出現次數意味着文件內的單詞不同)

因此,您的equals方法還應該比較兩個列表。 這可以通過list1.containsAll(list2) && list2.containsAll(list1)

對於list.contains(x)您需要x成為Tuple的實例,因為list的類型為List<Tuple>
因此,為了確定您的列表是否包含“ /my/path/myFile.txt”,您需要使用list.contains(new Tuple("/my/path/myFile.txt", 0, null))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM