File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need ideas for Collections

 
kwame Iwegbue
Ranch Hand
Posts: 197
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33674
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kwame,
You could nest two HashMaps. The outer by date and the inner by the other key.
 
Stuart Ash
Ranch Hand
Posts: 637
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Stuart Ash
Ranch Hand
Posts: 637
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 197
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
kwame Iwegbue
Ranch Hand
Posts: 197
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic