保持枚举在数组列表中排序?

发布于 2024-09-28 17:21:27 字数 208 浏览 7 评论 0原文

假设我有一个名为 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 技术交流群。

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

发布评论

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

评论(7

送舟行 2024-10-05 17:21:27

最通用的解决方案是使用 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.

安稳善良 2024-10-05 17:21:27

我将有很多数组列表
每种类型最多保​​留一个。我
想要保持每个数组列表排序
所有时间

您所描述的是一个SortedSet(例如TreeSet),而不是一个List

要确定排序顺序,您只需在定义枚举常量时将其置于正确的顺序即可。如果您不想这样做或需要不同的排序顺序,可以使用 Comparator 作为 TreeSet 的构造函数参数。

或者,您可以使用 EnumSet,它不实现 SortedSet,但也会按照声明枚举常量的顺序迭代内容。它也非常快并且内存效率高。

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

What you describe is a SortedSet (such as a TreeSet), not a List.

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 the TreeSet.

Alternatively, you can use an EnumSet, which does not implement SortedSet, but also iterates over the contents in the order in which the enum constants are declared. It's also very fast and memory-efficient.

等风来 2024-10-05 17:21:27

来自 Java 文档

将此枚举与指定对象进行比较以了解顺序。当此对象小于、等于或大于指定对象时,返回负整数、零或正整数。枚举常量只能与相同枚举类型的其他枚举常量进行比较。此方法实现的自然顺序是声明常量的顺序。

因此,如果可以的话,您将不需要实现比较器,但如果您想要按字母顺序排列,则需要实现。

From the Java Doc

Compares this enum with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. Enum constants are only comparable to other enum constants of the same enum type. The natural order implemented by this method is the order in which the constants are declared.

So if that is fine you wont need to implement a Comparator, but if you want alphabetical you will need to.

雪化雨蝶 2024-10-05 17:21:27

Arrays.asList(Planets.values()) 怎么样?

What about Arrays.asList(Planets.values())

帅冕 2024-10-05 17:21:27

该顺序由枚举功能维护。

The order is maintained by the enum fascility.

枫林﹌晚霞¤ 2024-10-05 17:21:27

你可以只使用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.

Saygoodbye 2024-10-05 17:21:27

另请注意,枚举有一个“序数”方法,该方法返回定义该值的位置或顺序。

Also note that Enums have an "ordinal" method which returns their position or order in which that value is defined.

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