In a web application is Hashtable access faster or better than database access?
Created May 4, 2012
Hashtable (and Maps) is faster than a database because the data are already loaded in memory, and generally in a way to get it as faster as possible (hash functions etc.). When getting data from the db, commonly you are passing thru a network connection, then to a file system, and at last to (flat)memory.
Disadvantages of maps are that them charge lot of memory (more than just data size): you cannot use map with huge structures if you don't want to keep it all in memory.
Databases let you to keep in memory only a subset of the entire application data. Furthermore, Hashtables (and Maps) don't provide a persistent storage: when you turn off the machine all the data are lost!
Hashtable are not transactional, Hashtable are not relational, don't provide any data-constraints... in short, Hashtables and Maps are different from databases.
Maps are right for keep application configuration (Properties), for keeping little tables (like a subset cities, states, etc.) that are used very frequently and changed rarely. And so on...
Take in mind that with Java 2 it's preferable to use HashMap (or Map's sub-classes) instead of Hashtable. This is because of design changes in Java collection framework.
For keeping temporary data in an application and for keeping fast access data, Maps (and Hashtables) are the right choice.
For keeping businness/persistent/structured data, databases are right!