How to Initialize HashSet Values Using Constructor in Java? Last Updated : 01 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In Java programming, HashSet is a collection framework that is implemented by the HashSet class. It only contains unique elements and does not allow duplicate values. Mainly it does not maintain any insertion order but is inserted based on hash code. We can initialize HashSet values in many ways in Java. But in this article, we will discuss how to initialize HashSet values using Constructor in Java. Method: Using Constructor and Arrays.asList( ) methodBy using the constructor along with Arrays.asList() method we can initialize HashSet values. Below is the syntax of this for a clear understanding of the concept. Syntax: HashSet<String>myhashset=new HashSet<>(Arrays.asList(" "));Here, myhashset is the name of the HashSet created. Program to initialize HashSet values using Constructor in Java Java // Java Program to intitalise the values of HashSet // Using Arrays.asList method import java.util.*; import java.util.HashSet; import java.util.Arrays; class GFG { public static void main (String[] args) { // Initialize HashSet using Constructor HashSet<String>myhashset=new HashSet<>(Arrays.asList("green", "white", "GFG")); //printing hashset values System.out.println("HashSet Initialized Using Constructor: " +myhashset); } } OutputHashSet Initialized Using Constructor: [green, white, GFG] Explanation of the above Program:In the above example, we have initialized a HashSet named myhashset.We have initialized this using a constructor in which we have passed some values as an argument with Arrays.asList() method.At last, it prints the values of the HashSet. Comment More infoAdvertise with us Next Article How to Initialize HashSet Values Using Constructor in Java? D digitalvasanth Follow Improve Article Tags : Java Java Examples Practice Tags : Java Similar Reads Initializing HashSet in Java Set in Java is an interface which extends Collection. It is an unordered collection of objects in which duplicate values cannot be stored. Basically, set is implemented by HashSet, LinkedHashSet or TreeSet (sorted representation). Set has various methods to add, remove clear, size, etc to enhance th 2 min read Internal working of Set/HashSet in Java As we know that a set is a well-defined collection of distinct objects. Each member of a set is called an element of the set. So in other words, we can say that a set will never contain duplicate elements. But how in java Set interface implemented classes like HashSet, LinkedHashSet, TreeSet etc. ac 3 min read Constructor hashCode() method in Java with Examples The hashCode() method of java.lang.reflect.Constructor class is used to return a hashcode for this Constructor object. The hashcode is always the same if the constructed object doesnât change. Hashcode is a unique code generated by the JVM at the time of class object creation. We can use hashcode to 2 min read Initialize HashMap in Java HashMap in Java is a part of the java.util package and allows storing key-value pairs. Initializing a HashMap can be done in multiple ways, including static blocks, utility methods from the Collections class, and modern approaches provided by Java 8 and Java 9. This article will guide you through th 4 min read The Initializer Block in Java In order to perform any operations while assigning values to an instance data member, an initializer block is used. In simpler terms, the initializer block is used to declare/initialize the common part of various constructors of a class. It runs every time whenever the object is created. The initial 2 min read Like