Java - 对象数组到 HashSet
我有以下数组:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它必须是一个 HashSet 吗?如果您愿意使用 TreeSet,请创建一个带有比较名称的自定义比较器的 TreeSet。类似的东西(未编译或测试!):
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!):
只要
Optreden
不是final
类,您就可以对其进行子类化以在构造函数中捕获name
并实现equals()< /code> 和
hashcode()
来使用name
,如下所示:只要您在集合中仅使用此类的实例,它就可以工作。
您必须重写
setName()
(如果存在)才能更新name
。As long as
Optreden
is not afinal
class, you can subclass it to trapname
in the constructor and implementequals()
andhashcode()
to usename
, as follows: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 updatename
.请看我的评论。根据原始问题中给出的当前限制,即只有一个构造函数和一个 toString() 方法可用,您可以使用有序集(例如 TreeSet)和自定义 Comparator 实现来创建一个集合。然后,您的 Comparator 实现将使用唯一的访问器方法 Optreden.toString() 来解析组的名称。
例如
,您可以创建一个
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 methodOptreden.toString()
to parse the name of the group.e.g.
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)
}