SortedSet Interface - Walking Techie

Blog about Java programming, Design Pattern, and Data Structure.

Saturday, November 26, 2016

SortedSet Interface

The SortedSet interface extends Set interface and declare the behavior of a set sorted in ascending order.

SortedSet is a generic interface that has this declaration:

public interface SortedSet<E> extends Set<E>

Here, E specifies the type of objects that the list will hold.

In addition to the methods defined by Set, SortedSet defines some of its own method.

Method Description
Comparator<? super E> comparator( ) Returns the invoking sorted set’s comparator. null is returned, if this set uses the (comparable) natural ordering of its elements.
E first( ) Returns the first element in the invoking sorted set.
SortedSet<E> headSet(E end) Returns a SortedSet containing those elements less than end that are contained in the invoking sorted set. Returned sorted set is also referenced by the invoking sorted set.
E last( ) Returns the last element in the invoking sorted set.
SortedSet<E> subSet(E start, E end) Returns a SortedSet that includes those elements between start and end–1. Returned sorted set is also referenced by the invoking sorted set.
SortedSet tailSet(E start) Returns a SortedSet that contains those elements greater than or equal to start that are contained in the sorted set. Returned sorted set is also referenced by the invoking sorted set.

Most Commonly thrown Exceptions in SortedSet

Exception Description
NoSuchElementException when no items are contained in the invoking set.
ClassCastException occurs when an attempt is made to add an incompatible object in a set.
NullPointerException will throw when you try to store a null object and null element is not allowed in the set.
IllegalArgumentException will throw when an invalid argument is used.

No comments :

Post a Comment