Java HashSet is a class in the Java Collections Framework that implements the Set interface. It is used to store a collection of unique elements. The elements in a HashSet are not ordered and can be accessed in any order. HashSet is one of the most commonly used classes in Java and is very useful in many applications.
HashSet is implemented using a hash table. A hash table is a data structure that stores elements in an array. Each element is stored in a bucket based on its hash code. The hash code is a unique identifier for each element. When an element is added to the HashSet, its hash code is calculated and it is stored in the bucket corresponding to its hash code. If there is already an element in the bucket, the new element is added to a linked list in the bucket.
HashSet provides constant time performance for the basic operations like add, remove, contains, and size. The performance of HashSet depends on the quality of the hash function used to calculate the hash code of the elements. A good hash function should distribute the elements evenly across the buckets to avoid collisions.
The following code creates a HashSet of strings and adds some elements to it:
import java.util.HashSet;
public class HashSetExample {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("apple");
set.add("banana");
set.add("orange");
set.add("pear");
System.out.println(set);
}
}
The output of the above code will be:
[banana, orange, apple, pear]
As you can see, the elements in the HashSet are not ordered.
The following code removes an element from a HashSet:
import java.util.HashSet;
public class HashSetExample {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("apple");
set.add("banana");
set.add("orange");
set.add("pear");
set.remove("banana");
System.out.println(set);
}
}
The output of the above code will be:
[orange, apple, pear]
The following code checks if an element is in a HashSet:
import java.util.HashSet;
public class HashSetExample {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("apple");
set.add("banana");
set.add("orange");
set.add("pear");
if (set.contains("banana")) {
System.out.println("The set contains banana");
} else {
System.out.println("The set does not contain banana");
}
}
}
The output of the above code will be:
The set contains banana
The following code iterates over a HashSet:
import java.util.HashSet;
import java.util.Iterator;
public class HashSetExample {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("apple");
set.add("banana");
set.add("orange");
set.add("pear");
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
String element = iterator.next();
System.out.println(element);
}
}
}
The output of the above code will be:
orange
apple
pear
banana
As you can see, the elements in the HashSet are not ordered.
Java HashSet is a very useful class in the Java Collections Framework. It is used to store a collection of unique elements. HashSet is implemented using a hash table and provides constant time performance for the basic operations like add, remove, contains, and size. HashSet is one of the most commonly used classes in Java and is very useful in many applications.