Properties Class in Collection Framework

java.util.Properties;
public class Properties extends Hashtable<Object,Object>
Properties is a subclass of Hashtable. The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. It is used to maintain lists of values in which the key is a String and the value is also a String.
Properties class features:
  1. A Properties cannot contain duplicate key.
  2. It neither cantain null key nor null value .
  3. It is used to maintain lists of values in which the key is a String and the value is also a String.
  4. It is synchronized.
  5. It does not guarantee that the insertion order will remain constant over time.
  6. hashCode() and equals() methods is used for its elements .
  7. Data structure is based on Hash Table .
Constructor of Properties
  1. Properties()
              It is used to create an empty property list with no default values.
  2. public Properties(Properties defaults)
              It is used to create an empty property list with the specified defaults.
Method of Properties class
  1. String getProperty(String key)
              Returns the value associated with key. A null object is returned if key is neither in the list nor in the default property list.
  2. String getProperty(String key, String defaultProperty)
              Returns the value associated with key. defaultProperty is returned if key is neither in the list nor in the default property list.
  3. void list(PrintStream streamOut)
              Sends the property list to the output stream linked to streamOut.
  4. void list(PrintWriter streamOut)
              Sends the property list to the output stream linked to streamOut.
  5. void load(InputStream streamIn) throws IOException
              Inputs a property list from theinput stream linked to streamIn.
  6. Enumeration propertyNames( )
              Returns an enumeration of the keys. This includes those keys found in the default property list, too.
  7. Object setProperty(String key, String value)
              Associates value with key. Returns the previous value associated with key, or returns null if no such association exists. (Added by Java 2,
    for consistency.)
  8. void store(OutputStream streamOut, String description)
              After writing the string specified by description, the property list is written to the output stream linked to streamOut. (Added by Java 2.)
Example of Properties class

import java.util.Set; import java.util.Properties; import java.util.Iterator; public class PropertiesDemo { public static void main(String args[]) { Properties capitals = new Properties(); Set states; String str; capitals.setProperty("Bihar", "Patna"); capitals.setProperty("U.P.", "Lucknow"); capitals.setProperty("Maharastra", "Mumbai"); capitals.setProperty("Himanchal Pradesh", "Shimla"); // Show all states and capitals in hashtable. states = capitals.keySet(); // get set-view of keys Iterator itr = states.iterator(); while(itr.hasNext()) { str = (String) itr.next(); System.out.println("The capital of " + str + " is " + capitals.getProperty(str) + "."); } System.out.println(); } }
Output

The capital of Maharastra is Mumbai.
The capital of U.P. is Lucknow.
The capital of Himanchal Pradesh is Shimla.
The capital of Bihar is Patna.

Download this example
 



 

Latest Tutorial


Most Visited Tutorial