For example, i had been given an example before as shown below :
Matthew Brown wrote:A Map is like a directory, that contains entries in pairs. Each pair is made up of a "key" and an "object", and given a key you can look up the associated object.
Think of a telephone directory. It contains names associated with phone numbers. If you know someones name, it's easy to find the phone number. If you were writing that in code, you'd probably use a Map.
Can anyone suggest real-life examples or even theoretical ones which make use of the following, preferably one at a time :
10- PriorityQueue ......etc
PS : I feel that such a question is good for an interview because it will test the ability to apply the theory. Just knowing the text-book definitions and methods is not enough.
The exceptions mentioned can be found in the java.util package, or you can create your own.
You will probably find those examples easier to implement at first; leave Maps until later.
To select a random number from a desired range ONLY once (eg any num between,say 50 -700). I put my numbers in an ArrayList "AL" . Then i select an index of "AL" using Math.random(). Get the number at that index , store it in a temporary variable before i remove() it from "AL". Then, i repeat the process till AL isEmpty(). IMHO, this seems to be a useful real-world application. Problem is I cant come up with more like these as of now...programmers block, i guess
Can anyone suggest more APPLICATIONS for other data structures ? Like if its a telephone directory - use DS xyz, if its a dictionary use DS abcd...etc...
any clues are welcome.
Address books and telephone directories can be worked out with Maps. You can find hundreds of uses for Lists and Sets. If you want a LIFO collection, use a Stack as above, and for FIFO use a Queue.
[LIFO and FIFO are explained in the Queue link.]
It is not so much a case of thinking what the collections are used for, as a case of asking, "What have I got to store?" and thinking how to store it.