gpt4 book ai didi

java - 从包含自定义类的 ArrayList 中删除重复元素

转载 作者:行者123 更新时间:2023-12-02 09:31:19 28 4
gpt4 key购买 nike

我有一个自定义类HighscoreArrayList数组列表包含多个 Highscore

如下面的代码所示,存在重复条目。也就是说, highscores.add(new Highscore("user 1", 25)); 当我说重复时,我指的是相同的 String (用户名)和 int(分数)值

我希望能够检测到它,以便只有其中 1 个条目保留在 ArrayList

代码:

ArrayList<Highscore> highscores = new ArrayList<>();
highscores.add(new Highscore("user 1", 25));
highscores.add(new Highscore("user 2", 10));
highscores.add(new Highscore("user 3", 55));
highscores.add(new Highscore("user 1", 25));
highscores.add(new Highscore("user 2", 5));
highscores.add(new Highscore("user 3", 30));

高分:

public class Highscore implements ConfigurationSerializable {

String username;
public int score;

public Highscore(String username, int score) {
this.username = username;
this.score = score;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public int getScore() {
return score;
}

public void setScore(int score) {
this.score = score;
}

@Override
public Map<String, Object> serialize() {
Map<String, Object> mappedObject = new LinkedHashMap<String, Object>();
mappedObject.put("username", username);
mappedObject.put("score", score);
return mappedObject;
}

public static Highscore deserialize(Map<String, Object> mappedObject) {
return new Highscore((String) mappedObject.get("username"),
(int) mappedObject.get("score"));
}
}

我看到有些人建议使用 HashSet 或 LinkedHashSet,但就我而言,这不起作用,因为它根据 Highscore 的 ID 来识别重复项,而该 ID 总是不同的。

提前致谢:)

最佳答案

I have seen some people suggest using a HashSet or LinkedHashSet, but in my case, that doesn't work as it identifies duplicates based on the id of the Highscore, which is always different.

您可以通过重写equals(和hashCode)方法来定义“相似”的含义。有关详细说明,请参阅this .

如果您的要求是不允许基于您的类的属性出现重复 - Set是一个不错的选择。例如HashSet 。您必须重写 equalshashCode 方法:

class Highscore {

private String username;
public int score;

//getters setters constructors

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Highscore highscore = (Highscore) o;
return score == highscore.score &&
Objects.equals(username, highscore.username);
}

@Override
public int hashCode() {
return Objects.hash(username, score);
}
}

然后:

Set<Highscore> highscores = new HashSet<>();

highscores.add(new Highscore("user 1", 25));
highscores.add(new Highscore("user 2", 10));
highscores.add(new Highscore("user 3", 55));
highscores.add(new Highscore("user 1", 25));
highscores.add(new Highscore("user 2", 5));
highscores.add(new Highscore("user 3", 30));

System.out.println(highscores.size());

将打印5并且不添加重复条目。如果您想保留插入顺序,请使用 LinkedHashSet .

当然,您也可以使用 ArrayList 并通过调用 List::remove(object) 来删除元素但它也使用 equals 来比较下面的对象。我认为这里使用 Set 是可取的。

关于java - 从包含自定义类的 ArrayList 中删除重复元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57946947/

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