2

I have a huge file which contains lines in key=value format. If i wish to get the value of a particular key using getProperty() method of Properties class in java , is the complete file loaded into memory before the getProperty() operation is performed?

I have read that Properties class is a HashTable implementation java. So I would like to know if the entire properties file is loaded into a HashTable even to fetch the value of a single property using Properties Class.

vaultah
  • 44,105
  • 12
  • 114
  • 143
sujith
  • 665
  • 2
  • 9
  • 22
  • 1
    Yes, the complete Table is loaded when you call the load method. The load method basically goes through each line and splits it using the '=' char and stores the value in front and behind the '=' in a Map. – Distjubo Sep 03 '15 at 14:42

1 Answers1

2

TL;DR: The entire file is loaded into memory

java.util.Properties is not a HashTable implementation, it is a HashTable. i.e. it is an in-memory hash based lookup.

From the source code you can see the implementation of getProperty simply delegates to super.get which is HashTable.get:

public String getProperty(String key) {
    Object oval = super.get(key);
    String sval = (oval instanceof String) ? (String)oval : null;
    return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval;
}

The load method reads the properties file (.properties or XML) into the HashTable.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166