Javatpoint Logo
Javatpoint Logo

Java SortedSet<E> interface

A set is used to provide a particular ordering on its element. The elements are ordered either by using a natural ordering or by using a Comparator. All the elements which are inserted into a sorted set must implement the Comparable interface.

The set's iterator will traverse the set in an ascending order. Several other operations are provided in order to make best use of ordering. All the elements must be mutually comparable.

Methods

comparator() Returns the comparator which is used to order the elements in the given set. Also returns null if the given set uses the natural ordering of the element.
first() Returns the first element from the current set.
headSet(E toElement) Returns a view of the portion of the given set whose elements are strictly less than the toElement.
last() Returns the reverse order view of the mapping which present in the map.
spliterator() Returns a key-value mapping which is associated with the least key in the given map. Also, returns null if the map is empty.
subSet(E fromElement, E toElement) Returns a key-value mapping which is associated with the greatest key which is less than or equal to the given key. Also, returns null if the map is empty.
tailSet(E fromElement) Returns a view of the map whose keys are strictly less than the toKey.

Example 1

Test it Now

Output:

The list of elements is given as:
Audi
BMW
Baleno
Mercedes
The first element is given as: Audi
The last element is given as: Mercedes
The respective element is given as: [Audi, BMW]
The respective element is given as: [Audi, BMW, Baleno, Mercedes]






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA