8:41 pm - Saturday April 20, 2019

Java Program to remove specified element from HashSet

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

import java.util.HashSet;

class RemoveSpecifiedElementFromHashSet {

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);

// remove() method is used to remove a particular element from the set
// using boolean function

boolean blnRemoved = hSet.remove(new Integer("2"));

System.out.println("Was 2 removed from HashSet ? " + blnRemoved);

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

}

}

Output:

D:\CandidJava\Util\HashSet>javac “RemoveSpecifiedElementFromHashSet .java”

Note: RemoveSpecifiedElementFromHashSet .java uses unchecked or unsafe ops.

Note: Recompile with -Xlint:unchecked for details.

D:\CandidJava\Util\HashSet>java RemoveSpecifiedElementFromHashSet

HashSet before removal : [1, 2, 3]

Was 2 removed from HashSet ? true

HashSet after removal : [1, 3]

Filed in: Z-A All Java Codes

No comments yet.

Leave a Reply