【问题标题】:Preventing duplicate objects from being added to an ArrayList or HashSet防止将重复对象添加到 ArrayList 或 HashSet
【发布时间】:2016-05-17 13:36:15
【问题描述】:

所以我的目标是获取一个字符串并将其拆分为一个 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;
}

}

还有我的 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 |词表大小:18

单词:|计数:1

词:快|计数:1

单词:棕色 |计数:1

单词:狐狸|计数:1

单词:跳跃 |计数:1

单词:结束|计数:1

单词:|计数:1

单词:懒惰 |计数:1

词:狗 |计数:1

单词:|计数:1

词:快|计数:1

单词:棕色 |计数:1

单词:狐狸|计数:1

单词:跳跃 |计数:1

单词:结束|计数:1

单词:|计数:1

单词:懒惰 |计数:1

词:狗 |计数:1

【问题讨论】:

  • 你的 Word 类必须覆盖 equals 和 hashCode
  • 现在,你没有覆盖equals(Object);因为您正在定义另一个equals(Word)。始终将 @Override 放在您认为会覆盖某些内容的方法上。

标签: java arraylist hashset


【解决方案1】:

您没有覆盖Objectequals。你正在超载它。要覆盖它,参数类型应该是 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

【讨论】:

    猜你喜欢
    • 2012-12-20
    • 2016-08-10
    • 2018-03-15
    • 1970-01-01
    • 2018-03-02
    • 2018-11-16
    • 2020-12-09
    • 2016-10-31
    • 1970-01-01
    相关资源
    最近更新 更多