In a web application is Hashtable access faster or better than database access?

Luigi Viggiano

Hashtable (or Maps) is faster than a database access. Sometimes it's better to use hastable instead of db, sometimes not: it depends on what you have to do with the data.

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.
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!

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.