Key points about the HashSet class
Unordered and unsorted — elements in a HashSet are not ordered or sorted, and there’s no
guarantee the order stays constant over time.
Unique elements — HashSet does not allow duplicate values; adding the same value again
doesn’t replace the old one.
Null elements — HashSet allows one null value.
Non-synchronized — HashSet is not thread-safe; multiple threads can access it
simultaneously.
Underlying data structure — HashSet is implemented in terms of a hash table, and internally
uses a HashMap to store elements.
Performance — add, remove, and contains all have O(1) time complexity.
Implements Set — HashSet implements the Set interface, inheriting all its methods.
Creating a HashSet
HashSet<String> set = new HashSet<>();Adding elements to a HashSet
This is done using the add method.
HashSet<String> set = new HashSet<>();
// add elementsset.add("Java");set.add("Python");set.add("JavaScript");
// display the elementsfor (String language : set) { System.out.println(language);}Creating a HashSet from another collection
List<Integer> list = new ArrayList<>();list.add(5);list.add(10);list.add(15);list.add(20);list.add(25);
List<Integer> list2 = new ArrayList<>();list2.add(3);list2.add(6);list2.add(9);list2.add(12);list2.add(15);
// Creating a HashSet from another collection (ArrayList)Set<Integer> set = new HashSet<>(list);
// Adding all the elements from an existing collection to a HashSetset.addAll(list2);
System.out.println(set);
// Output = [3, 5, 6, 9, 10, 12, 15, 20, 25]Accessing elements
HashSet doesn’t have a get() method to access elements, since it’s not an indexed collection.
You can iterate through it to access its elements.
public class Main { public static void main(String[] args) { // Creating a HashSet HashSet<String> set = new HashSet<>();
// Adding new elements to the HashSet set.add("Java"); set.add("Python"); set.add("JavaScript");
// Accessing elements using an iterator Iterator<String> iterator = set.iterator(); while (iterator.hasNext()) { String language = iterator.next(); System.out.println(language); } }}Removing elements
You can remove elements from a HashSet using:
remove(Object o)— removes a single specified element from the set.removeAll(Collection<?> c)— removes all elements from the set that are contained in the specified collection.removeIf(Predicate<? super E> filter)— removes all elements from the set that satisfy the given predicate.
public class Main { public static void main(String[] args) { // Creating a HashSet HashSet<String> set = new HashSet<>(); set.addAll(Arrays.asList("Apple", "Banana", "Cherry", "Date", "Elderberry"));
System.out.println("Original HashSet: " + set);
// Using remove(Object o) set.remove("Cherry"); System.out.println("After remove('Cherry'): " + set);
// Using removeAll(Collection c) set.removeAll(Arrays.asList("Apple", "Banana")); System.out.println("After removeAll(['Apple', 'Banana']): " + set);
// Using removeIf(Predicate<? super E> filter) set.removeIf(fruit -> fruit.startsWith("E")); System.out.println("After removeIf(fruit -> fruit.startsWith('E')): " + set); }}