This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Which one is better? Hash Map or Properties File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Which one is better? Hash Map or Properties File" Watch "Which one is better? Hash Map or Properties File" New topic
Author

Which one is better? Hash Map or Properties File

Santhosh Gowd
Greenhorn

Joined: Nov 28, 2011
Posts: 13
I have a set of key value pairs. which one is better to use? Hash map or properties file and why? I need to use these key value pairs in a single java class only and the set is a bit large.
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
since your requirement seems to be only to support a large set of data I guess it would be
easier to put in to a properties file. What I am thinking is that coding all of these values will
be very tedious. However there may be many ways how this can be accomplished.


Also if these values need to be changed from time to time properties file could be the way to go.
If these values should not be changed easily, putting in a property file may not be good.
Santhosh Gowd
Greenhorn

Joined: Nov 28, 2011
Posts: 13
no, i do not need to change these values. the values remain static. Any other way to go? I am processing this data while navigating between two jsp pages.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

If you have a set of key-value pairs then HashMap is the obvious thing to use. If those pairs have to come from an external file where they live permanently, then Properties would be the thing to use, but it sounds like that isn't the case. So use HashMap.

If you use Properties then readers of your code are going to assume that your data comes from an external file, because that's what Properties is for. So if your data doesn't come from an external file, then don't use Properties or you are going to confuse people.
Santhosh Gowd
Greenhorn

Joined: Nov 28, 2011
Posts: 13
This is my situation- I am navigating between to jsps, and i am calling a method there by a class in between. This method uses a fixed set of key-value pairs. So every time i should initialize this hash map, just like another variable. So this wouldn't be an overhead? as the set of values is a bit large. How does the property file works? It is also loaded each time?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

If the values won't change, loading them for each individual request is rather ridiculous. Employ a context listener to create the map once at startup, and store it in the application context where it can be accessed by all web app resources.

Whether you load it from a properties file or in code is rather moot. Choose which approach makes the most sense for code clarity.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Santhosh Gowd
Greenhorn

Joined: Nov 28, 2011
Posts: 13
wow that really helps. Thanks! @Bear.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Which one is better? Hash Map or Properties File
 
Similar Threads
how to remove duplicate values in hash map?
Multiple set of data mapping
How do I slim down a class full of get...() and set...()
Storing key-value pairs: Better apporaches for storing and reading?
Difference Between hashmap and Property class