![]() |
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 JavaJava
Output
HashSet Initialized Using Constructor: [green, white, GFG] Explanation of the above Program:
|
Reffered: https://www.geeksforgeeks.org
Java Programs |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |