小编Mat*_*ran的帖子

HashSet不删除所有重复的条目

我试图使用HashSet来确保我从.txt文件中读取的数据是唯一的.

以下是样本数据;

999990  bummer
999990  bummer
999990  bummer
999990  bummer
99999   bummer
999990  bummerr
Run Code Online (Sandbox Code Playgroud)

使用Java.io.File和Java.util.Scanner读取并将其存储为Term of Term;

阅读;

while (rawTerms.hasNextLine()){
    String[] tokens = rawTerms.nextLine().trim().split(delimiter);
    if (tokens.length == 2) {               
        uniqueSet.add(new Term(Double.parseDouble(tokens[0]), tokens[1])); //add the term to set
    }
    else {
      rawTerms.close();
      throw new Exception("Invalid member length: "+ tokens.length);
    }           
}

allTerms = new ArrayList<>(uniqueSet); //Covert set into an ArrayList
Run Code Online (Sandbox Code Playgroud)

使用番石榴的术语类;

public Term(double weight, String theTerm){
    this.weight = weight;
    this.theTerm = theTerm;
}


@Override
public boolean equals(final Object obj) {
    if …
Run Code Online (Sandbox Code Playgroud)

java eclipse guava

0
推荐指数
2
解决办法
1210
查看次数

标签 统计

eclipse ×1

guava ×1

java ×1