Properties clear() method in Java with Examples

The Java.util.Properties.clear() method is used to remove all the elements from this Properties instance. Using the clear() method only clears all the element from the Properties and does not delete the Properties. In other words, we can say that the clear() method is used to only empty an existing Properties.

Syntax:

public void clear()

Parameters: The method does not take any parameter



Return Value: The function does not returns any value.

Below programs illustrate the Java.util.Properties.clear() method.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code illustrating clear() method
  
import java.util.*;
  
class PropertiesDemo {
    public static void main(String arg[])
    {
        Properties gfg = new Properties();
        Set URL;
        String str;
  
        gfg.put("ide",
                "ide.geeksforgeeks.org");
        gfg.put("contribute",
                "contribute.geeksforgeeks.org");
        gfg.put("quiz",
                "quiz.geeksforgeeks.org");
  
        // checking what's in table
        System.out.println("Current Properties: "
                           + gfg.toString());
  
        System.out.println("\nClearing the Properties");
        gfg.clear();
  
        // checking what's in table now
        System.out.println("New Properties: "
                           + gfg.toString());
    }
}

chevron_right


Output:

Current Properties: {contribute=contribute.geeksforgeeks.org, quiz=quiz.geeksforgeeks.org, ide=ide.geeksforgeeks.org}

Clearing the Properties
New Properties: {}

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code illustrating clear() method
  
import java.util.*;
  
class PropertiesDemo {
    public static void main(String arg[])
    {
        Properties gfg = new Properties();
        Set URL;
        String str;
  
        gfg.put(1, "Geeks");
        gfg.put(2, "GeeksforGeeks");
        gfg.put(3, "Geek");
  
        // checking what's in table
        System.out.println("Current Properties: "
                           + gfg.toString());
  
        System.out.println("\nClearing the Properties");
        gfg.clear();
  
        // checking what's in table now
        System.out.println("New Properties: "
                           + gfg.toString());
    }
}

chevron_right


Output:

Current Properties: {3=Geek, 2=GeeksforGeeks, 1=Geeks}

Clearing the Properties
New Properties: {}

Reference: https://docs.oracle.com/javase/9/docs/api/java/util/Properties.html#clear–



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.