Open In App

How to Create a HashSet With a Predefined Capacity in Java?

Last Updated : 01 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Java, we can create HashSet with a predefined capacity by creating a constructor and passing the capacity as a parameter to it. We have to initialize the capacity using a variable otherwise you can pass a direct value to it.

Syntax:

HashSet <datatype>myset=new HashSet<>(capacity);

Here, myset is the name of the set and capacity defines the size of it.

Program to Create a HashSet with a Predefined Capacity in Java

Java




// Java Program to create a HashSet with a predefined capacity
import java.util.*;
  
class GFG {
    public static void main (String[] args) {
       
        int capacity = 10;
  
        // Create a HashSet with a predefined capacity
        HashSet<String> myset = new HashSet<>(capacity);
  
        // Adding elements to the HashSet
         
        myset.add("Flowers");
        myset.add("Fruits");
           myset.add("Colours");
  
        // Display the elements in the myset
        System.out.println(myset);
        
    }
}


Output

[Colours, Flowers, Fruits]

Explanation of the above Program:

  • In the above example, we have initialized the capacity variable with 10 using the HashSet constructor.
  • After that, we have passed as a parameter to the constructor HashSet for which myset has been created of size 10.
  • Then it adds three elements by using add() method to the HashSet.
  • At last, it prints the values of the HashSet.

Similar Reads

How to Copy or Append HashSet to Another HashSet in Java?
HashSet is used to store distinct values in Java. HashSet stores the elements in random order, so there is no guarantee of the elements' order. The HashSet class implements the Set interface, backed by a hash table which is actually a HashMap instance. We can copy or append a HashSet to another HashSet. There is a couple of ways to copy HashSet or
4 min read
Using predefined class name as Class or Variable name in Java
In Java, you can use any valid identifier as a class or variable name. However, it is not recommended to use a predefined class name as a class or variable name in Java. The reason is that when you use a predefined class name as a class or variable name, you can potentially create confusion and make your code harder to read and understand. It may a
5 min read
Java Program to Create Set of Pairs Using HashSet
Problem statement: We need to find and print unique pairs among all given pairs. Generally, if we have to find the unique numbers among all given numbers then we just put all numbers in HashSet and print them. Let us consider a sample illustration to interpret the problem statement better. Suppose we have three pairs be customly they are (3, 5), (4
3 min read
How to Create a HashSet with a Custom Initial Load Factor in Java?
Java HashSet is a simple data structure provided by the Java Collection Framework that provides efficient storage and enables the storage of unique objects. One of the parameters that affect its performance is the load factor, which determines when the underlying hash table should be updated to accommodate multiple items. In this article, we will l
2 min read
How to Create Subsets of a HashSet based on Specific Criteria in Java?
In Java, HashSet provides a dynamic collection of unique elements. Sometimes, we may need to create subsets of a HashSet based on specific criteria. In this article, we will learn how to create subsets of a HashSet based on Specific Criteria in Java. Approach Create Subsets of a HashSet based on Specific CriteriaIterate through the original HashSet
2 min read
Difference Between Length and Capacity in Java
Length and capacity are two different things in Java. Length basically increases when characters are appended to the string whereas the capacity increases when the current capacity is exceeded by a new length. length () method is a part of the Java String class. It is used to find the length of the string. It basically counts the number of characte
3 min read
Vector capacity() Method in Java
The Java.util.Vector.capacity() method in Java is used to get the capacity of the Vector or the length of the array present in the Vector. Syntax: Vector.capacity() Parameters: The method does not take any parameter. Return Value: The method returns the capacity or the internal data array’s length present in the Vector, which is an integer value. B
2 min read
StringBuilder capacity() in Java with Examples
The capacity() method of StringBuilder Class is used to return the current capacity of StringBUilder object. The capacity is the amount of storage available to insert new characters.Syntax: public int capacity() Return Value: This method returns the current capacity of StringBuilder Class.Below programs demonstrate the capacity() method of StringBu
2 min read
Stack capacity() method in Java with Example
The capacity() method of Stack Class is used to get the capacity of this Stack. That is the number of elements present in this stack container. Syntax: public int capacity() Parameters: This function accepts a parameter E obj which is the object to be added at the end of the Stack. Return Value: The method returns integer value which is the capacit
2 min read
Buffer capacity() method in Java with Examples
The capacity() method of java.nio.Buffer Class is used to return this buffer's capacity. Syntax: public final int capacity() Return Value: The capacity of this buffer Below are the examples to illustrate the capacity() method: Examples 1: // Java program to demonstrate // capacity() method import java.nio.*; import java.util.*; public class GFG { p
2 min read
Practice Tags :