Dhaval Solanki
Dhaval Solanki

Reputation: 115

Reading Properties file vs HashMap Java

In my application I have defined around 1,00,000 values in properties file. Currently I am copying all data in map and accessing it. Is it a good way or we can directly read values from properties file?

Upvotes: 0

Views: 2323

Answers (4)

Hasnain Ali Bohra
Hasnain Ali Bohra

Reputation: 2180

There is a lot difference between the HashMap and propertyFile processing. Let me clear you by example :-

suppose you have 1 million key value pair entries to process. Behavior of HashMap :-

  1. HashMap :- If you are searching for a particular key in the HashMap and you have implemented correct hashcode() and equals() contract then by the hashing the getting and setting key, value will be fast as it will use hashing and red black tree indexing for searching.

    cons :- you have to initialize the HashMap i.e. reading every value from File and put it to the HashMap.

  2. PropertyFile :- If you look at the internal implementation of PropertyFile.java it internally uses the HashTable.

    public class Properties extends Hashtable<Object,Object> So once you loaded the property file into the object. The performance comparison between them is same as HashTable vs. HashMap performance.

Upvotes: 1

Aris2World
Aris2World

Reputation: 1234

The strategy depends on the requirements and constraints you have. Based on the two dimensions (requirement: speed, constraint: memory) this can be a list of possible solutions:

  1. speed and no constraints: read the whole properties in a map
  2. no requirements and limited memory: read from the file
  3. speed and limited memory: use a cache to read an amount of properties which is a trade-off between speed and memory used. The trade-off depends on an empirical evaluation considering:
    • how the properties are used by the application?
    • which eviction policy is better to use? (answer to this question is wired to the previous one)

Speed and memory belong to the system area. There could be other requirements or constraints that belong to the application area, for example: properties are read only or they can be written by someone so you need to reload them when necessary; once the applications started and is configured with this properties it has no more access to the source of the properties.

So there are many good solutions but no one that fits all the situations. If you are in doubt start with the simple solution and then when a requirement is clear or a constraint appear move to another that fits.

Upvotes: 0

Nicolas
Nicolas

Reputation: 177

Accessing a file on the hard drive takes a lot of time (worst if that is a network hard drive). Loading a large file into a map structure uses a certain amount of memory. Once your files is loaded in your map, accessing data should be really faster than read them from a file each time. If you dispose enough memory, it is better to use map.

Upvotes: 0

SharpLu
SharpLu

Reputation: 1214

I think you can direct read properties file from Java. Examples

https://www.mkyong.com/java/java-properties-file-examples/

By the way, If you read about 1,00,000 values that will increase the IO of your application. Suggest you use HashMap instead of fetching values from properties files each time.

Upvotes: 0

Related Questions