【问题标题】:Removing duplicate elements from ArrayList containing Custom Classes从包含自定义类的 ArrayList 中删除重复元素
【发布时间】:2019-09-15 18:16:26
【问题描述】:

我有一个自定义类 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 识别重复项,这总是不同的。

提前致谢:)

【问题讨论】:

  • 覆盖自定义类中的等于和哈希码。

标签: java sorting arraylist


【解决方案1】:

我看到有些人建议使用 HashSet 或 LinkedHashSet,但在我的情况下,这不起作用,因为它根据 Highscore 的 id 识别重复项,这总是不同的。

您可以通过覆盖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 是可取的。

【讨论】:

  • 谢谢!我从没想过使用 equals() 也很好解释。 +1
猜你喜欢
  • 2015-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
  • 2014-09-19
  • 2011-03-25
相关资源
最近更新 更多