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 ?
The easiest way to iterate over the items of the set is like so:
TreeSetis automatically sorted, so you don’t need to do anything.You have two options:
Option 1: Make it implement
Comparable<MyInt>:Option 2: Supply a
Comparator<MyInt>when constructing theTreeSet: