3:06 pm - Monday September 26, 2444

Clear the Element using HashSet Clear() method

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

BACK

This program used to clear the element by using HashSet Clear() method.

Example Program


package com.example;

import java.util.*;

public class HashSetDemo {
	public static void main(String args[]) { // create hash set
		HashSet<String> newset = new HashSet<String>(); // populate hash set
		newset.add("Learning");
		newset.add("Easy");
		newset.add("Simply"); // checking elements in hash set
		System.out.println("Hash set values: " + newset); // clear set values
		newset.clear();
		System.out.println("Hash set values after clear: " + newset);
	}
}

Output:

Hash set values: [Learning, Simply, Easy]
Hash set values after clear: []

Description

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

Declaration

Following is the declaration for java.util.HashSet.clear() method.

public void clear()

Parameters

  • NA

Return Value

NA

Exception

  • NA

 

BACK

 

 

Filed in: Z-A All Java Codes

Comments are closed.