views:

88

answers:

3

Which Java collection class can be used to maintain the entries in the order in which they were last accessed? Choose one answer.

a. java.util.HashSet

b. java.util.LinkedHashMap

c. java.util.Hashtable

d. java.util.Vector

e. None of these

A: 

You can try to use java.util.PriorityQueue for this matter

DixonD
that does not really answer the question (s)he posed. It's not one of the allowed answers (despite the fact that it might be a good answer).
lorenzog
@lorenzog How about answer "e. None of these"?
DixonD
@DixonD - that would be an incorrect answer, because one of the other options is correct; see @nil's answer.
Stephen C
+2  A: 

java.util.LinkedHashMap can be used to maintain the entries in the order in which they were last accessed

A special constructor is provided to create a linked hash map whose order of iteration is the order in which its entries were last accessed, from least-recently accessed to most-recently (access-order).

Other than this you can also try java.util.PriorityQueue.

nil
A: 

A hint: take a look at the APIs of each data structure; and see if, by using only the methods listed, you can get them to do what you need (i.e. insert N elements with a specific order, query for the k-th (where 0 <= k <= N), and see if you actually get the k-th element returned according to the order you inserted them) without too much code grumbling.

This will not give you a direct answer, and you might not even get a certain answer; but in doing so (and I mean, go write some code!) you will most certainly understand how the data structures work and which one is the best for you. Worst case scenario you can go back and read the textbook and actually have a clear idea of what they are talking about.

If, on the other hand, you are more inclined towards studying the theory first, take a look at this link: it's a Java Trail on the topic.

lorenzog
Note that the the questioner is NOT looking for insertion order. They are looking for last-accessed order.
GaryF
@GaryF: true, in fact my point was to tackle the problem from a similar -albeit not exactly the same- point of view, and go on from there. Perhaps I wasn't clear enough.
lorenzog