1.Basic java example program to get size of hashset
Output:
- int size() This method is used get get size of hashset
- package com.getSizehashset;
- import java.util.HashSet;
- import java.util.Iterator;
- public class HashsetExample{
- public static void main(String[] args) {
- //create object of HashSet
- HashSet<Integer> hashSet = new HashSet();
- //add elements to HashSet object
- hashSet.add(1);
- hashSet.add(2);
- hashSet.add(3);
- hashSet.add(4);
- hashSet.add(5);
- hashSet.add(6);
- hashSet.add(7);
- hashSet.add(8);
- System.out.println("Size of HashSet after addition : " + hashSet.size());
- System.out.println("Hashset contains");
- Iterator it=hashSet.iterator();
- while(it.hasNext()){
- System.out.println(it.next());
- }
- }
- }
Output:
- Size of HashSet after addition
- 8
- hashset contains
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
Bagikan
Get Size of Java HashSet Example
4/
5
Oleh
Kris Kimcil