8:04 am - Friday March 24, 2017

HashSet to remove specfic element on output Remove() 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 remove the specific element from hashset by using remove() method.

Example Program


package com.example;

import java.util.HashSet;

class HashSetRemove {
	public static void main(String[] args) {// creating HashSet Object
		HashSet hSet = new HashSet();// add() method is used to insert the
		// elements ito the set
		hSet.add(new Integer("1"));
		hSet.add(new Integer("2"));
		hSet.add(new Integer("3"));
		hSet.add(new Integer("4"));
		hSet.add(new Integer("5"));
		hSet.add(new Integer("6"));
		hSet.remove(3);
		System.out.println("HashSet contains.." + hSet);
	}
}

Output:

HashSet contains..[1,2,4,5,6]

Description

The remove(Object o) method is used to remove the specified element from this set if it is present.

Declaration

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

public boolean remove(Object o)

Parameters

  • o–This is the object to be removed from this set, if present.

Return Value

The method call returns ‘true’ if the set contained the specified element.

Exception

  • NA

BACK

Filed in: Z-A All Java Codes

Comments are closed.