防止将重复对象添加到ArrayList或HashSet

时间:2016-05-17 13:36:16

标签: java arraylist hashset

所以我的目标是获取一个String并将其拆分为一个Word对象数组。我只希望一个对象代表一个英文单词,这意味着在添加到数组之前应该过滤掉重复的单词。我不能为我的生活弄清楚为什么我过滤掉重复单词的标准失败了。我已经尝试过ArrayList和HashSet。我的目标是在String中计算该单词的实例,但我还没有实现。

package sandbox;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class Sandbox {

public static void main(String[] args) {

    String original = "the quick brown fox jumps over the lazy dog the quick brown fox jumps over the lazy dog";
    int wordCount = counter(original);//Count the words using a method
    Word[] word = new Word[wordCount];
    List<Word> wordList = new ArrayList<>();
    HashSet wordSet = new HashSet();

    for (int i = 0; i < wordCount; i++) {
        String[] parts = original.split(" ");//Splits the String.
        word[i] = new Word();//Instantiates a Word object.
        word[i].setWord(parts[i], 1);//Sets Word object values.

        if (wordSet.contains(word[i])) {//Criteria for adding the Word object to the HashSet.
            System.out.println("Duplicate detected.");
        } else {
            wordSet.add(word[i]);//Adds the Word object to a HashSet.
        }

        if (wordList.contains(word[i])) {//Criteria for adding the Word object to the ArrayList.
            System.out.println("Duplicate detected.");
        } else {
            wordList.add(word[i]);//Adds the Word object to the ArrayList.
        }

    }

    System.out.println("wordSet size: " + wordSet.size() + " | wordList size: " + wordList.size());
    for (int i = 0; i < wordCount; i++) {

        System.out.println(wordList.get(i));

    }
    System.out.println(wordSet.toString());
}

public static int counter(String s) {

    int wordCount = 0;

    boolean word = false;
    int endOfLine = s.length() - 1;

    for (int i = 0; i < s.length(); i++) {
        // if the char is a letter, word = true.
        if (Character.isLetter(s.charAt(i)) && i != endOfLine) {
            word = true;
            // if char isn't a letter and there have been letters before,
            // counter goes up.
        } else if (!Character.isLetter(s.charAt(i)) && word) {
            wordCount++;
            word = false;
            // last word of String; if it doesn't end with a non letter, it
            // wouldn't count without this.
        } else if (Character.isLetter(s.charAt(i)) && i == endOfLine) {
            wordCount++;
        }
    }
    return wordCount;
}

}

和我的Word课程:

package sandbox;

public class Word {

private String word = "";
private int count;

public Word() {

    word = "";
    count = 0;

}

public void setWord(String w, int c) {
    word = w;
    count = c;

}

public void getWord() {
    System.out.println(word + ", " + count);
}

public boolean duplicate(Word word2) {

    return this.word.equals(word2.word);

}

@Override
public String toString() {
    return ("word: " + this.word + " | count: " + count);
}

public boolean equals(Word word2) {
    return this.word.equals(word2.word);
}
}

这是我目前的输出:

  

wordSet大小:18 | wordList大小:18

     

字:|数:1

     

字:快速|数:1

     

字:棕色|数:1

     

字:狐狸|数:1

     

字:跳跃|数:1

     

字:over |数:1

     

字:|数:1

     

字:懒惰|数:1

     

字:狗|数:1

     

字:|数:1

     

字:快速|数:1

     

字:棕色|数:1

     

字:狐狸|数:1

     

字:跳跃|数:1

     

字:over |数:1

     

字:|数:1

     

字:懒惰|数:1

     

字:狗|数:1

1 个答案:

答案 0 :(得分:2)

您没有覆盖Object equals。你正在超载它。要覆盖它,参数类型应为Object

应该是:

@Override
public boolean equals(Object other) {
    if (!(other instanceof Word)) return false;
    Word word2 = (Word) other;
    return this.word.equals(word2.word);
}

要使HashSet行为正常,您还必须覆盖hashCode