我添加它们时自动对元素进行排序的Java列表 [英] Java list that automatically sorts elements as I add them

查看:668
本文介绍了我添加它们时自动对元素进行排序的Java列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

在java中排序的集合

我想知道是否有Java中的内置类,可以让我添加自动排序的元素。如果两个元素的排名相同,则排序应保留添加顺序。我认为这将是一个优先级队列,但不应该弹出元素,我希望他们留在列表中。

I'm wondering if there is a built-in class in Java that lets me add elements that are automatically sorted. Sorting should preserve add order if two elements are ranked the same. I think it would be like a priority queue but shouldn't "pop" the elements, I want them to stay in the list.

显然我可以自己实现,我宁愿使用Java语言实现的东西(较少的错误测试/对于未来的项目而言也不错,而不是导入我自己的代码)。

Obviously I could implement this myself but I'd rather use something implemented in the Java language (less bug testing / would be good to know for future projects too instead of importing my own code).

我会如果没有这样的语言,也对第三方来源感兴趣。

I would also be interested in third party source if there isn't anything like this in the language.

推荐答案

似乎 SortedList 这将工作

It seems SortedList this would work


此类实现排序列表。它由比较器构成,可以比较两个对象并相应地排序对象。将对象添加到列表中时,将其插入正确的位置。根据比较器相等的对象将按照列表的顺序添加到列表中。只添加比较器可以比较的对象。

This class implements a sorted list. It is constructed with a comparator that can compare two objects and sort objects accordingly. When you add an object to the list, it is inserted in the correct place. Object that are equal according to the comparator, will be in the list in the order that they were added to this list. Add only objects that the comparator can compare.

这篇关于我添加它们时自动对元素进行排序的Java列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