10

Is there any way to access the successor or the predecessor of an element in Treeset of Java. Or is there any way of getting the iterator of a particular element so that we can use iterator.next() to get the next element.

kaushalpranav
  • 1,725
  • 24
  • 39

2 Answers2

10

use higher/lower

with the TreeSet set

is set.higher(e) like e.next() and set.lower(e) like e.prev()

Turo
  • 4,724
  • 2
  • 14
  • 27
1

You can get a particular TreeSet's iterator using the iterator() method, and you can retrieve a reverse iterator with the use of descendingIterator() method.

masm64
  • 1,222
  • 3
  • 14
  • 31