11:40 am - Wednesday May 24, 2017

To find size of element HashSet Size() 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 find the size of element using Hashset Size() method

Example Program


package com.example;

import java.util.HashSet;

public class FindSize {
	public static void main(String a[]) {
		HashSet<Integer> hs = new HashSet<Integer>();
		hs.add(12);
		hs.add(23);
		hs.add(5);
		hs.add(6);
		hs.add(7);
		hs.add(6);
		System.out.println(hs);
		System.out.println("Is HashSet empty? " + hs.isEmpty());
		System.out.println("before Remove Size the HashSet: " + hs.size());
		hs.remove(6);
		System.out.println(hs);
		System.out.println("After remove Size of the HashSet: " + hs.size());
		// System.out.println("Does HashSet contains first element? "+hs.contains("first"));
		// }}

	}
}



 

Output:

[5,6,7,12,23]

Is HashSet empty? false

before Remove Size the HashSet:5

[5,7,12,23]

After remove Size of the HashSet:4

Description

The size() method is used to get the number of elements in this set.

Declaration

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

public int size()

Parameters

  • NA

Return Value

The method call returns the number of elements in this set.

Exception

  • NA

BACK

Filed in: Z-A All Java Codes

Comments are closed.