Reputation: 115
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
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 :-
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
.
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
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:
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
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
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