Android & Kotlin with the eyes of Abhas

Android & Kotlin with the eyes of Abhas

Kotlin Sets - Abhas Kumar

Just like Lists & Maps, Set is also a collection in Kotlin. In List we can have repeatable elements but Set is a collection which contains only unique value. Let's see the declaration of Set :

fun main() {
val numSet = setOf(1, 1, 2, 3, 4, 5)
println(numSet)     // This is going to print [1, 2, 3, 4, 5]

}
  1. As we have seen in above program, Set will automatically remove the duplicates while printing the value of numSet.
fun main() {
val equalCheckSet1 = setOf( 1, 2 )
val equalCheckSet2 = setOf( 2, 1 )
println(equalCheckSet1 == equalCheckSet2 )  // prints ' true ' 

}
  1. Set doesn't care about order of elements in it. That's why ' equalCheckSet1 ' & ' equalCheckSet2 ' prints true on equal check.
fun main() {
val numSet = setOf( 1, 5, 7, 9 , 11 )
println( 9 in numSet )    // prints ' true '

println( numSet.contains( 9 ) )   // prints ' true '

println ( numSet.containsAll( setOf( 5, 7, 9 ) ) )   // prints ' true '
  1. We can check membership of any element in Set through in , contains() and containsAll() if we want to check another set into it. We have seen the demonstration of all three keywords in above program.
fun main(){
val list = listOf( 21, 21, 1, 1, 4, 6 , 8, 8, 9, 34, 56 )
println(list.toSet())  // prints [21, 1, 4, 6, 8, 9, 34, 56]
println(list.distinct() ) // prints [21, 1, 4, 6, 8, 9, 34, 56]
}
  1. List can be converted into Set if needed using .toSet() method, .toSet() will convert the list into new set with unique elements. You can also see one more method distinct() in our demonstration program above, distinct() will return the list removing all the duplicates from original list unlike toSet() .

Now, let's see more of Sets. By default setOf() is read only and we can't do any manipulation like addition or removal with the elements in it. To make it mutable we need to declare it as:

val mutableSet = mutableSetOf(1,2,3,4,5)  // Mutable set declared
mutableSet-=3 
println(mutableSet) // prints ' [1,2,4,5] '
println(mutableSet.subtract(setOf(2,3,4)))  // prints ' [1,5] '
println(mutableSet intersect setOf(1,4))      // prints ' [1,4] '

Now in above demonstration we can see the way to declare Mutable Set which allows us to do some changes in Set. As of now, I have done some changes in declared set and you can see this as well in above demonstration. Like Lists, -= and += is used to remove or add elements from Set. With subtract() method we can remove a whole set from original set and we can see this in above program. In last line of demonstration program above we can see intersect keyword which will extract given set from original set and prints it. We need to see the difference of subtract() and intersect carefully. One thing I want to clarify here at last is intersect is not a keyword, actually it is mutableSet.intersect(setOf(1,4)) but with Infix notation feature in Kotlin we can define infix methods without curly brackets and period. So we need to keep in mind that intersect here is infix method.

This is all about Kotlin Set for now. You can join me on Twitter ' twitter.com/lazy_doer ' for such Kotlin discussions and share thoughts. Thanks.

 
Share this