提问者:小点点

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


所以我的目标是把一个字符串拆分成一个Word对象数组。我只想要一个对象来表示一个英语单词,这意味着在添加到数组之前应该过滤掉重复的单词。我一辈子都不知道为什么我过滤掉重复单词的标准失败了。我已经尝试了ArrayList和HashSet。我的目标是在字符串中计算该单词的实例,但我还没有实现。

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;
}

}

我的单词分类是:

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);
}
}

这是我当前的输出:

单词集大小:18 |单词列表大小:18

word: the | count: 1

字:快速|计数: 1

单词:棕色|计数:1

单词:狐狸|计数:1

单词:跳跃|计数:1

字:超过|计数:1

word: the | count: 1

单词:懒|计数:1

单词:狗|计数:1

word: the | count: 1

字:快速|计数: 1

单词:棕色|计数:1

单词:狐狸|计数:1

单词:跳跃|计数:1

字:超过|计数:1

word: the | count: 1

单词:懒|计数:1

单词:狗|计数:1


共1个答案

匿名用户

您不会覆盖对象的等号。你正在超载它。要覆盖它,参数类型应为 对象

它应该是:

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

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