2

I want to go over the objects inside my TreeSet, I can do it by making an array using toArray but I don't need to go over all of the objects in the Set.

How can I go over the objects in the Set (starting with the first than the second etc') ?

And another small question about TreeSet : Can I keep the objects in the TreeSet sorted (so the first object will be with the smallest key etc') ?

edit: say I have a class myInt (with int myInteger) and I want to use it in TreeSet with a different oredering than the natural one, what do I need to define in my class (myInt) to do this ?

7
  • Looks like TreeSet keeps it sorted and it looks like first() will give you the smallest, where last() gives you the largest. You can also use iterators over any collection to go through the tree. Commented Oct 15, 2011 at 6:26
  • @Matt - TreeSet keeps it sorted according to what key ? Commented Oct 15, 2011 at 6:28
  • you can create a comparator or it sorts by it's natural ordering. download.oracle.com/javase/1.4.2/docs/api/java/util/… Commented Oct 15, 2011 at 6:28
  • You can supply your own comparator - thats what sorts it. If you use an object that implements comparable - that will be used. Commented Oct 15, 2011 at 6:30
  • @Fortyrunner - How can I change the comparator used by the Set ? Commented Oct 15, 2011 at 6:31

4 Answers 4

3

How can I go over the objects in the Set

The easiest way to iterate over the items of the set is like so:

SortedSet<T> set = new TreeSet<T>();
for (T elem : set) {
  // use elem
}

Can I keep the objects in the TreeSet sorted

TreeSet is automatically sorted, so you don't need to do anything.

I have a class MyInt (with int myInteger) and I want to use it in TreeSet with a different ordering than the natural one

You have two options:

Option 1: Make it implement Comparable<MyInt>:

public class MyInt implements Comparable<MyInt> {
   public int compareTo(MyInt o) {
     // return -1 if `this` is less than `o`
     //         0 if `this` is equal to `o`
     //         1 of `this` is greater than `o`
   }
}

Option 2: Supply a Comparator<MyInt> when constructing the TreeSet:

public class MyIntCmp implements Comparator<MyInt> {
  // implement compare() and equals() as per Comparator javadoc
}
SortedSet<T> set = new TreeSet<T>(new MyIntCmp());
Sign up to request clarification or add additional context in comments.

1 Comment

@Belgi: See the expanded answer.
1
for (MySetElementType element : mytreeset) {
}

TreeSet always keeps the objects sorted by how the objects compareTo (Comparable interface) is implemented. (Or you can pass a separate Comparator into the TreeSet constructor.)

Comments

0

Since the SetTree implements iterable you can use a regular for each loop or use the iterator directly. In the javadocs it says that it will iterate in ascending order.

http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html#iterator()

Comments

0
  1. The items inside TreeSet are automatically sorted according to their natural ordering if you are not giving your own comparator.
  2. Second thing you can define an Iterator to go through the TreeSet item directly without converting it to array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.