set-collection-kotlin

Set in Kotlin

The Set interface extends the Collection interface. It will make sure that an instance of Set contains no duplicate elements.

The subclasses class implements hashcode and equals methods to make sure the uniqueness of objects.

Three concrete classes of Set are HashSet, LinkedHashSet, and TreeSet

In my Way of telling Set : Set is nothing but a sack, which can contain any objects inside the sack

If you place two identical pebbles inside a sack will you able to find which one you dropped first into the sack ?, heck No.

Because sack does not maintain any order, so when you pick the pebble it will lead to the confusion that which one you want to access(intention), so due to this issue Set type of collection in Kotlin will not allow you to put duplicates in the set.

Set stores the values in random order and accessing the elements happens through element Value rather than index.

HashSet, LinkedHashSet, TreeSet are the classes which implement the Set interface

Kotlin Set functions

indexOf()

indexOf() method gets the position of the given value if the given value is not present then kotlin returns -1. indexOf() returns the position of the first occurrence.

fun main(args: Array<String>) {
    var abc = setOf(8, 6, 5, 1)
    println(abc.indexOf(6))
}

lastIndexOf()

lastIndexOf() function returns the last positions of the given value if the given value is not present then kotlin will return -1

fun main(args: Array<String>) {
    var abc = setOf(8, 6, 5, 1, 6)
    println(abc.lastIndexOf(6))
}

last()

last() method will return the value of the last element in the given set in kotlin

fun main(args: Array<String>) {
    var abc = setOf(8, 6, 5, 1, 6)
    println(abc.last())
}

size

size property (it is not a function) returns the number of elements/items present in the list.

fun main(args: Array<String>) {
    var abc = setOf(8, 6, 5, 1, 6)
    println(abc.size)
}

List Collections in Kotlin

contains()

contains() method will return true if the given value is present in the set otherwise false.

fun main(args: Array<String>) {
    var abc = setOf(8, 6, 5, 1, 6)
    println(abc.contains(8))
    println(abc.contains(108))
}

containsAll()

containsAll() function accepts a set of elements and checks whether the given subset items are present in the Original set irrespective of order.

fun main(args: Array<String>) {
    var abc = setOf(8, 6, 5, 1, 6)
    println(abc.containsAll(setOf(1,6)))
    println(abc.containsAll(setOf(5,8)))
    println(abc.containsAll(setOf(5,33)))
}

isEmpty()

isEmpty() function checks whether the set has any element or not, if there is no element is present then kotlin will return true otherwise false.

fun main(args: Array<String>) {
    var abc = setOf(8, 6, 5, 1, 6)
    println(abc.isEmpty())
}

drop()

drop() function removes the given number of elements from the set starting at the beginning of the list, for example, if you pass 3 as value to drop() function then, first three elements from the set will be dropped and return the remaining elements. It does not affect the original list

If the given number id more than the size of the set then it will remove all values but it will not throw any error

fun main(args: Array<String>) {
    var abc = setOf(8, 6, 5, 1, 6)
    println(abc.drop(2))
    println(abc)
}

elementAt()

elementAt() function will return a value at the given index, if the given index is not present(i.e more than size of the list) then kotlin will throw Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20

fun main(args: Array<String>) {
    var abc = setOf(8, 6, 5, 1, 6)
    println(abc.elementAt(20))
}

elementAtOrElse()

elementAtOrElse() function will return a value at the given index, if the given index is not present then instead of raising the ArrayIndexOutOfBoundsException kotlin will return the given lambda result

fun main(args: Array<String>) {
    var abc = setOf(8, 6, 5, 1, 6)
    println(abc.elementAtOrElse(1, {999}))
    println(abc.elementAtOrElse(35, {999}))
}

elementAtOrNull()

elementAtOrNull() function will return the value at the gievn index, if given index is not present then kotlin will return null instead of raising ArrayIndexOutOfBoundsException

fun main(args: Array<String>) {
    var abc = setOf(8, 6, 5, 1, 6)
    println(abc.elementAtOrNull(1))
    println(abc.elementAtOrNull(33))
}

first()

first() function will return the first element of the list

