Java - 对象数组到 HashSet

发布于 2024-11-19 08:34:53 字数 1407 浏览 3 评论 0原文

我有以下数组:

private static Optreden[] optredens = {
            new Optreden("Editors", "Main Stage", 16, 0, 4),
            new Optreden("Empire of the Sun", "Pyramid Marquee", 23, 45, 5),
            new Optreden("Florence and the Machine", "Marquee", 18, 45, 3),
            new Optreden("The Specials", "Marquee", 13, 10, 5),
            new Optreden("Muse", "Main Stage", 19, 0, 5),
            new Optreden("Faithless", "Main Stage", 14, 30, 5),
            new Optreden("Absynthe Minded", "Pyramid Marquee", 21, 45, 5),
            new Optreden("Pink", "Main Stage", 20, 30, 2),
            new Optreden("Editors", "Marquee", 21, 20, 4),
            new Optreden("Faithless", "Pyramid Marquee", 19, 0, 5)
            };

Optreden 对象构造函数如下所示:

Optreden(name, stage, hour, minutes, rating);

必须如下所示:

The Specials (Marquee, 13u10)--> *****
Empire of the Sun (Pyramid Marquee, 23u45)--> *****
Florence and the Machine (Marquee, 18u45)--> ***
Pink (Main Stage, 20u30)--> **
Muse (Main Stage, 19u)--> *****
Absynthe Minded (Pyramid Marquee, 21u45)--> *****
Editors (Main Stage, 16u)--> ****
Faithless (Main Stage, 14u30)--> *****

现在,我必须创建 Optreden 对象的 HashSet,但它可能不包含重复的名称,因此当我打印 HashSet 时,它 ,我无法编辑 Optreden 类,它只有一个构造函数和一个 toString 方法,没有 getName() getter。

我怎样才能做到这一点?谢谢。

I've got following array:

private static Optreden[] optredens = {
            new Optreden("Editors", "Main Stage", 16, 0, 4),
            new Optreden("Empire of the Sun", "Pyramid Marquee", 23, 45, 5),
            new Optreden("Florence and the Machine", "Marquee", 18, 45, 3),
            new Optreden("The Specials", "Marquee", 13, 10, 5),
            new Optreden("Muse", "Main Stage", 19, 0, 5),
            new Optreden("Faithless", "Main Stage", 14, 30, 5),
            new Optreden("Absynthe Minded", "Pyramid Marquee", 21, 45, 5),
            new Optreden("Pink", "Main Stage", 20, 30, 2),
            new Optreden("Editors", "Marquee", 21, 20, 4),
            new Optreden("Faithless", "Pyramid Marquee", 19, 0, 5)
            };

The Optreden object constructor looks like this:

Optreden(name, stage, hour, minutes, rating);

Now, I have to create a HashSet of the Optreden objects BUT it may not contain duplicate names, so when I print the HashSet it has to look like this:

The Specials (Marquee, 13u10)--> *****
Empire of the Sun (Pyramid Marquee, 23u45)--> *****
Florence and the Machine (Marquee, 18u45)--> ***
Pink (Main Stage, 20u30)--> **
Muse (Main Stage, 19u)--> *****
Absynthe Minded (Pyramid Marquee, 21u45)--> *****
Editors (Main Stage, 16u)--> ****
Faithless (Main Stage, 14u30)--> *****

Thing is, I can't edit the Optreden class and it only has a constructor and a toString method, no getName() getter.

How can I pull this off? Thanks.

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

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

发布评论

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

