Java Reference
In-Depth Information
a set when you need a collection where duplicates are not allowed. Attempts to add a
duplicate element to a set are ignored. For example, suppose that you need to write an
application that searches a book for keywords and keeps track of the page numbers where
the keywords appear. After a page number is found, add it to the set. If a keyword appears
twice on a page, adding the page number again to the set has no effect, which is the desired
behavior in this situation.
Figure 7. 3 shows the Set interfaces and their implementing classes.
FIGURE 7.3 The Set interfaces and their implementing classes
interface
Set
class
HashSet
interface
SortedSet
class
LinkedHashSet
interface
NavigableSet
class
TreeSet
The different set classes each provide their own unique functionality:
HashSet A set that stores its elements in a hash table. There is no ordering to the items,
and HashSet uses the hashCode method of its elements to determine their placing in the set.
LinkedHashSet A set that stores its elements in a linked list hash table. The items are
hashed based on their hashCode and also ordered in a doubly linked list.
TreeSet A set that stores its elements in a tree data structure that is also sorted and navi-
gable. The add , remove , and contains methods are guaranteed to work in log( n ) time,
where n is the number of elements in the tree.
The basic operations of Set include the ability to add or remove a single element or
collection of elements, and to perform unions and intersections.
Queues
A queue is a collection whose elements are added and removed in a specifi c order. Queues
are typically used for storing elements prior to processing them. For example, suppose you
have an order processing application that places new orders into a queue. The warehouse
could retrieve the order from the queue to fulfi ll the order, and the billing department could
retrieve the order from the queue to collect payment. Queues typically process elements in a
FIFO behavior, but the actual behavior depends on the type of queue you are using.
 
Search WWH ::




Custom Search