fun main(args: Array<String>) {
    var abc = setOf(8, 6, 5, 1, 6)
    println(abc.first())
}

firstOrNull()

firstOrNull() function will return the first element of the list if the set empty then kotlin ill return null

fun main(args: Array<String>) {
    var abc = setOf(8, 6, 5, 1, 6)
    println(abc.firstOrNull())
}

single()

single() function returns the single element or throws an exception if the set is empty or has more than one element.

The exception would be Exception in thread "main" java.lang.IllegalArgumentException: Set has more than one element.

fun main(args: Array<String>) {
    var abc = setOf(8)
    println(abc.single())
    var xyz = setOf(8, 3, 5)
    println(xyz.single())
}

singleOrNull()

singleOrNull() function returns a single element, or 'null' if the set is empty or has more than one element.

fun main(args: Array<String>) {
    var abc = setOf(8)
    println(abc.singleOrNull())
    var xyz = setOf(8, 3, 5)
    println(xyz.singleOrNull())
}

any()

any()function will return true if the given set has at least one element otherwise false.

fun main(args: Array<String>) {
    var abc = setOf(8, 6, 3, 9, 5, 7)
    println(abc.any())
}

average()

average() function returns an average value of elements in the collection, add all elements in the set and divide the result with the number of elements present in the list

fun main(args: Array<String>) {
    var abc = setOf(8, 6, 3, 9, 5, 7)
    println(abc.average())
}

isNotEmpty()

isNotEmpty() function return true if the given set is not empty, that means if the set has at least one element then result would be true otherwise false

fun main(args: Array<String>) {
    var abc = setOf(8, 6, 3, 9, 5, 7)
    println(abc.isNotEmpty())
}

Map Collections in Kotlin

count()

count() function returns the number of elements in this collection.

fun main(args: Array<String>) {
    var abc = setOf(8, 6, 3, 9, 5, 7)
    println(abc.count())
}

distinct()

distinct() return the unique values present in the set which mean if a element is present more than once then the result will have the element once only

fun main(args: Array<String>) {
    var abc = setOf(8, 6, 3, 8, 6, 7)
    println(abc.distinct())
}

max()

max() function return maximum value present in the list

fun main(args: Array<String>) {
    var abc = setOf(8, 6, 3, 8, 6, 7)
    println(abc.max())
}

min()

min() function returns minimum value present in the list

fun main(args: Array<String>) {
    var abc = setOf(8, 6, 3, 8, 6, 7)
    println(abc.min())
}

plusElement() & plus()

Returns a set containing all elements of the original collection and then the given [element]. These methods do not alter the existing list

fun main(args: Array<String>) {
    var abc = setOf(8, 6, 3, 8, 6, 7)
    println(abc.plusElement(20))
	println(abc)
}

reversed()

reversed() function returns a set with elements in reversed order.

fun main(args: Array<String>) {
    var abc = setOf(8, 6, 3, 8, 6, 7)
    println(abc.reversed())
    println(abc)
}

shuffled()

shuffled() function returns a new set with the elements of this set randomly shuffled

fun main(args: Array<String>) {
    var abc = setOf(8, 6, 3, 8, 6, 7)
    println(abc.shuffled())
    println(abc)
}

sorted()

sorted() function sorts the elements present in the set according to ascending order

fun main(args: Array<String>) {
    var abc = setOf(8, 6, 3, 8, 6, 7)
    println(abc.sorted())
    println(abc)
}

Manipulate Collections

sum()

sum() function adds all the elements and returns the result

fun main(args: Array<String>) {
    var abc = setOf(8, 6, 3, 8, 6, 7)
    println(abc.sum())
    println(abc)
}

toIntArray()

toIntArray() function returns an array of Int containing all of the elements of this collection.

fun main(args: Array<String>) {
    var abc = setOf(8, 6, 3, 8, 6, 7)
    println(abc.toIntArray())
}

toSet()

toSet() function returns a Set of all elements.

fun main(args: Array<String>) {
    var abc = setOf(8, 6, 3, 8, 6, 7)
    println(abc.toList())
}						

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions