保持枚举在数组列表中排序?
假设我有一个名为 Planets 的枚举,其中包含 VENUS、EARTH 和 MARS。我将有很多数组列表,每种类型最多包含一个。我想始终按照金星、地球和火星的顺序对每个数组列表进行排序。
我需要为此使用比较器吗?有没有办法让它们在插入后自动排序,或者我需要在每次插入后调用排序?我需要在每种类型中保留一个 int 值来区分它们的顺序吗?
如果有其他建议请提供,谢谢。
Say I had a an enum called Planets that contained VENUS, EARTH, and MARS. I will have a lot of array lists that will hold at most one of each type. I want to keep each array list sorted at all times in order from VENUS, EARTH, and MARS.
Would I need to use a comparator for this? Is there a way to keep them sorted automatically after an insert, or will I need to call sort after each insert? Will I need to keep an int value within each type to distinguish their order?
Offer alternative advice if you have any, thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
最通用的解决方案是使用 TreeSet,它在插入时按排序顺序保存项目。如果您不提供比较器,那么它将保持“自然排序”,对于枚举来说,这是它们声明的顺序。由于该顺序很容易更改,因此最好的选择是使用自定义比较器声明 TreeSet必要时命令它们。
当然,如果你只有 3 个枚举,并且每个集合最多有一个,你可能会偷懒并以正确的顺序手动将它们放入 EnumSet 中,但从长远来看,TreeSet 可能是“正确”的方法。
The most general solution is to use a TreeSet, which keeps items in sorted order as you insert. If you don't provide a comparator, then it'll maintain "natural ordering", which for enums is the order they were declared in. Since that order is susceptible to change, your best bet is to declare the TreeSet with a custom Comparator that orders them as necessary.
Of course, if you only have 3 enums, and each collection has at most one of each, you could be lazy and manually put them in an EnumSet in the proper order, but the TreeSet is probably the "proper" approach for the long term.
您所描述的是一个
SortedSet
(例如TreeSet
),而不是一个List
。要确定排序顺序,您只需在定义枚举常量时将其置于正确的顺序即可。如果您不想这样做或需要不同的排序顺序,可以使用
Comparator
作为TreeSet
的构造函数参数。或者,您可以使用
EnumSet
,它不实现SortedSet
,但也会按照声明枚举常量的顺序迭代内容。它也非常快并且内存效率高。What you describe is a
SortedSet
(such as aTreeSet
), not aList
.To determine the sort order, you just have to put the enum constants in the correct order when you define them. If you don't want to do that or need a varying sort order, you can use a
Comparator
as constructor argument for theTreeSet
.Alternatively, you can use an
EnumSet
, which does not implementSortedSet
, but also iterates over the contents in the order in which the enum constants are declared. It's also very fast and memory-efficient.来自 Java 文档
因此,如果可以的话,您将不需要实现比较器,但如果您想要按字母顺序排列,则需要实现。
From the Java Doc
So if that is fine you wont need to implement a Comparator, but if you want alphabetical you will need to.
Arrays.asList(Planets.values()) 怎么样?
What about
Arrays.asList(Planets.values())
该顺序由枚举功能维护。
The order is maintained by the enum fascility.
你可以只使用EnumSet。它使枚举按其自然顺序(即您在枚举声明中指定的顺序)排序。这是已记录的功能,您可以信赖它。
You can just use EnumSet. It keeps enums sorted by their natural ordering (i.e. order you specify in your enum declaration). This is documented feature, you can rely on it.
另请注意,枚举有一个“序数”方法,该方法返回定义该值的位置或顺序。
Also note that Enums have an "ordinal" method which returns their position or order in which that value is defined.