评论(3

情泪▽动烟 2024-11-26 08:34:53

它必须是一个 HashSet 吗?如果您愿意使用 TreeSet,请创建一个带有比较名称的自定义比较器的 TreeSet。类似的东西(未编译或测试!):

Comparator<Optreden> compareByName = new Comparator<Optreden>() {
    public int compare(Optreden a, Optreden b) {
        return getName(a).compareTo(getName(b));
    }
    private String getName(Optreden o) {
        String s = o.toString();
        return s.substring(0, (s.indexOf('(') - 1);
    }
}

Set<Optreden> optredensUniqueByName = new TreeSet<Optreden>(compareByName);
optredensUniqueByName.addAll(Arrays.asList(optredens));

Does it have to be a HashSet? If you're happy to use a TreeSet instead, create one with a custom comparator that compares the names. Something like (not compiled or tested!):

Comparator<Optreden> compareByName = new Comparator<Optreden>() {
    public int compare(Optreden a, Optreden b) {
        return getName(a).compareTo(getName(b));
    }
    private String getName(Optreden o) {
        String s = o.toString();
        return s.substring(0, (s.indexOf('(') - 1);
    }
}

Set<Optreden> optredensUniqueByName = new TreeSet<Optreden>(compareByName);
optredensUniqueByName.addAll(Arrays.asList(optredens));
|煩躁 2024-11-26 08:34:53

只要 Optreden 不是 final 类,您就可以对其进行子类化以在构造函数中捕获 name 并实现 equals()< /code> 和 hashcode() 来使用 name,如下所示:

public class MyOptreden extends Optreden
{
    private String name;

    public MyOptreden(String name, String stage, int hour, int minutes, int rating) {
        super(name, stage, hour, minutes, rating);
        this.name = name; // A capture name here
    }

    @Override
    public boolean equals(Object obj) {
        return obj instanceof MyOptreden && ((MyOptreden) obj).name.equals(name);
    }

    @Override
    public int hashCode() {
        return name.hashCode();
    }
}

只要您在集合中仅使用此类的实例,它就可以工作。

您必须重写 setName()(如果存在)才能更新 name

As long as Optreden is not a final class, you can subclass it to trap name in the constructor and implement equals() and hashcode() to use name, as follows:

public class MyOptreden extends Optreden
{
    private String name;

    public MyOptreden(String name, String stage, int hour, int minutes, int rating) {
        super(name, stage, hour, minutes, rating);
        this.name = name; // A capture name here
    }

    @Override
    public boolean equals(Object obj) {
        return obj instanceof MyOptreden && ((MyOptreden) obj).name.equals(name);
    }

    @Override
    public int hashCode() {
        return name.hashCode();
    }
}

As long as you are using only instances of this class in your set, it will work.

You will have to override setName(), if it exists, to update name.

魔法少女 2024-11-26 08:34:53

请看我的评论。根据原始问题中给出的当前限制,即只有一个构造函数和一个 toString() 方法可用,您可以使用有序集(例如 TreeSet)和自定义 Comparator 实现来创建一个集合。然后,您的 Comparator 实现将使用唯一的访问器方法 Optreden.toString() 来解析组的名称。
例如

class OptredenComparator implements Comparator<Optreden> {
...

 int compare(Optreden o1, Optreden o2) {
     // this is to illustrate the logic. Mind the nulls
     String name1 = o1.split("(")[0];
     String name2 = o2.split("(")[0];
     return name1.compareTo(name2);
 }
...
}

,您可以创建一个 TreeSet(new OptredenComparator) 并向其中添加您的 Optreden 实例。

我想强调的是,虽然这是原始问题的答案,但需要解决的根本问题是用作数据容器(Optreden)的类的设计

}

Please see my comment. Under the current restrictions given in the original question i.e. only a constructor and a toString() method available, you could create a set, using an ordered Set (e.g. TreeSet) and a custom Comparator implementation. Your Comparator implementation will then use the only accessor method Optreden.toString() to parse the name of the group.
e.g.

class OptredenComparator implements Comparator<Optreden> {
...

 int compare(Optreden o1, Optreden o2) {
     // this is to illustrate the logic. Mind the nulls
     String name1 = o1.split("(")[0];
     String name2 = o2.split("(")[0];
     return name1.compareTo(name2);
 }
...
}

You can then create a TreeSet(new OptredenComparator) and add your instances of Optreden to it.

I would like to emphasize that although this is the answer to the original question, the root issue that need to be addressed is the design of the class being used as data container (Optreden)

}

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