How to Create a HashSet With a Predefined Capacity in Java? Last Updated : 01 Feb, 2024 Comments Improve Suggest changes Like Article Like 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. Comment More infoAdvertise with us Next Article How to Create a HashSet With a Predefined Capacity in Java? D digitalvasanth Follow Improve Article Tags : Java Java Programs HashSet java-hashset Java Examples +1 More Practice Tags : Java Similar Reads 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 acco 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 Spe 2 min read 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 Hash 4 min read How to Convert a HashSet to JSON in Java? In Java, a HashSet is an implementation of the Set interface that uses a hash table to store elements. It allows fast lookups and does not allow duplicate elements. Elements in a HashSet are unordered and can be of any object type. In this article, we will see how to convert a HashSet to JSON in Jav 2 min read Convert a HashSet to a LinkedList in Java In Java, HashSet and LinkedList are linear data structures. These are majorly used in many cases. There may be some cases where we need to convert HashSet to LinkedList. So, in this article, we will see how to convert HashSet to LinkedList in Java. Java Program to Convert a HashSet to a LinkedList T 2 min read Like