Java 中的 2D LinkedList.contains()

发布于 2024-08-14 05:43:09 字数 561 浏览 4 评论 0 原文

嘿,大家。我是 Java 新手,我有这样的 2D LinkedList:

LinkedList> albums = new LinkedList>();

其中填充了如下数据:

if (!artist.isEmpty() && !name.isEmpty()) {
    albums.add(new LinkedList());
    albums.getLast().add(artist.toString());
    albums.getLast().add(name.toString());
}

但我想确保我的列表中没有重复的专辑。如何检查我的专辑列表是否已包含相同的艺术家姓名

Hey, everyone. I'm new to Java and I have 2D LinkedList like this:

LinkedList<LinkedList<String>> albums = new LinkedList<LinkedList<String>>();

Which is filled with data like so:

if (!artist.isEmpty() && !name.isEmpty()) {
    albums.add(new LinkedList<String>());
    albums.getLast().add(artist.toString());
    albums.getLast().add(name.toString());
}

But I want to make sure my list has no duplicate albums. How to check whenever my albums list already contains same pair of artist and name?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月如刀 2024-08-21 05:43:09

我的建议是创建一个名为 Album 的新类,看起来像这样:

public class Album
{
    private String name;
    private String artist;

    public Album(String name, String artist)
    {
        this.name = name;
        this.artist = artist;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getArtist()
    {
        return artist;
    }

    public void setArtist(String artist)
    {
        this.artist = artist;
    }

    public boolean equals(Object o)
    {
        if (o instanceof Album)
        {
            Album that = (Album)o;
            return album.equals(that.album) && artist.equals(that.artist);
        }
        else
        {
            return false;
        }
    }

    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((album == null) ? 0 : album.hashCode());
        result = prime * result + ((artist == null) ? 0 : artist.hashCode());
        return result;
    }
}

然后您应该能够使用 contains() 来检查专辑是否已存在于链接列表中。

My suggestion would be to create a new class, called Album that looks something like this:

public class Album
{
    private String name;
    private String artist;

    public Album(String name, String artist)
    {
        this.name = name;
        this.artist = artist;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getArtist()
    {
        return artist;
    }

    public void setArtist(String artist)
    {
        this.artist = artist;
    }

    public boolean equals(Object o)
    {
        if (o instanceof Album)
        {
            Album that = (Album)o;
            return album.equals(that.album) && artist.equals(that.artist);
        }
        else
        {
            return false;
        }
    }

    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((album == null) ? 0 : album.hashCode());
        result = prime * result + ((artist == null) ? 0 : artist.hashCode());
        return result;
    }
}

Then you should be able to use contains() to check whether or not the album already exists in the linked list.

∞琼窗梦回ˉ 2024-08-21 05:43:09

是的,评论者说得对。创建一个包含 artistname 字段的类 Album 并实现 equals() (和 hashCode() )在他们身上。然后您可以使用 contains() 来查找重复项。或者甚至考虑使用 Set (但前提是散列代码确实在您的类上定义,因为集合是由散列支持的)。

Yes, commenter is right. Create a class Album with artist and name fields and implement equals() (and hashCode()) on them. And then you can use contains() to find the duplicate. Or even consider using a Set (but only if hash code is really defined on your class, since a set is backed by a hash).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文