8:18 pm - Thursday April 25, 2019

Hash set Example Program for Remove() specified element

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 Remove() method

*/

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]

BACK

Filed in: Z-A All Java Codes

No comments yet.

Leave a Reply