🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
A HashSet in Java is a collection that does not allow duplicate elements and provides constant time performance for basic operations such as add, remove, and contains, assuming the hash function disperses elements properly among the buckets. This makes HashSet an excellent choice for collections where unique elements are a priority, and order is not a concern. This blog post will explore CRUD (Create, Read, Update, Delete) operations in a HashSet.
2. Program Steps
1. Create a new HashSet.
2. Add elements to the HashSet (Create).
3. Read/Check if elements are present in the HashSet.
4. Update an element in the HashSet (remove the old element and add a new one).
5. Remove an element from the HashSet (Delete).
3. Code Program
import java.util.HashSet;
public class HashSetCRUDExample {
public static void main(String[] args) {
// Step 1: Creating a new HashSet
HashSet<String> fruits = new HashSet<>();
// Step 2: Adding elements to the HashSet (Create)
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
System.out.println("HashSet after adding elements: " + fruits);
// Step 3: Reading/Checking if elements are present in the HashSet
boolean hasApple = fruits.contains("Apple");
System.out.println("Contains Apple? " + hasApple);
// Step 4: Updating an element in the HashSet (No direct method to update)
// Remove the old element and add the new element
if (fruits.remove("Banana")) {
fruits.add("Blueberry");
System.out.println("HashSet after updating (replacing Banana with Blueberry): " + fruits);
}
// Step 5: Removing an element from the HashSet (Delete)
fruits.remove("Cherry");
System.out.println("HashSet after removing Cherry: " + fruits);
}
}
Output:
HashSet after adding elements: [Apple, Banana, Cherry] Contains Apple? true HashSet after updating (replacing Banana with Blueberry): [Apple, Blueberry, Cherry] HashSet after removing Cherry: [Apple, Blueberry]
Explanation:
1. The program starts by creating a HashSet named fruits that stores String objects. HashSet ensures that all elements are unique.
2. Adding elements: The add() method is used to insert new elements. If an element already exists, add() does not modify the set.
3. Reading/Checking elements: The contains() method checks if a specific element is present in the set, demonstrating how to read or verify elements in a HashSet.
4. Updating elements: HashSet does not have a direct method for updating elements because it does not maintain element order or indices. To "update" an element, you remove the old element and add the new one. This example replaces "Banana" with "Blueberry".
5. Removing elements: The remove() method deletes a specified element from the set.
6. The output illustrates the state of the HashSet after each operation. This example demonstrates how to perform CRUD operations in a HashSet, highlighting its use cases for managing collections of unique elements.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment