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 132 views
132 views asked Feb 16, 2021 by avibootz
1 answer 114 views
114 views asked Feb 7, 2024 by avibootz
1 answer 132 views
132 views asked Oct 30, 2022 by avibootz
1 answer 163 views
3 answers 105 views
105 views asked Jul 6, 2025 by avibootz
2 answers 132 views
1 answer 119 views
119 views asked Jul 23, 2023 by avibootz
...