The isEmpty() method returns true if the collection is empty (contains no elements), false otherwise. The contain() method can also be used to check the existence of an element in a set. The in operator can be used to check the existence of an element in a set. Println("Size of the Set " + theSet.size) Val theSet = setOf("one", "two", null, "four", "five") We can use size property to get the total number of elements in a set: Note - here it works like this operator in Java. Lets study them one by one: Using toString() function There are various ways to loop through a Kotlin Set. When you run the above Kotlin program, it will generate the following output: Val theMutableSet = mutableSetOf("one", "two", "three", "four") Val theSet = setOf("one", "two", "three", "four") A read-only view of a mutable set can be obtained by casting it to Set. Creating Kotlin Setsįor set creation, use the standard library functions setOf() for read-only sets and mutableSetOf() for mutable sets. Kotlin mutable or immutable sets do not allow to have duplicate elements. A Kotlin set can be either mutable ( mutableSetOf) or read-only ( setOf). ![]() Kotlin set is an unordered collection of items.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |