Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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: 74755
336
  • 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: 22579
122
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.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic