2:00 am - Tuesday April 23, 2019

Remove all elements from HashSet example

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

//*@author :CandidJava.com

//*@descriptions : This program explain the function and use of clear() method

import java.util.HashSet;

class RemoveAllElementsFromHashSet {

public static void main(String[] args) {

//creating HashSet Object

HashSet hSet = new HashSet();

hSet.add(new Integer("1"));

hSet.add(new Integer("2"));

hSet.add(new Integer("3"));

System.out.println("HashSet before removal : " + hSet);

//using clear() method all the elements has to be removed from the set

hSet.clear();

System.out.println("HashSet after removal : " + hSet);

//isEmpty() method is used to check whether the set is empty or Not

System.out.println("Is HashSet empty ? " + hSet.isEmpty());

}

}

 

Output:

D:\CandidJava\Util\HashSet>javac RemoveAllElementsFromHashSet.java

Note: RemoveAllElementsFromHashSet.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details.

D:\CandidJava\Util\HashSet>java RemoveAllElementsFromHashSet

HashSet before removal : [1, 2, 3]

HashSet after removal : []

Is HashSet empty ? true

Filed in: Z-A All Java Codes

No comments yet.

Leave a Reply