32

Here is the piece of code that I have used for 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.

Is there a more optimized way of doing it?

jps
  • 20,041
  • 15
  • 75
  • 79
Gaurav Saini
  • 744
  • 2
  • 11
  • 22
  • 2
    Flip the order of ```this``` and ```that``` in the overridden ```compareTo``` so items are inserted in the opposite order. – opyate Jan 24 '14 at 11:23

5 Answers5

43

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.

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.

Adamski
  • 54,009
  • 15
  • 113
  • 152
10

TreeSet::descendingSet

In Java 6 and later, there is a method on TreeSet called descendingSet() producing a NavigableSet interface object.

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
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Brian
  • 13,412
  • 10
  • 56
  • 82
7
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 ?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Pierre
  • 34,472
  • 31
  • 113
  • 192
3

Using descendingSet method you can reverse existing treeSet in the class

import java.util.TreeSet; 
  
public class TreeSetDescending { 
  
    public static void main(String[] args) 
    { 
        // Declare a treeset 
        TreeSet<Object> ints = new TreeSet<Object>(); 
        ints.add(2); 
        ints.add(20); 
        ints.add(10); 
        ints.add(5); 
        ints.add(7); 
        ints.add(3); 
  
        // Initialize treeset with predefined set in reverse order 
        // using descendingSet() 
        TreeSet<Object> intsReverse = (TreeSet<Object>)ints.descendingSet(); 
  
        // Print the set 
        System.out.println("Without descendingSet(): " + ints); 
        System.out.println("With descendingSet(): " + intsReverse); 
    } 
} 
Anupam Haldkar
  • 985
  • 13
  • 15
1

Reverse compare

You can reverse the order of the two arguments in the compare method of your 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);
        }
}
  }
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Ashutosh
  • 11
  • 1