Java Reference
In-Depth Information
a ListIterator lets you look ahead or look back in a List . if you use its next() method followed by the
previous() method, the iterator goes back to the same position. the call to the next() method moves it one index
forward and the call to the previous() method moves it one index backward.
Tip
Working with Queues
A queue is a collection based on the notion of a real-world queue. A queue is a collection of objects on which some
kind of processing is applied one element at a time. A queue has two ends known as head and tail. In the simple
queue, objects are added to the tail and removed from the head; the object added first will be removed first. However,
queues can be categorized based on the way it allows insertion and removal of its elements. In this section, I will
discuss the following types of queues:
simple queue allows insertion at the tail and removal from the head.
A
priority queue associates a priority with every element of the queue and allows the element
with the highest priority to be removed next from the queue.
A
delay queue associates a delay with every element of the queue and allows for the removal of
the element only when its delay has elapsed.
A
doubly ended queue allows for insertion and removal of its elements from the head as well as
the tail.
A
blocking queue blocks the thread that adds elements to it when it is full and it blocks the
thread removing elements from it when it is empty.
A
transfer queue is a special type of blocking queue where a handoff of an object occurs
between two threads (a producer and a consumer).
A
blocking doubly ended queue is a combination of a doubly ended queue and a blocking
queue.
A
Simple Queues
Simple queues are represented by an instance of the Queue interface. Typically, you hold a group of objects in a queue
for some kind of processing that is applied to one element at a time. For example, the line of customers at a counter in
a post office is an example of a queue. You can classify a queue based on many criteria.
How many elements can a queue hold? Sometimes you have an unlimited (at least theoretically) number
of elements in a queue, and sometimes it has a predefined number of elements. When the length of a queue is
unlimited, it is called an unbounded queue . When the length of the queue is predefined, it is called a bounded queue .
The bound of a queue defines its behavior when an element is added to a full bounded queue. Attempting to add an
element to a full queue may throw an exception; it may fail silently; it may wait indefinitely (or for a predefined time
period) for the queue to have room to accommodate the new element, etc. The exact behavior depends on the type of
the queue.
Which element of the queue comes out next? A queue always has an entry point and an exit point for its
elements. The exit point is called the head of the queue and the entry point is called the tail . The head and the tail
may be the same. If the head and the tail of a queue are the same, it is called a Last In, First Out ( LIFO ) queue. A LIFO
queue is also known as a s tack . The head and the tail of a queue may be different. If a queue follows a rule that the
element entering the queue first will leave the queue first (first come, first served rule), it is called a First In, First Out
( FIFO ) queue. Have you ever had a chance to stand in a queue for a long time and as soon as your turn comes, another
 
 
Search WWH ::




Custom Search