kotlin.collections.MutableCollections.kt Maven / Gradle / Ivy
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("CollectionsKt")
package kotlin
/**
* Adds the specified [element] to this mutable collection.
*/
public operator fun MutableCollection.plusAssign(element: T) {
this.add(element)
}
/**
* Adds all elements of the given [collection] to this mutable collection.
*/
public operator fun MutableCollection.plusAssign(collection: Iterable) {
this.addAll(collection)
}
/**
* Adds all elements of the given [array] to this mutable collection.
*/
public operator fun MutableCollection.plusAssign(array: Array) {
this.addAll(array)
}
/**
* Adds all elements of the given [sequence] to this mutable collection.
*/
public operator fun MutableCollection.plusAssign(sequence: Sequence) {
this.addAll(sequence)
}
/**
* Removes a single instance of the specified [element] from this mutable collection.
*/
public operator fun MutableCollection.minusAssign(element: T) {
this.remove(element)
}
/**
* Removes all elements contained in the given [collection] from this mutable collection.
*/
public operator fun MutableCollection.minusAssign(collection: Iterable) {
this.removeAll(collection)
}
/**
* Removes all elements contained in the given [array] from this mutable collection.
*/
public operator fun MutableCollection.minusAssign(array: Array) {
this.removeAll(array)
}
/**
* Removes all elements contained in the given [sequence] from this mutable collection.
*/
public operator fun MutableCollection.minusAssign(sequence: Sequence) {
this.removeAll(sequence)
}
/**
* Adds all elements of the given [iterable] to this [MutableCollection].
*/
public fun MutableCollection.addAll(iterable: Iterable) {
when (iterable) {
is Collection -> addAll(iterable)
else -> for (item in iterable) add(item)
}
}
/**
* Adds all elements of the given [sequence] to this [MutableCollection].
*/
public fun MutableCollection.addAll(sequence: Sequence) {
for (item in sequence) add(item)
}
/**
* Adds all elements of the given [array] to this [MutableCollection].
*/
public fun MutableCollection.addAll(array: Array) {
addAll(array.asList())
}
/**
* Removes all elements from this [MutableCollection] that are also contained in the given [iterable].
*/
public fun MutableCollection.removeAll(iterable: Iterable) {
removeAll(iterable.convertToSetForSetOperationWith(this))
}
/**
* Removes all elements from this [MutableCollection] that are also contained in the given [sequence].
*/
public fun MutableCollection.removeAll(sequence: Sequence) {
val set = sequence.toHashSet()
if (set.isNotEmpty())
removeAll(set)
}
/**
* Removes all elements from this [MutableCollection] that are also contained in the given [array].
*/
public fun MutableCollection.removeAll(array: Array) {
if (array.isNotEmpty())
removeAll(array.toHashSet())
// else
// removeAll(emptyList())
}
/**
* Retains only elements of this [MutableCollection] that are contained in the given [iterable].
*/
public fun MutableCollection.retainAll(iterable: Iterable) {
retainAll(iterable.convertToSetForSetOperationWith(this))
}
/**
* Retains only elements of this [MutableCollection] that are contained in the given [array].
*/
public fun MutableCollection.retainAll(array: Array) {
if (array.isNotEmpty())
retainAll(array.toHashSet())
else
clear()
// retainAll(emptyList())
}
/**
* Retains only elements of this [MutableCollection] that are contained in the given [sequence].
*/
public fun MutableCollection.retainAll(sequence: Sequence) {
val set = sequence.toHashSet()
if (set.isNotEmpty())
retainAll(set)
else
clear()
}