LinkedHashSet clear() method in Java with Examples
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:
// 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
filter_none
Output:
LinkedHashSet: [10, 20, 30] LinkedHashSet after use of clear() method: []
Example 2:
// 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
filter_none
Output:
LinkedHashSet: [A, B, C] LinkedHashSet after use of clear() method: []
Recommended Posts:
- LinkedHashSet contains() method in Java with Examples
- Set clear() method in Java with Examples
- Calendar clear() Method in Java with Examples
- AbstractList clear() method in Java with Examples
- Collection clear() method in Java with Examples
- Properties clear() method in Java with Examples
- AbstractQueue clear() method in Java with examples
- ConcurrentSkipListMap clear() method in Java with Examples
- EnumMap clear() Method in Java with Examples
- List clear() method in Java with Examples
- AbstractCollection clear() Method in Java with Examples
- AbstractMap clear() Method in Java with Examples
- Calendar clear(int cal_field) Method in Java with Examples
- Locale.Builder clear() method in Java with Examples
- LinkedHashSet in Java with Examples
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.



