Menu
Topics Index
...
`


Collections Framework > Legacy Classes and Interfaces >
Siva Nookala - 20 Feb 2017
Properties is a subclass of Hashtable.In Properties class both key and values are Strings.The Properties class is used by many other Java classes.

Properties defines the following instance variable
Properties defaults
This variable holds default property list associated with a Properties object.
Properties defines following two constructors
  • Properties()
  • Properties(Properties propDefault)
    ConstructorsDescription
    Properties()It creates an empty Properties object .
    Properties(Properties <cw>propDefault)It creates an object that uses propDefault for its default values.
    In above two cases Properties should be empty.
    Properties defines following methods apart from the Hashtable.
    MethodDescription
    String getProperty(String key)It returns the value associated with key. A null object is returned if key is neither in the list nor in the default property list.
    String getProperty(String key, String defaultProperty)It returns the value associated with key, defaultProperty is returned if key is neither in the list nor in the default property list.
    void list(PrintStream streamOut) It sends the property list to the output stream linked to streamOut.
    void list(PrintWriter streamOut)Sends the property list to the output stream linked to streamOut.
    void load(InputStream streamIn) throws IOExceptionInputs a property list from the input stream linked to streamIn.
    Enumeration propertyNames( )It returns an enumeration of the keys. This includes those keys found in the default property list, too.
    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.
    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.
    PropertiesDemo
    import java.util.*;

    class PropertiesDemo
    {
        public static void main(String arg[])
        {
            Properties colours = new Properties();
            Set fruits;
            colours.put("Mango", "Yellow");
            colours.put("Grapes", "Green");
            colours.put("Orange", "Orange");
            colours.put("Apple", "Red");
            fruits = colours.keySet(); // LINE A
            for(Object frt : fruits)
            {
                System.out.println("The colour of " + frt + " is " +             colours.getProperty((String) frt) + ".");
            }
            System.out.println();
            String str = colours.getProperty("Apple", "Not found"); // LINE B
            System.out.println("The colour of Apple is " + str + ".");    
        }
    }
    OUTPUT

    The colour of Mango is Yellow.
    The colour of Orange is Orange.
    The colour of Grapes is Green.
    The colour of Apple is Red.

    The colour of Apple is Red.

    DESCRIPTION

    Here first it creates Properties list in which the keys are the names of fruits and the values are the colours of fruits.At LINE A we can get set-values of keys. Look for LINE B not in list specify default value. If we run the program we can get the properties key and values but not default value.

    THINGS TO TRY
    • Remove the below code in above program, and check the output.
      colours.put("Apple", "Red");
  • 0
    Wrong
    Score more than 2 points

    © meritcampus 2019

    All Rights Reserved.

    Open In App