java.util.TreeSet.headSet() Method

Description

The headSet(E toElement,boolean inclusive) method is used to return a view of the portion of this set whose elements are less than (or equal to, if inclusive is true) toElement. The returned set is backed by this set, so changes in the returned set are reflected in this set, and vice-versa.

Declaration

Following is the declaration for java.util.TreeSet.headSet() method.

public NavigableSet<E> headSet(E toElement,boolean inclusive)

Parameters

toElement--This is the high endpoint of the returned set.

inclusive--This is true if the high endpoint is to be included in the returned view.

Return Value

The method call returns a view of the portion of this set whose elements are less than (or equal to, if inclusive is true) toElement.

Exception

ClassCastException--This is thrown if toElement is not compatible with this set's comparator.

NullPointerException--This is thrown if toElement is null and this set uses natural ordering, or its comparator does not permit null elements.

IllegalArgumentException--This is thrown if this set itself has a restricted range, and toElement lies outside the bounds of the range.

Example

The following example shows the usage of java.util.TreeSet.headSet() method.