Java 以降序排列元素的树集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1090969/
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
Treeset to order elements in descending order
提问by Gaurav Saini
Here is the piece of code that I have used for Java 5.0
这是我用于 Java 5.0 的一段代码
TreeSet<Integer> treeSetObj = new TreeSet<Integer>( Collections.reverseOrder() ) ;
Collections.reverseOrder()is used to obtain a comparator in order to reverse the way the elements are stored and iterated.
Collections.reverseOrder()用于获取比较器,以反转元素的存储和迭代方式。
Is there a more optimized way of doing it?
有没有更优化的方法?
采纳答案by Adamski
Why do you think this approach won't be optimized? The reverse order Comparator
is simply going to be flipping the sign of the output from the actual Comparator
(or output from compareTo
on the Comparable
objects being inserted) and I would therefore imagine it is very fast.
为什么你认为这种方法不会被优化?相反的顺序Comparator
只是将实际输出的符号Comparator
(或插入对象compareTo
上的输出)翻转,Comparable
因此我认为它非常快。
An alternative suggestion: Rather than change the order you store the elements in you could iterate over them in descending order using the descendingIterator()
method.
另一个建议:与其更改存储元素的顺序,不如使用该descendingIterator()
方法按降序迭代它们。
回答by Pierre
TreeSet<Integer> treeSetObj = new TreeSet<Integer>(new Comparator<Integer>()
{
public int compare(Integer i1,Integer i2)
{
return i2.compareTo(i1);
}
});
there is need to flip the result. But I guess this is just a micro-optimization... Do you really need this ?
需要翻转结果。但我想这只是一个微优化......你真的需要这个吗?
回答by Brian
TreeSet::descendingSet
TreeSet::descendingSet
In Java 6 and later, there is a method on TreeSet
called descendingSet()
producing a NavigableSet
interface object.
在 Java 6 及更高版本中,有一种TreeSet
称为descendingSet()
生成NavigableSet
接口对象的方法。
public NavigableSet descendingSet()
The descending set is backed by this set, so changes to the set are reflected in the descending set, and vice-versa. If either set is modified while an iteration over either set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined.
The returned set has an ordering equivalent to
Collections.reverseOrder(comparator()). The expression s.descendingSet().descendingSet() returns a view of s essentially equivalent to s.
Specified by: descendingSet in interface NavigableSet<E> Returns: a reverse order view of this set Since: 1.6
公共 NavigableSet 降序集()
降序集合由该集合支持,因此对集合的更改反映在降序集合中,反之亦然。如果在对任一集合进行迭代时修改任一集合(通过迭代器自己的删除操作除外),则迭代结果未定义。
The returned set has an ordering equivalent to
Collections.reverseOrder(比较器())。表达式 s.descendingSet().descendingSet() 返回 s 的视图,本质上等同于 s。
Specified by: descendingSet in interface NavigableSet<E> Returns: a reverse order view of this set Since: 1.6
回答by Ashutosh
Reverse compare
逆转 compare
You can reverse the order of the two arguments in the compare
method of your Comparator
.
您可以扭转在两个参数的顺序compare
您的方法Comparator
。
TreeSet t = new TreeSet(new MyComparator());
{
class MyComparator implements Comparator
{
public int compare(Integer i1,Integer i2)
{
Integer I1=(Integer)i1;
Integer I2=(Integer)i2;
return I2.compareTo(I1); // return -I1compareTo(I2);
}
}
}