All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kotlin.collections.Sets.kt Maven / Gradle / Ivy

There is a newer version: 1.0.7
Show newest version
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("SetsKt")

package kotlin.collections

import java.io.Serializable
import java.util.*


internal object EmptySet : Set, Serializable {
    override fun equals(other: Any?): Boolean = other is Set<*> && other.isEmpty()
    override fun hashCode(): Int = 0
    override fun toString(): String = "[]"

    override val size: Int get() = 0
    override fun isEmpty(): Boolean = true
    override fun contains(element: Nothing): Boolean = false
    override fun containsAll(elements: Collection): Boolean = elements.isEmpty()

    override fun iterator(): Iterator = EmptyIterator

    private fun readResolve(): Any = EmptySet
}


/** Returns an empty read-only set.  The returned set is serializable (JVM). */
public fun  emptySet(): Set = EmptySet
/** Returns a new read-only ordered set with the given elements.  The returned set is serializable (JVM). */
public fun  setOf(vararg elements: T): Set = if (elements.size > 0) elements.toSet() else emptySet()

/** Returns an empty read-only set.  The returned set is serializable (JVM). */
@kotlin.internal.InlineOnly
public inline fun  setOf(): Set = emptySet()

/** Returns a new [MutableSet] with the given elements. */
public fun  mutableSetOf(vararg elements: T): MutableSet = elements.toCollection(LinkedHashSet(mapCapacity(elements.size)))

/** Returns a new [HashSet] with the given elements. */
public fun  hashSetOf(vararg elements: T): HashSet = elements.toCollection(HashSet(mapCapacity(elements.size)))

/** Returns a new [LinkedHashSet] with the given elements. */
public fun  linkedSetOf(vararg elements: T): LinkedHashSet = elements.toCollection(LinkedHashSet(mapCapacity(elements.size)))

/** Returns this Set if it's not `null` and the empty set otherwise. */
@kotlin.internal.InlineOnly
public inline fun  Set?.orEmpty(): Set = this ?: emptySet()

/* Not available on platform: JS */







/* Not available on platform: JS */





/* Not available on platform: JS */






internal fun  Set.optimizeReadOnlySet() = when (size) {
    0 -> emptySet()
    1 -> setOf(iterator().next())
    else -> this
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy