π 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
In this guide, we will learn about the Kotlin setOf function with lot's of examples.
What is setOf()?
The setOf() is a function in Kotlin’s standard library designed to create an immutable (read-only) set. A set is a collection that doesn't allow duplicate elements. Being immutable means that once a set is created using setOf(), its elements cannot be modified - you can't add or remove items.
Basic Syntax:
val set: Set<Type> = setOf(element1, element2, element3, ...)Examples with Outputs
Creating a Basic Set
val colors = setOf("Red", "Blue", "Green")
println(colors) // Output: [Red, Blue, Green]Uniqueness in Sets
Sets inherently ensure the uniqueness of elements:
val numbers = setOf(1, 2, 3, 2, 1)
println(numbers) // Output: [1, 2, 3]Merging Two Sets
Merging is simple and retains unique elements:
val set1 = setOf(1, 2, 3)
val set2 = setOf(3, 4, 5)
val mergedSet = set1 + set2
println(mergedSet) // Output: [1, 2, 3, 4, 5]Empty and Nullable Sets
val emptySet = setOf<String>()
println(emptySet) // Output: []
val nullableSet = setOf(null, "Hello")
println(nullableSet) // Output: [null, Hello]Convert a Set to a List
val numSet = setOf(1, 2, 3)
val numList = numSet.toList()Conclusion
Kotlin's setOf() provides a concise and idiomatic way to define immutable sets. In this guide, we went through the usage of the setOf() method with lots of examples.
Related Kotlin Posts
- Kotlin List
- Kotlin Set
- Kotlin listOf
- Kotlin mutableListOf
- Kotlin mapOf
- Kotlin mutableMapOf
- Kotlin setOf
- Kotlin mutableSetOf
- Kotlin sequenceOf
- Kotlin list filter
- Kotlin list map
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