Java Reference
In-Depth Information
The program creates a TransferQueue and adds five elements to it. It creates and starts a producer and a
consumer. Its output needs a little explanation. You added five elements initially to make sure the consumer will have
some elements to consume from the TransferQueue when the producer tries to transfer an element. The producer
got the first go. It puts the integer 6 into the queue. The consumer removed the integer 1 from the queue. At this time,
the producer tried to hand off the integer 7 to the consumer, leaving five elements (2, 3, 4, 5, and 6) still queued in
the TransferQueue . The consumer must remove all these elements from the TransferQueue , before it will accept
the transfer request for the integer 7 from the producer. This is evident from the output. The consumer removes the
elements 2, 3, 4, 5, and 6, and then the element 7. Both the producer and the consumer run in infinite loops. You need
to stop the program manually.
Blocking Doubly Ended Queues
A blocking, doubly ended queue provides the functionality of a doubly ended queue and a blocking queue. An
instance of the BlockingDeque interface represents a blocking, doubly ended queue. It inherits from the Deque and
BlockingQueue interfaces. It adds eight more methods to add and remove elements from the head and the tail. These
methods block indefinitely or for a specified amount of time, as in the case of a BlockingQueue . The new methods are
putXxx() , offerXxx() , takeXxx() , and pollXxx() , where Xxx is First or Last . The method with the suffix First is
used to put or take an element from the head of the Deque , whereas the method with the suffix Last is used to put or
take an element from its tail. Please refer to the “Double Ended Queues” and “Blocking Queue” sections described
earlier in this chapter for more details on using these methods.
The LinkedBlockingDeque class is an implementation class for the BlockingDeque interface. It supports bounded
as well as unbounded blocking deque.
Working with Maps
A map represents a type of collection that is different from the collections that you have seen so far. It contains
key-value mappings. It is easy to visualize a map as a table with two columns. The first column of the table contains
keys; the second column contains the values associated with the keys. Table 12-5 shows person names as keys and
their phone numbers as values. You can think of this table representing a map that contains mapping between names
and phone numbers. Sometimes a map is also known as a dictionary . In a dictionary, you have a word and you look
up its meanings. Similarly, in a map, you have a key and you look up its value.
Table 12-5. A Table with Two Columns, Key and Value. Each Row Contains a Key-Value Pair.
Key
Value
John
(342)113-9878
Richard
(245)890-9045
Donna
(205)678-9823
Ken
(205)678-9823
If you still have problem visualizing a map, you can think of it as a collection in which each element represents
a key-value pair as <key, value> . A <key, value> pair is also known as an entry in the map. The key and the value
must be reference types. You cannot use primitive types ( int , double , etc.) for either keys or values in a map.
A map is represented by an instance of the Map<K,V> interface. The Map interface is not inherited from the
Collection interface. A Map does not allow any duplicate keys. Each key is mapped to exactly one value. In other words,
each key in a Map has exactly one value. Values do not have to be unique. That is, two keys may map to the same value.
 
 
Search WWH ::




Custom Search