How to get the size of HashSet in Java

1 Answer

0 votes
package javaapplication1;

import java.util.HashSet;

public class JavaApplication1 {

    public static void main(String[] args) {

        HashSet hash = new HashSet();
   
        hash.add(new Integer("1"));
        hash.add(new Integer("2"));
        hash.add(new Integer("3"));
        hash.add(new Integer("4"));
        hash.add(new Integer("5"));
        hash.add(new Integer("6"));
        hash.add(new Integer("7"));

       System.out.println("Size of HashSet: " + hash.size());
   
        hash.remove(new Integer("2"));
        System.out.println("Size of HashSet: " + hash.size());
  }
}
  
/*
run:

Size of HashSet: 7
Size of HashSet: 6

*/

 



answered Sep 26, 2016 by avibootz

Related questions

1 answer 216 views
216 views asked Feb 16, 2021 by avibootz
1 answer 216 views
216 views asked Feb 7, 2024 by avibootz
1 answer 224 views
224 views asked Oct 30, 2022 by avibootz
1 answer 292 views
3 answers 259 views
259 views asked Jul 6, 2025 by avibootz
2 answers 261 views
1 answer 227 views
227 views asked Jul 23, 2023 by avibootz
...