Get tailset from TreeSet in Java

In this tutorial, we will learn how to Get tailset from TreeSet in Java.

TreeSet in Java extends AbstractSet and implements the NavigableSet interface. It creates a collection that uses a tree for storage. The objects are stored in sorted, ascending order. Access and retrieval times are quite fast, which makes TreeSet an excellent choice when storing large amounts of sorted information that must be found quickly. TreeSet is similar to HashSet, the difference is that TreeSet sorts the elements in ascending order and doesn’t allow null elements.

TreeSet class in Java provides tailSet() method to get tailset from TreeSet. The syntax of tailSet() method is:

Syntax of tailSet() method:

fromElement – index(inclusive) from which tailsetto be obtained

returns a tailset whose elements are greater than or equal to fromElement

ClassCastException – if fromElement is not compatible with this set’s comparator.
NullPointerException – if fromElement is null and this set uses natural ordering, or its comparator does not permit null elements
IllegalArgumentException – if this set itself has a restricted range, and fromElement lies outside the bounds of the range




Related posts

Leave a Comment