Java 中的排序列表,如 TreeMap 或 SortedSet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13236203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:59:26  来源:igfitidea点击:

Sorted List in java like TreeMap or SortedSet

javacollections

提问by Lolly

Do we have sorted list in java just like SortedSet or TreeMap ? I have a class having one of the property as List of objects. This list has to be sorted at any time when adding or when setting it through setters (set(List list)).

我们是否像 SortedSet 或 TreeMap 一样在 Java 中有排序列表?我有一个类,其中一个属性为对象列表。在添加或通过设置器(set(List list))设置时,必须随时对这个列表进行排序。

Do we have any component like TreeMap for list ? Any suggestions or help will be really appreciable. Thanks in advance.

我们有像 TreeMap 这样的列表组件吗?任何建议或帮助将是非常可观的。提前致谢。

回答by Bhesh Gurung

The purpose of having a list is that they should maintain the order of the elements in which they were added. So, I don't think whatever you are looking for exists.

拥有列表的目的是它们应该保持添加它们的元素的顺序。所以,我不认为你要找的东西存在。

You can use Collections.sort()method to sort the list any time.

您可以随时使用Collections.sort()方法对列表进行排序。

回答by haylem

What you want is a sorted Bag/MultiSetimplementation, like Google Guava's TreeMultiSet?

你想要的是一个有序的Bag/MultiSet实现,比如Google GuavaTreeMultiSet?

A TreeMultiSetin Guava is defined as:

TreeMultiSetGuava 中的A定义为:

A multiset which maintains the ordering of its elements, according to either their natural order or an explicit Comparator.

一个多重集,它根据元素的自然顺序或显式的 Comparator 保持其元素的顺序。

Where a MultiSetis:

其中 aMultiSet是:

A collection that supports order-independent equality, like Set, but may have duplicate elements. A multiset is also sometimes called a bag.

支持与顺序无关的相等性的集合,如 Set,但可能有重复的元素。多重集有时也称为 bag

For more information about MultiSets, you can read this dzone article on Google Guava: MultiSets(except in your case you really want the TreeMultiSet), and this page of the Guava wiki explaining their new collection types.

有关 MultiSets 的更多信息,您可以阅读Google Guava上的这篇 dzone 文章:MultiSets(除非您确实需要TreeMultiSet),以及解释其新集合类型的 Guava wiki 页面。

回答by Amit Deshpande

You can extends existing ArrayListTo create a SortedList. As you will only have to take care of order while insertion.

您可以扩展现有的ArrayList以创建一个SortedList. 因为您只需要在插入时处理订单。

public class SortedList<E extends Comparable<E>> extends ArrayList<E> {

    @Override
    public boolean add(E e) {
        int index = Collections.binarySearch(this, e);
        super.add(index < 0 ? ~index : index, e);
        return true;
    };
}

Java Doc Collections.binarySearch

Java文档 Collections.binarySearch

Returns: the index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the list: the index of the first element greater than the key, or list.size() if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0if and only if the key is found.

返回: 搜索键的索引,如果它包含在列表中;否则,(-(insertion point) - 1)。插入点定义为将键插入列表的点:大于键的第一个元素的索引,如果列表中的所有元素都小于指定的键,则为 list.size() 。请注意,这保证了>= 0当且仅当找到键时返回值。

Update:As @Louis Wasserman has pointed out this create problems with basic list contract that is insert elements based on index. If you want to support that functionality then you should use Collections.sort(). You can also use org.apache.commons.collections.list.TreeListwhich has below relative performance statistics to that class

更新:正如@Louis Wasserman 指出的那样,这会对基于索引插入元素的基本列表契约造成问题。如果您想支持该功能,那么您应该使用Collections.sort(). 您还可以使用org.apache.commons.collections.list.TreeListwhich 具有低于该类的相对性能统计数据

              get  add  insert  iterate  remove
TreeList       3    5       1       2       1
ArrayList      1    1      40       1      40
LinkedList  5800    1     350       2     325

回答by Adam Arold

You may use some other data type for your Collectionsince you do not care about the order of elements (which is an essential property of a List). For example I think that SortedSetcan do the trick if you don't have duplicates.

您可以使用其他一些数据类型,Collection因为您不关心元素的顺序(这是 a 的基本属性List)。例如,SortedSet如果您没有重复项,我认为这可以解决问题。

Otherwise you can use Collections.sort()on your List.

否则你可以Collections.sort()在你的List.

回答by mluisbrown

The Java SDK doesn't have a sorted Listclass. The easiest solution for what you need would be to call Collections.sort()on your Listevery time you add something to it.

Java SDK 没有排序的List类。您需要的最简单的解决方案是每次添加内容时调用Collections.sort()您的List内容。