This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes need ideas for Collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "need ideas for Collections" Watch "need ideas for Collections" New topic
Author

need ideas for Collections

kwame Iwegbue
Ranch Hand

Joined: Sep 02, 2000
Posts: 197
i'm writing a program that
1. gets information in form of question; answer; date (daily from a website)
2. stores this in a manner that is accessible by choosing date

(I am able to implement first part of this )but how can i save this info, since it seems that HashMap, HashTable etc only stores key/value pairs.


Who dares, wins! (SAS motto)
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30068
    
149

Kwame,
You could nest two HashMaps. The outer by date and the inner by the other key.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
In Object-Oriented programming, every problem has the same solution: create a class.

You can create a beany class representing the attributes you now have, implement the canonical methods, and store instances in an ArrayList.

Tomorrow, if you need to use a fourth attribute (say, location), you can more easily extend the bean than change the nested hashmap implementation.



ASCII silly question, Get a silly ANSI.
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
Originally posted by Stuart Ash:
In Object-Oriented programming, ALMOST every problem has the same solution: create a class.

, and store instances in an ArrayList.




Actually, even an array might do.
kwame Iwegbue
Ranch Hand

Joined: Sep 02, 2000
Posts: 197
thanks for your response.
being new to programming in general, can some explain to me why use databases, sql etc, if you can store information in collections?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

It depends on how long you need to keep the information, and how many people need access to the same information, and If that information needs to be shared outside of the Java App, and even accross multiple JVMs, RMI not withstanding.

So in a Collection the information is only in memory for so long as the Collection is referenced and in scope. In a database it is stored for longer than you apps lifecyle, or most anything.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
kwame Iwegbue
Ranch Hand

Joined: Sep 02, 2000
Posts: 197
wouldn't it be possible to save to Collection's state to a file on exit from the application, and have the file continuosly updated.
(...I'm reaching here guys...trying to avoid learning about databases right now!)
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by kwame Iwegbue:
wouldn't it be possible to save to Collection's state to a file on exit from the application, and have the file continuosly updated.
(...I'm reaching here guys...trying to avoid learning about databases right now!)


Yes it IS possible. And for small programs it is even feasible. However, as the amount of data you need to store grows larger, this solution does not scale well. What I mean is that a so called "flat file" works fine for perhaps a few hundred elements from the Collection. However, if you need to store millions of elements and do various searches on them, performance is significantly degraded. Databases are much more efficient at searching through large amounts of data.

Layne


Java API Documentation
The Java Tutorial
 
wood burning stoves
 
subject: need ideas for Collections
 
Similar Threads
Hibernate Date auto Generation
how to format text from 01/12/1999 to 01121999 ?
Date or Timestamp?
java.util.Date does it contain information about hours?
Front end EJB ..release date?