Properties keys() method in Java with Examples
Last Updated :
30 Sep, 2019
The keys() method of Properties class is used to get the enumeration of the keys in this Properties object. This enumeration can be used to traverse and iterate the keys sequentially.
Syntax:
public Enumeration keys()
Parameters: This method accepts no parameters
Returns: This method returns an Enumeration of the keys of this Properties object sequentially.
Below programs show the implementation of int keys() method.
Program 1:
import java.util.*;
public class GfG {
public static void main(String[] args)
{
Properties properties = new Properties();
properties.put( "Pen" , 10 );
properties.put( "Book" , 500 );
properties.put( "Clothes" , 400 );
properties.put( "Mobile" , 5000 );
System.out.println( "Properties: "
+ properties.toString());
Enumeration enu = properties.keys();
System.out.println( "The enumeration of keys are:" );
while (enu.hasMoreElements()) {
System.out.println(enu.nextElement());
}
}
}
|
Output:
Properties: {Book=500, Mobile=5000, Pen=10, Clothes=400}
The enumeration of keys are:
Book
Mobile
Pen
Clothes
Program 2:
import java.util.*;
public class GFG {
public static void main(String[] args)
{
Properties properties = new Properties();
properties.put( "Geeks" , 10 );
properties.put( "4" , 15 );
properties.put( "Geeks" , 20 );
properties.put( "Welcomes" , 25 );
properties.put( "You" , 30 );
System.out.println( "Current Properties: "
+ properties.toString());
Enumeration enu = properties.keys();
System.out.println( "The enumeration of keys are:" );
while (enu.hasMoreElements()) {
System.out.println(enu.nextElement());
}
}
}
|
Output:
Current Properties: {You=30, Welcomes=25, 4=15, Geeks=20}
The enumeration of keys are:
You
Welcomes
4
Geeks
References: https://docs.oracle.com/javase/9/docs/api/java/util/Properties.html#keys–
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...