Java Collection Tutorial - Java HashSet(int initialCapacity) Constructor








Syntax

HashSet(int initialCapacity) constructor from HashSet has the following syntax.

public HashSet(int initialCapacity)

Example

In the following code shows how to use HashSet.HashSet(int initialCapacity) constructor.

import java.util.HashSet;
/*from   w w w .j  a  va2s  . co  m*/
public class Main{
    public static void main(String args[]) {
        HashSet<String> hs = new HashSet<String>(20);

        hs.add("java2s.com");
        hs.add("j a v a2s.com");

        System.out.println(hs);
    }
}

The code above generates the following result.