Set in Kotlin

Learn via video courses
Topics Covered

Overview

Set in Kotlin is a generic, unordered collection of elements designed to enforce uniqueness. Kotlin categorizes sets into two primary types: immutable and mutable. The setOf() function creates an immutable set, allowing only read-only operations, while the mutableSetOf() function generates a mutable set, enabling both reading and writing. These sets are indispensable tools for efficiently managing collections with distinct and non-duplicating elements.

Syntax

  • The setOf function creates a read-only set (Set).
  • It accepts a variable number of elements (vararg elements).
  • Elements can be of any data type due to the use of generics (<T>).
  • The original order of elements is preserved in the set.

Properties of Set Interface

PropertyDescription
abstract val size: IntReturns the size of the collection.

Functions of Set Interface

Here are the functions of set in Kotlin in a table format:

FunctionDescription
contains(element: E): BooleanChecks if a specified element is present in the collection. Returns true if found, otherwise false.
containsAll(elements: Collection): BooleanVerifies if all specified elements from a given collection are present in the collection. Returns true if all elements are found, otherwise false.
isEmpty(): BooleanDetermines if the collection is empty (contains no elements). Returns true for an empty collection, otherwise false.
iterator(): IteratorReturns an iterator that can be used to traverse the elements of the set.
all(predicate: (T) -> Boolean): BooleanReturns true if all elements match a given predicate.
any(): BooleanReturns true if the collection contains at least one element.
count(predicate: (T) -> Boolean): IntReturns the total number of elements that match a given predicate.
distinct(): ListReturns a list containing only distinct elements from the collection.
drop(n: Int): ListReturns a list that contains all elements except the first n elements.
elementAtOrElse(index: Int, defaultValue: (Int) -> T): TReturns an element at the given index or the result of calling the defaultValue function if the index is out of bounds in the collection.
filter(predicate: (T) -> Boolean): ListReturns a list containing only the elements that match a given predicate.
filterIndexed(predicate: (index: Int, T) -> Boolean): ListReturns a list containing only the elements that match a given predicate, with access to their index.
filterNot(predicate: (T) -> Boolean): ListReturns a list containing only elements that do not match a given predicate.
find(predicate: (T) -> Boolean): T?Returns the first element that matches a given predicate, or null if no such element is found.
findLast(predicate: (T) -> Boolean): T?Returns the last element that matches a given predicate, or null if no such element is found.
first(): TReturns the first element in the collection.
first(predicate: (T) -> Boolean): TReturns the first element that matches a given predicate.
firstOrNull(): T?Returns the first element or null if the collection is empty.
indexOf(element: T): IntReturns the first index of a given element, or -1 if the element is not found in the collection.
indexOfFirst(predicate: (T) -> Boolean): IntReturns the index of the first element that matches a given predicate, or -1 if no such element is found.
indexOfLast(predicate: (T) -> Boolean): IntReturns the index of the last element that matches a given predicate, or -1 if no such element is found.
intersect(other: Iterable): SetReturns a set containing all elements present in both the original set and the given collection.
isNotEmpty(): BooleanReturns true if the collection is not empty.
last(): TReturns the last element in the collection.
last(predicate: (T) -> Boolean): TReturns the last element that matches a given predicate.
lastIndexOf(element: T): IntReturns the last index of a given element, or -1 if the element is not found.
lastOrNull(): T?Returns the last element or null if the collection is empty.
lastOrNull(predicate: (T) -> Boolean): T?Returns the last element that matches a given predicate, or null if no such element is found.
max(): T?Returns the largest element or null if there are no elements in the collection.
maxBy(selector: (T) -> R): T?Returns the first element that yields the largest value when applying the given function, or null if the collection is empty.
min(): T?Returns the smallest element or null if the collection is empty.
minBy(selector: (T) -> R): T?Returns the first element that yields the smallest value when applying the given function, or null if the collection is empty.
minus(element: T): SetReturns a set containing all elements of the original set except for the specified element.
minus(elements: Iterable): SetReturns a set containing all elements of the original set except for those found in the specified elements collection.
minusElement(element: T): SetReturns a set containing all elements of the original set except for the given element.
plus(element: T): SetReturns a set containing all elements of the original set as well as the given element if it is not already present in the set.
plus(elements: Iterable): SetReturns a set containing all elements of the original set as well as the given elements collection which is not already present in the set. The returned set preserves the iteration of elements in the same order as the original set.
plusElement(element: T): SetReturns a set containing all elements of the original set as well as the given element.
reversed(): ListReturns a list with elements in reverse order.
single(): TReturns the single element or throws an exception if the collection has more than one element or is empty.
singleOrNull(): T?Returns a single element or null if the collection has more than one element or is empty.

Note:

In this table, T serves as the generic type parameter, and its specific meaning can vary based on how you use these collection functions in your Kotlin code.

Examples

1. Kotlin Set Interface

Code:

Output :

2. Kotlin Set Interface - contains() and containsAll()

Code:

Output :

3. Kotlin Set Interface - isEmpty() and isNotEmpty()

Code:

Output:

4. Kotlin Set Interface - drop()

Code:

Output :

5. Kotlin Set Interface - elementAt() and elementAtOrNull()

Code:

Output :

Conclusion

  • Kotlin's Set interface provides a generic, unordered collection of unique elements. It supports mutable and immutable sets, with setOf() for read-only and mutableSetOf() for read-write functionality.

  • The setOf() function creates a read-only set in Kotlin, accepting a variable number of elements. It preserves the order of elements, making it useful for maintaining element sequence.

  • size property returns the size of the collection.

  • Functions of Set in Kotlin Interface encompass a diverse range, including element containment checks, filtering, finding, and set operations such as union and difference