Hashing
A hash function is any function that can be used to convert arbitrary-sized data to fixed-size values. Hash values, simply hashes, are returned by a hash function. The values are typically used to index a hash table, which is a fixed-size table. This process is called Hashing.
Features
- It stores the elements with the help of a technique called hashing.
- It stores unique elements and allows null values.
- Since the elements are inserted on the base of their hashcode, it cannot maintain the insertion order.
- It is the best approach for search operation because of the constant time complexity.
HashSet Capacity and Load Factor
Capacity
The capacity of a HashSet can be defined as the number of elements it can withhold. The default capacity of a HashSet is 16.
Load Factor
The load factor can be expressed as to how complete the HashSet is. The default load factor is 0.75. The capacity and load factors give us the experience of memory and performance.
Load Factor = (Number of elements stored in the table)/(Size of the HashTable)
Examples
Code 1
import java.util.*;
public class Hash1{
public static void main(String args[]){
//Creating HashSet and adding elements
HashSet<String> set=new HashSet();
set.add("Hello");
set.add("This");
set.add("is");
set.add("Vertical");
set.add("blogs");
Iterator<String> i=set.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Vertical
Hello
blogs
This
is

You can also try this code with Online Java Compiler
Run Code
In this code, we used a HashSet to add some values to the map this code. As you can see it totally depends on HashSet so the order of printing is much different from the order of addition. Also try it on java online compiler.
Code 2
import java.util.*;
public class HashSet2{
public static void main(String args[]){
//Creating HashSet and adding elements
HashSet<String> set=new HashSet<String>();
set.add("One");
set.add("Step");
set.add("Closer");
set.add("To");
set.add("FAANG");
//Traversing elements
Iterator<String> it=set.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Closer
One
FAANG
Step
To
In this code, we used a HashSet to add some values to the map this code. As we can see it totally depends on HashSet so the order of printing is much different from the order of addition. Add function is used to add the elements in the map, whereas the hasNext checks are a map contains the next element or not. Similarly .next prints the elements depending upon HashSet.
Code 3
import java.util.*;
public class Hash3{
public static void main(String args[]){
ArrayList<String> list=new ArrayList<String>();
list.add("Hello");
list.add("Young");
list.add("Coder");
HashSet<String> set=new HashSet(list);
set.add("Saalim");
Iterator<String> i=set.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Coder
Young
Hello
Saalim

You can also try this code with Online Java Compiler
Run Code
In this code, we used a HashSet to add some values to the map this code. As we can see it totally depends on HashSet so the order of printing is much different from the order of addition. Add function is used to add the elements in the map, whereas the hasNext checks are map contains the next element or not. Similarly .next prints the elements depending upon HashSet.
Frequently Asked Questions
-
What type of information gets stored in HashSet?
HashSet uses a technique called hashing to store the information. In this technique, a key holds the information that determines a unique value, and that value is called a hash code.
-
What do you mean by the capacity of HashSet?
The term capacity simple refers to the number of elements it can contain. A HashSet's memory is dynamically allocated, which means it can increase its size according to the user's need.
-
What is Hashing?
A hash function is any function that can be used to convert arbitrary-sized data to fixed-size values. Hash values, or simply hashes, are the values returned by a hash function. The values are typically used to index a hash table, which is a fixed-size table. This process is called Hashing.
Key Takeaways
In this blog, we have covered HashSet. We have given a brief introduction about Java HashSet along with its syntax. We discussed its features along with the HashCapacity and loaded factor. After that, we discussed different types of HashSet, along with examples.
Recommended Readings:
If you want to excel in these topics, please jump to our website Problems on HashSet.