0% found this document useful (0 votes)
7 views2 pages

Hash Set

HashSet is a part of the Java Collection Framework that implements the Set interface, allowing only unique elements and providing constant-time performance for basic operations. It does not maintain insertion order, allows a single null value, and is not thread-safe. Key methods include add, remove, contains, size, isEmpty, and clear, making it suitable for scenarios where duplicates are not needed and fast operations are required.

Uploaded by

azkiyahuda711
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Hash Set

HashSet is a part of the Java Collection Framework that implements the Set interface, allowing only unique elements and providing constant-time performance for basic operations. It does not maintain insertion order, allows a single null value, and is not thread-safe. Key methods include add, remove, contains, size, isEmpty, and clear, making it suitable for scenarios where duplicates are not needed and fast operations are required.

Uploaded by

azkiyahuda711
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

HashSet in Java Collection Framework

1. What is HashSet?
 HashSet is a part of the Java Collection Framework, under java.util package.
 It implements the Set interface and does not allow duplicate elements.
 It is unordered (does not maintain insertion order).
 Uses a hash table for storing elements, offering constant-time performance (O(1))
for basic operations like add, remove, and contains.
2. Key Features of HashSet
 No Duplicates – Only unique elements are stored.
 Unordered Collection – Does not maintain insertion order.
 Allows Null – Can store a single null value.
 Not Thread-Safe – Use Collections.synchronizedSet() for thread safety.
 Uses Hashing – Provides fast lookup and retrieval.
3. Example Program: Using HashSet in Java
import java.util.HashSet;
public class HashSetExample {
public static void main(String[] args) {
// Creating a HashSet
HashSet<String> fruits = new HashSet<>();

// Adding elements
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Grapes");
fruits.add("Banana"); // Duplicate, won't be added

// Displaying elements
System.out.println("HashSet elements: " + fruits);

// Checking if an element exists


System.out.println("Contains 'Apple'? " + fruits.contains("Apple"));

// Removing an element
fruits.remove("Grapes");
System.out.println("After removing 'Grapes': " + fruits);

// Iterating using for-each loop


System.out.println("Iterating over HashSet:");
for (String fruit : fruits) {
System.out.println(fruit);
}

// Checking size
System.out.println("Size of HashSet: " + fruits.size());

// Clearing the HashSet


fruits.clear();
System.out.println("Is HashSet empty? " + fruits.isEmpty());
}
}
4. Output

HashSet elements: [Orange, Banana, Apple, Grapes]


Contains 'Apple'? true
After removing 'Grapes': [Orange, Banana, Apple]
Iterating over HashSet:
Orange
Banana
Apple
Size of HashSet: 3
Is HashSet empty? True

5. Important Methods in HashSet

Method Description

add(E e) Adds an element to the HashSet.

remove(Object o) Removes the specified element.

contains(Object o) Checks if an element exists in the HashSet.

size() Returns the number of elements in the HashSet.

isEmpty() Checks if the HashSet is empty.

clear() Removes all elements from the HashSet.

iterator() Returns an iterator to traverse elements.

6. When to Use HashSet?


 When you don’t need duplicate elements.
 When order doesn’t matter.
 When fast operations (O(1) complexity) are needed.

You might also like