Reputation: 2884
I saw the following question in the exam but I could not come up with a good real scenario: can anyone explain why individual might use more than one data structure to organize the same data set?
Upvotes: 0
Views: 65
Reputation: 83253
Sure, a linked hash table (e.g. java.util.LinkedHashMap) combines a linked list and a hash table.
The linked list keeps the order of the keys, and the hash table allows for constant lookup by key.
Further explanation:
Suppose you need a data structure which has fast lookup, insert, and delete by key. Suppose also that you wanted to keep the order of when elements were inserted.
For example, you run a business that receives requests. You often add new requests, remove requests, and look up requests by the request id. You also will frequently list the requests in order of when they were received.
A hash table allows fulfills the first requirement. The linked list fulfills the second. A linked hash table fulfills both.
If you need even more explanation, this should a least give you a good start on your Google search.
FYI, I once read a quote from someone (I wish I could remember who), who said (more or less) if he had to live on a desert island and could only take one data structure with him, he would take a linked hash table.
Upvotes: 3