aspose file tools*
The moose likes Beginning Java and the fly likes Collections -  suggest examples Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Collections -  suggest examples " Watch "Collections -  suggest examples " New topic
Author

Collections - suggest examples

Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

I want to make simple/small programs that are mini-versions of real-life applications that use Collections. This is to practice and remember the chapter by DOING.

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 :

1- ArrayList
2- LinkedList
3- HashSet
4- LinkedHashSet
5- TreeSet
6- SortedMap
7- HashMap
8- TreeMap
9- LinkedHashMap
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.



SCJP 6. Learning more now.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39877
    
  28
Start by creating your own Stack interfaceCreate classes like thisDo the same for a Queue<E> interface, and implement it with a doubly-linked list, an array which changes in size, and a "circular" array.
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.
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

thanks, can you also give me ideas/word problems (ie without codes) that i will have to implement as a "project" ? Like, make me an english dictionary of 20 words, telephone directory of name and number etc ?

regards
rb

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39877
    
  28
You're welcome You can use a stack for reversing things. You can mimic a compiler by putting values onto the stack, and try arithmetic
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Can i get some examples/problems for each type of Data Structure (DS) i mentioned in the original post. I came up with one use of an ArrayList, accidentally :

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.
rb.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39877
    
  28
I thought you had got it all worked out . . .
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39877
    
  28
What you are describing is random selection from a decreasing population.
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Campbell Ritchie wrote:I thought you had got it all worked out . . .

its taking time...but i am making some progress. The sun tuts seem to be ok as of now...

regards
rb
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39877
    
  28
Yes, the Oracle/Sun tutorial is good on collections.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collections - suggest examples