Remove all Elements from LinkedHashSet in Java Example

This Java example shows how to remove or clear all the elements from LinkedHashSet in Java using clear() method with an example.

Remove all Elements from LinkedHashSet in Java Example

package com.javaguides.collections.linkedhashsetexamples;

import java.util.LinkedHashSet;
import java.util.Set;

public class LinkedHashSetRemoveExample {
    public static void main(String[] args) {
        Set < Integer > numbers = new LinkedHashSet < > ();
        numbers.add(2);
        numbers.add(3);
        numbers.add(4);
        numbers.add(5);
        numbers.add(6);
        numbers.add(7);
        numbers.add(8);
        numbers.add(9);
        numbers.add(10);

        System.out.println("numbers : " + numbers);

        // Remove all elements from LinkedHashSet (clear it completely)
        numbers.clear();
        System.out.println("After clear() => " + numbers);
    }
}

Output

numbers : [2, 3, 4, 5, 6, 7, 8, 9, 10]
After clear() => []

Comments