• 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

What is the impact of HashMap/TreeMap on Database queries?

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was asked this question in interview.

Can you guys tell me what would be the right answer to this questions?

1) when hashmap or treemap are implemented, do they effect database queries?
I said that we could cache the data into collections so that we can read data from memory instead of doing DB transacations. This would increase the performance of the system.

2) if you override an equals but not the hashcode and try to insert an object into a hashmap, what happens and what kind of excepetion would you get?"
 
Marshal
Posts: 79261
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see that using Maps would affect a database query; you have to run the query, obtain a ResultSet, then put the contents of that ResultSet into the Map.
If you override equals and not hashCode, or override then incorrectly (Google "Angelika Langer Java equals method" for some very useful information about that) then you can put your Objects into the Map and never find them again. You wouldn't expect an Exception (I don't think), only a null return value.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nilesh Raje wrote:1) when hashmap or treemap are implemented, do they effect database queries?
I said that we could cache the data into collections so that we can read data from memory instead of doing DB transacations. This would increase the performance of the system.


As Campbell said, you need to read from the database at least once. Every subsequent request could use the cached values.

It will affect performance, but I'm not sure if it will increase it. There is a trade-off between memory usage and execution speed. By caching results, your application will be faster for every request except the first one. You are storing the contents in memory though. In the end it all depends on various factors whether or not it will improve your application:
- if you will only make one request, there will be no increase in execution speed (there is no cache to use yet), but your memory footprint will increase
- if you make many many request, your data may become stale. The database can contain updated information that will never see its way through to your application.
- ???

In the end, you will have to test and determine the best strategy.

Campbell Ritchie wrote:You wouldn't expect an Exception (I don't think), only a null return value.


Right. The map will look in the wrong bucket, and can't find the object anymore. The API specifies that that should result in a return value of null.
 
The government thinks you are too stupid to make your own lightbulb choices. But this tiny ad thinks you are smart:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic