Chapter 17: java.util Part 1: The Collections Framework 445
ClassCastExceptionis thrown when an object is incompatible with the elements in the set.
ANullPointerExceptionis thrown if an attempt is made to use a null object and null is not
allowed in the set. AnIllegalArgumentExceptionis thrown if an invalid argument is used.
The Queue Interface
TheQueueinterface extendsCollectionand declares the behavior of a queue, which is often a
first-in, first-out list. However, there are types ofqueues in which the ordering is based upon
other criteria.Queueis a generic interface that has this declaration:
interface Queue<E>
Method Description
E ceiling(Eobj) Searches the set for the smallest elementesuch thate>=obj.If
such an element is found, it is returned. Other wise,nullis returned.
Iterator<E> descendingIterator( ) Returns an iterator that moves from the greatest to least. In
other words, it returns a reverse iterator.
NavigableSet<E> descendingSet( ) Returns aNavigableSetthat is the reverse of the invoking set.
The resulting set is backed by the invoking set.
E floor(Eobj) Searches the set for the largest elementesuch thate<=obj. If
such an element is found, it is returned. Other wise,nullis
returned.
NavigableSet<E>
headSet(EupperBound, booleanincl)
Returns aNavigableSetthat includes all elements from the
invoking set that are less thanupperBound. Ifinclistrue, then
an element equal toupperBoundis included. The resulting set is
backed by the invoking set.
E higher(Eobj) Searches the set for the largest elementesuch thate>obj. If such
an element is found, it is returned. Other wise,nullis returned.
E lower(Eobj) Searches the set for the largest elementesuch thate<obj. If
such an element is found, it is returned. Other wise,nullis
returned.
E pollFirst( ) Returns the first element, removing the element in the process.
Because the set is sor ted, this is the element with the least
value.nullis returned if the set is empty.
E pollLast( ) Returns the last element, removing the element in the process.
Because the set is sor ted, this is the element with the greatest
value.nullis returned if the set is empty.
NavigableSet<E>
subSet(ElowerBound,
booleanlowIncl,
EupperBound,
booleanhighIncl)
Returns aNavigableSetthat includes all elements from the
invoking set that are greater thanlowerBoundand less than
upperBound. IflowInclistrue, then an element equal to
lowerBoundis included. IfhighInclistrue, then an element
equal toupperBoundis included. The resulting set is backed
by the invoking set.
NavigableSet<E>
tailSet(ElowerBound, booleanincl)
Returns aNavigableSetthat includes all elements from the
invoking set that are greater thanlowerBound. Ifinclistrue, then
an element equal tolowerBoundis included. The resulting set is
backed by the invoking set.
TABLE 17-4 The Methods Defined byNavigableSet