Menu
Topics Index
...
`

PropertiesDemo - Java Example Program


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");

© meritcampus 2019

All Rights Reserved.

Open In App