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 212 views
212 views asked Feb 16, 2021 by avibootz
1 answer 209 views
209 views asked Feb 7, 2024 by avibootz
1 answer 222 views
222 views asked Oct 30, 2022 by avibootz
1 answer 290 views
3 answers 255 views
255 views asked Jul 6, 2025 by avibootz
2 answers 259 views
1 answer 225 views
225 views asked Jul 23, 2023 by avibootz
...