• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Collections - suggest examples

 
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you are describing is random selection from a decreasing population.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, the Oracle/Sun tutorial is good on collections.
 
Uh oh, we're definitely being carded. Here, show him this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic