🎓 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
The hashSetOf function in Kotlin is used to create a mutable HashSet of elements. This function belongs to the Kotlin standard library and provides a straightforward way to create hash sets that can be modified after their creation.
Table of Contents
- Introduction
hashSetOfFunction Syntax- Examples
- Basic Usage
- Adding and Removing Elements
- Checking for Element Presence
- Real-World Use Case
- Conclusion
Introduction
The hashSetOf function allows you to create a mutable HashSet containing specified elements. A HashSet is a collection that does not allow duplicate elements and uses hash-based storage for fast lookups and modifications.
The hashSetOf function creates a HashSet that can be modified after its creation. This allows you to perform operations such as adding, removing, and checking for elements in the set, making it suitable for dynamic collections of unique items.
hashSetOf Function Syntax
The syntax for the hashSetOf function is as follows:
fun <T> hashSetOf(vararg elements: T): HashSet<T>
Parameters:
elements: A variable number of elements to be included in the set.
Returns:
- A mutable
HashSetcontaining the specified elements.
Examples
Basic Usage
To demonstrate the basic usage of hashSetOf, we will create a mutable HashSet of integers.
Example
fun main() {
val numbers = hashSetOf(1, 2, 3, 4, 5)
println("HashSet of numbers: $numbers")
}
Output:
HashSet of numbers: [1, 2, 3, 4, 5]
Adding and Removing Elements
This example shows how to add and remove elements in a HashSet.
Example
fun main() {
val fruits = hashSetOf("Apple", "Banana", "Cherry")
println("Original set: $fruits")
fruits.add("Date")
println("After adding an element: $fruits")
fruits.remove("Banana")
println("After removing an element: $fruits")
}
Output:
Original set: [Apple, Banana, Cherry]
After adding an element: [Apple, Banana, Cherry, Date]
After removing an element: [Apple, Cherry, Date]
Checking for Element Presence
This example demonstrates how to check for the presence of an element in a HashSet.
Example
fun main() {
val animals = hashSetOf("Dog", "Cat", "Horse")
val isDogPresent = animals.contains("Dog")
val isElephantPresent = animals.contains("Elephant")
println("Is Dog present: $isDogPresent")
println("Is Elephant present: $isElephantPresent")
}
Output:
Is Dog present: true
Is Elephant present: false
Real-World Use Case
Managing a Dynamic Set of Users
In real-world applications, the hashSetOf function can be used to manage a dynamic set of users, such as tracking active users in a system.
Example
fun main() {
val activeUsers = hashSetOf("Alice", "Bob", "Charlie")
println("Active users: $activeUsers")
activeUsers.add("Dave")
println("After adding a user: $activeUsers")
activeUsers.remove("Charlie")
println("After removing a user: $activeUsers")
}
Output:
Active users: [Alice, Bob, Charlie]
After adding a user: [Alice, Bob, Charlie, Dave]
After removing a user: [Alice, Bob, Dave]
Conclusion
The hashSetOf function in Kotlin is a powerful and convenient way to create mutable HashSets. It allows you to define a collection of unique elements that can be dynamically updated, making it suitable for various applications, including managing dynamic data collections.
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