gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:55:39 24 4
gpt4 key购买 nike

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

word: the | count: 1

word: quick | count: 1

word: brown | count: 1

word: fox | count: 1

word: jumps | count: 1

word: over | count: 1

word: the | count: 1

word: lazy | count: 1

word: dog | count: 1

word: the | count: 1

word: quick | count: 1

word: brown | count: 1

word: fox | count: 1

word: jumps | count: 1

word: over | count: 1

word: the | count: 1

word: lazy | count: 1

word: dog | count: 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

关于java - 防止将重复对象添加到 ArrayList 或 HashSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37277761/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com