⚫ HashSet is data structures in the Java Collections API.
⚫ HashSet stores unique elements and permits nulls.
// Import the HashSet class
import java.util.HashSet;
/**
* HashSet stores unique elements and permits nulls.
*/
public class AddNullValuesToHashSet {
public static void main(String[] args) {
// Create an HashSet object
HashSet<String> cars = new HashSet<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("BMW");
cars.add(null);
System.out.println(cars);
}
}
[null, Volvo, Ford, BMW]
⚫ HashSet is backed by a HashMap.
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
⚫ HashSet is not thread-safe.
HashSet implementation is not synchronized. If multiple threads access a hash set concurrently, and at least one of the threads modifies the set, it must be synchronized externally.
Note: Reading doesn't modify a set, therefore it is thread safe
Behing the Scenes:
1-) Hashing: When you add an element to a HashSet, the hashCode() method of the object is called to determine the hash code of that element. Hash code is basically an integer value generated for an object, which is used to quickly identify it in the hash table.
2-) Bucketing: The hash code determines the bucket in which the element will be stored. A bucket is simply a slot in the hash table. If there's already an element in the same bucket (i.e., if there's a hash code collision), then the equals() method of the existing element and the new element is called to check if they are equal. If equals() returns true, the new element is considered a duplicate and not added to the HashSet.
3-) Maintaining uniqueness: Each element is stored in a bucket based on its hash code, and HashSet ensures that no two elements in the same bucket are considered equal according to the equals() method. This is how duplicates are avoided.
4-) Performance: The use of hashing provides very efficient insertion, deletion, and lookup operations (O(1) on average) compared to other data structures like lists or trees.
Create HahSet:
import java.util.HashSet; // import the HashSet class
public class Create {
public static void main(String[] args) {
// Create an HashSet object
HashSet<String> cars = new HashSet<String>();
HashSet<String> bikes = new HashSet<>();
}
}
Add Element to HahSet:
// Import the HashSet class
import java.util.HashSet;
public class Add {
//It is an implementation of a hash set, which is similar to a HashMap but contains only unique elements (no duplicate values).
public static void main(String[] args) {
HashSet<String> cars = new HashSet<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("BMW");
cars.add("Mazda");
System.out.println(cars);
}
}
Check Element in HahSet:
// Import the HashSet class
import java.util.HashSet;
public class Check {
public static void main(String[] args) {
HashSet<String> cars = new HashSet<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("BMW");
cars.add("Mazda");
System.out.println(cars.contains("Mazda"));
// Output: true
}
}
Clear HahSet:
// Import the HashSet class
import java.util.HashSet;
public class Clear {
public static void main(String[] args) {
HashSet<String> cars = new HashSet<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("BMW");
cars.add("Mazda");
cars.clear();
System.out.println(cars);
}
}
Wrapper Types in HahSet Create:
// Import the HashMap class
import java.util.HashSet;
public class OtherTypes {
// int - Integer
// double - Double
// char - Character
// boolean - Boolean
public static void main(String[] args) {
// Create a HashSet object called numbers
HashSet<Integer> numbers = new HashSet<Integer>();
// Add values to the set
numbers.add(4);
numbers.add(7);
numbers.add(8);
// Show which numbers between 1 and 10 are in the set
for(int i = 1; i <= 10; i++) {
if(numbers.contains(i)) {
System.out.println(i + " was found in the set.");
} else {
System.out.println(i + " was not found in the set.");
}
}
}
}
Remove Element from HahSet:
// Import the HashSet class
import java.util.HashSet;
public class Remove {
public static void main(String[] args) {
HashSet<String> cars = new HashSet<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("BMW");
cars.add("Mazda");
cars.remove("Volvo");
System.out.println(cars);
}
}
HahSet Size:
// Import the HashSet class
import java.util.HashSet;
public class Size {
public static void main(String[] args) {
HashSet<String> cars = new HashSet<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("BMW");
cars.add("Mazda");
System.out.println(cars.size());
}
}
💫The End 💫