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: 2.1.0-Beta1
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 {
    private const val serialVersionUID: Long = 3406603774387020532

    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 set with the given elements.
 * Elements of the set are iterated in the order they were specified.
 * 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.
 * Elements of the set are iterated in the order they were specified.
 */
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.
 * Elements of the set are iterated in the order they were specified.
 */
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()

/**
 * Returns an immutable set containing only the specified object [element].
 * The returned set is serializable.
 */
@JvmVersion
public fun  setOf(element: T): Set = Collections.singleton(element)


/**
 * Returns a new [SortedSet] with the given elements.
 */
@JvmVersion
public fun  sortedSetOf(vararg elements: T): TreeSet = elements.toCollection(TreeSet())

/**
 * Returns a new [SortedSet] with the given [comparator] and elements.
 */
@JvmVersion
public fun  sortedSetOf(comparator: Comparator, vararg elements: T): TreeSet = elements.toCollection(TreeSet(comparator))


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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy