Related Articles

Related Articles

LinkedHashSet clear() method in Java with Examples
  • Last Updated : 12 Oct, 2018

The clear() method of java.util.LinkedHashSet class is used to remove all of the elements from this set. The set will be empty after this call returns.

Syntax:

public void clear()

Return Value: This method does not return anything.

Below are the examples to illustrate the clear() method.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// clear() method
// for Integer value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // Create the object of LinkedHashSet<Integer>
            LinkedHashSet<Integer>
                linkset = new LinkedHashSet<Integer>();
  
            // populate hash set
            linkset.add(10);
            linkset.add(20);
            linkset.add(30);
  
            // print the LinkedHashSet
            System.out.println("LinkedHashSet: "
                               + linkset);
  
            // clear set values
            // using clear() method
            linkset.clear();
  
            // print the LinkedHashSet
            System.out.println("LinkedHashSet after "
                               + "use of clear() method: "
                               + linkset);
        }
  
        catch (NullPointerException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

LinkedHashSet: [10, 20, 30]
LinkedHashSet after use of clear() method: []

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// clear() method
// for String value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // Create the object of LinkedHashSet<Integer>
            LinkedHashSet<String>
                linkset = new LinkedHashSet<String>();
  
            // populate hash set
            linkset.add("A");
            linkset.add("B");
            linkset.add("C");
  
            // print the LinkedHashSet
            System.out.println("LinkedHashSet: "
                               + linkset);
  
            // clear set values
            // using clear() method
            linkset.clear();
  
            // print the LinkedHashSet
            System.out.println("LinkedHashSet after "
                               + "use of clear() method: "
                               + linkset);
        }
  
        catch (NullPointerException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

LinkedHashSet: [A, B, C]
LinkedHashSet after use of clear() method: []

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :