Reading from v / s Properties File HashMap Search

This is a more efficient way to access pairs (key, value) in terms of both memory and computation: reading from a properties file using .getProperty properties ("key") or loading the entire properties file in HashMap to the beginning of the program, and then searching key in HashMap?

In addition, if only one of the property values ​​is used, would it be better to store the value in a member variable and access it or look for it each time using properties.getProperty ("key")?

+4
source share
2 answers

properties.getProperty("key") is a search from a Hashtable that is a Properties object. When you execute Properties.load() , you load the records from the file and save them in the Properties object, which extends the Hashtable. Each subsequent access to the property searches the Hashtable. No more IO file.

Of course, accessing a member variable is slightly faster than accessing a value from a key in HashMap, but I doubt that it is here that you get something important in performance. HashMap search is O (1) and it is damned fast. You'll probably need millions of queries before you notice the difference. Do what is most readable to you.

+13
source

In fact, System.getProperty() uses the java.util.Properties object internally. The Properties class extends HashTable<Object,Object> , so sou is unlikely to get much performance by explicitly using the HashMap .

However, if you use several properties very often, this will certainly help if you save them in variables. A HashTable / HashMap search and variable access can be like O (1), but a HashTable search definitely has a much larger constant coefficient.

+1
source

Source: https://habr.com/ru/post/1414394/


All Articles