Remove all the elements from HashSet Using Java

 Posted On  | Yashwant Chavan 

This java tutorial will help you to learn how to delete or clear all the elements from HashSet

Remove all elements from HashSet using Java

In below example we simply delete all the elements from HashSet using .clear() method.

package com.technicalkeeda.app;

import java.util.HashSet;

public class ClearHashSetExample {

    public static void main(String[] args) {

        HashSet <String> set = new HashSet <String> ();
        set.add("Mumbai");
        set.add("Pune");
        set.add("Nashik");

        System.out.println("Cities Before Clear:-" + set);
        set.clear();
        System.out.println("Cities After Clear:-" + set);

    }
}

Output

Cities Before Clear:-[Pune, Nashik, Mumbai]
Cities After Clear:-[]


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0214 seconds.