🎓 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 mutableSetOf function in Kotlin is used to create a mutable set of elements. This function belongs to the Kotlin standard library and provides a straightforward way to create sets that can be modified after their creation.
Table of Contents
- Introduction
mutableSetOfFunction Syntax- Understanding
mutableSetOf - Examples
- Basic Usage
- Adding and Removing Elements
- Checking for Element Presence
- Real-World Use Case
- Conclusion
Introduction
The mutableSetOf function allows you to create a mutable set containing specified elements. Unlike read-only sets, mutable sets can be modified after creation, allowing you to add, remove, and update elements.
mutableSetOf Function Syntax
The syntax for the mutableSetOf function is as follows:
fun <T> mutableSetOf(vararg elements: T): MutableSet<T>
Parameters:
elements: A variable number of elements to be included in the set.
Returns:
- A mutable set containing the specified elements.
Understanding mutableSetOf
The mutableSetOf function creates a set 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.
Examples
Basic Usage
To demonstrate the basic usage of mutableSetOf, we will create a mutable set of integers.
Example
fun main() {
val numbers = mutableSetOf(1, 2, 3, 4, 5)
println("Mutable set of numbers: $numbers")
}
Output:
Mutable set of numbers: [1, 2, 3, 4, 5]
Adding and Removing Elements
This example shows how to add and remove elements in a mutable set.
Example
fun main() {
val fruits = mutableSetOf("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 mutable set.
Example
fun main() {
val animals = mutableSetOf("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 Tags
In real-world applications, the mutableSetOf function can be used to manage a dynamic set of tags, such as tags associated with blog posts or items in an application.
Example
fun main() {
val tags = mutableSetOf("Kotlin", "Programming", "Development")
println("Original tags: $tags")
tags.add("Mobile")
println("After adding a tag: $tags")
tags.remove("Development")
println("After removing a tag: $tags")
}
Output:
Original tags: [Kotlin, Programming, Development]
After adding a tag: [Kotlin, Programming, Development, Mobile]
After removing a tag: [Kotlin, Programming, Mobile]
Conclusion
The mutableSetOf function in Kotlin is a powerful and convenient way to create mutable sets. It allows you to define a collection of unique elements that can be dynamically updated, making it suitable for various applications, including managing tags, dynamic data collections, and more.
By understanding and using the mutableSetOf function, you can effectively manage mutable sets in your Kotlin applications.
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