0% found this document useful (0 votes)
2 views23 pages

Java Collections 04 Class Notes

Uploaded by

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

Java Collections 04 Class Notes

Uploaded by

Parul
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CS & IT

ENGINEERING
Java With OOPS
Java Collections

Lecture No-04 By- Aditya sir


Recap of Previous Lecture

Topic

Hashmap

Questions
Topics to be Covered

Topic Java Collections: HashSet

Questions

3
Topic : Introduction to HashSet

Definition:
• A HashSet is a collection that contains no duplicate elements and is backed by a
hash table (using a HashMap internally).
• It is part of the [Link] package.
Characteristics:
• No Duplicates: Automatically prevents duplicate values.
• Unordered: The elements are not stored in any particular order.
• Fast Operations: Offers constant-time performance (O(1)) for basic operations
like add, remove, and contains on average.
• Null Elements: Allows one null element.
Topic : Introduction to HashSet

Common Methods:
• add(element), remove(element), contains(element), size(), isEmpty(), clear(),
and methods for iteration.
Use Cases:
• Eliminating duplicates from a collection.
• Fast membership checking.
• Implementing mathematical sets (union, intersection, difference).
A B

CANI
A
FUB
Egg
EEyB

A B A
B V B
A
Topic : Basic HashSet Examples

Example 1 – Creating and Populating a HashSet


• Problem Statement: Create a HashSet of integers and print the set.
• Hint: Use add(element) to insert values; note that duplicates will be ignored.
• Code:
import [Link];
public class HS_Example1 {
public static void main(String[] args) {
// Hint: Add elements using add(), duplicates are automatically discarded.
HashSet<Integer> numbers = new HashSet<>();
[Link](10);
Topic : Basic HashSet Examples

[Link](20);
[Link](30);
[Link](20); // duplicate; will not be added
[Link](40);
[Link](50);
[Link](10); // duplicate
[Link](60);
[Link]("HashSet: " + numbers);
}
}
• Expected Output (order may vary):
HashSet: [50, 20, 40, 10, 60, 30]
Topic : Basic HashSet Examples

Example 2 – Checking if an Element Exists


• Problem Statement: Check if a HashSet of strings contains the element "apple".
• Hint: Use contains(element) to check membership.
• Code:
import [Link];
public class HS_Example2 {
public static void main(String[] args) {
// Hint: Use contains() to verify the existence of an element.
HashSet<String> fruits = new HashSet<>();
[Link]("apple");
Topic : Basic HashSet Examples

[Link]("banana");
[Link]("cherry");
boolean hasApple = [Link]("apple");
[Link]("Contains 'apple'? " + hasApple);
}
}
• Expected Output:
Contains 'apple'? true
Topic : Basic HashSet Examples

Example 3 – Removing an Element


• Problem Statement: Remove a specific element from a HashSet of integers and
print the updated set.
• Hint: Use remove(element) to delete the element.
• Code:
import [Link];
public class HS_Example3 {
public static void main(String[] args) {
// Hint: Remove an element using remove()
HashSet<Integer> numbers = new HashSet<>();
[Link](10);
Topic : Basic HashSet Examples

[Link](20);
[Link](30);
[Link](40);
[Link]("Before removal: " + numbers);
[Link](20);
[Link]("After removal: " + numbers);
}
}
• Expected Output (order may vary):
Before removal: [40, 10, 20, 30]
After removal: [40, 10, 30]
Topic : Basic HashSet Examples

Example 4 – Iterating Over a HashSet


• Problem Statement: Iterate over a HashSet of strings and print each element.
• Hint: Use a for-each loop to iterate over the set.
• Code:
import [Link];
public class HS_Example4 {
public static void main(String[] args) {
// Hint: Use a for-each loop to iterate through the HashSet.
HashSet<String> colors = new HashSet<>();
[Link]("Red");
Topic : Basic HashSet Examples

[Link]("Green");
[Link]("Blue");
[Link]("Yellow");
for (String color : colors) {
[Link](color);
}
}
}
• Expected Output (order may vary):
Red
Green
Blue
Yellow
Topic : Basic HashSet Examples

Example 5 – Getting the Size of the HashSet


• Problem Statement: Print the number of elements in a HashSet.
• Hint: Use size() to get the count.
• Code:
import [Link];
public class HS_Example5 {
public static void main(String[] args) {
// Hint: Use size() to get the number of elements.
HashSet<String> animals = new HashSet<>();
Topic : Basic HashSet Examples

[Link]("Cat");
[Link]("Dog");
[Link]("Bird");
[Link]("Fish");
[Link]("Size: " + [Link]());
}
}
• Expected Output:
Size: 4
Topic : Basic HashSet Examples

Example 6 – Clearing a HashSet


• Problem Statement: Clear all elements from a HashSet and print its size.
• Hint: Use clear() to remove all elements, then check size with size().
• Code:
import [Link];
public class HS_Example6 {
public static void main(String[] args) {
// Hint: clear() empties the set; size() should return 0.
HashSet<Integer> numbers = new HashSet<>();
Topic : Basic HashSet Examples

[Link](1);
[Link](2);
[Link](3);
[Link]("Before clear: " + [Link]());
[Link]();
[Link]("After clear: " + [Link]());
}
}
• Expected Output:
Before clear: 3
After clear: 0
Topic : Basic HashSet Examples

Example 7 – Converting a HashSet to an Array


• Problem Statement: Convert a HashSet of strings to an array and print the array
elements.
• Hint: Use toArray() to convert the set to an array.
• Code:
import [Link];
import [Link];
public class HS_Example7 {
public static void main(String[] args) {
Topic : Basic HashSet Examples

// Hint: Convert the HashSet to an array using toArray().


HashSet<String> fruits = new HashSet<>();
[Link]("apple");
[Link]("banana");
[Link]("cherry");
Object[] fruitArray = [Link]();
c
[Link]("Array: " + [Link](fruitArray));
}
}
• Expected Output (order may vary):
Array: [banana, cherry, apple]
Topic : Basic HashSet Examples

Example 8 – Using HashSet to Remove Duplicates from an ArrayList


• Problem Statement: Given an ArrayList with duplicate values, use a HashSet to
eliminate duplicates and print the result.
• Hint: Create a HashSet from the ArrayList and then (optionally) convert back to an
ArrayList.
• Code:
import [Link];
import [Link];
public class HS_Example8 {
public static void main(String[] args) {
// Hint: HashSet automatically removes duplicate entries.
Topic : Basic HashSet Examples

ArrayList<Integer> listWithDuplicates = new ArrayList<>();


[Link](10);
[Link](20);
[Link](10);
[Link](30);
[Link](20);
HashSet<Integer> uniqueSet = new HashSet<>(listWithDuplicates);
[Link]("Unique elements: " + uniqueSet);
}
}
• Expected Output (order may vary):
Unique elements: [20, 10, 30]
THANK - YOU

You might also like