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.
HashSet does not maintain any order while LinkedHashSet maintains insertion order of elements much like List interface and TreeSet maintains sorting order or elements.
Objects in a TreeSet are stored in a sorted and ascending order. TreeSet does not preserve the insertion order of elements but elements are sorted by keys.
last() method is used to return the last of the element of a TreeSet. The last element here is being referred to the highest of the elements in the set. If the elements are of integer types, then the largest integer is returned.
use higher/lower
with the TreeSet set
is set.higher(e) like e.next() and set.lower(e) like e.prev()
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With