Restore Map.minus(key/keys) and Map.minusAssign(key/keys) operators.
#KT-13353
This commit is contained in:
@@ -602,6 +602,81 @@ public inline operator fun <K, V> MutableMap<in K, in V>.plusAssign(map: Map<K,
|
||||
putAll(map)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a map containing all entries of the original map except the entry with the given [key].
|
||||
*
|
||||
* The returned map preserves the entry iteration order of the original map.
|
||||
*/
|
||||
@SinceKotlin("1.1")
|
||||
public operator fun <K, V> Map<out K, V>.minus(key: K): Map<K, V>
|
||||
= this.toMutableMap().apply { minusAssign(key) }.optimizeReadOnlyMap()
|
||||
|
||||
/**
|
||||
* Returns a map containing all entries of the original map except those entries
|
||||
* the keys of which are contained in the given [keys] collection.
|
||||
*
|
||||
* The returned map preserves the entry iteration order of the original map.
|
||||
*/
|
||||
@SinceKotlin("1.1")
|
||||
public operator fun <K, V> Map<out K, V>.minus(keys: Iterable<K>): Map<K, V>
|
||||
= this.toMutableMap().apply { minusAssign(keys) }.optimizeReadOnlyMap()
|
||||
|
||||
/**
|
||||
* Returns a map containing all entries of the original map except those entries
|
||||
* the keys of which are contained in the given [keys] array.
|
||||
*
|
||||
* The returned map preserves the entry iteration order of the original map.
|
||||
*/
|
||||
@SinceKotlin("1.1")
|
||||
public operator fun <K, V> Map<out K, V>.minus(keys: Array<out K>): Map<K, V>
|
||||
= this.toMutableMap().apply { minusAssign(keys) }.optimizeReadOnlyMap()
|
||||
|
||||
/**
|
||||
* Returns a map containing all entries of the original map except those entries
|
||||
* the keys of which are contained in the given [keys] sequence.
|
||||
*
|
||||
* The returned map preserves the entry iteration order of the original map.
|
||||
*/
|
||||
@SinceKotlin("1.1")
|
||||
public operator fun <K, V> Map<out K, V>.minus(keys: Sequence<K>): Map<K, V>
|
||||
= this.toMutableMap().apply { minusAssign(keys) }.optimizeReadOnlyMap()
|
||||
|
||||
/**
|
||||
* Removes the entry with the given [key] from this mutable map.
|
||||
*/
|
||||
@SinceKotlin("1.1")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline operator fun <K, V> MutableMap<K, V>.minusAssign(key: K) {
|
||||
remove(key)
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all entries the keys of which are contained in the given [keys] collection from this mutable map.
|
||||
*/
|
||||
@SinceKotlin("1.1")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline operator fun <K, V> MutableMap<K, V>.minusAssign(keys: Iterable<K>) {
|
||||
this.keys.removeAll(keys)
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all entries the keys of which are contained in the given [keys] array from this mutable map.
|
||||
*/
|
||||
@SinceKotlin("1.1")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline operator fun <K, V> MutableMap<K, V>.minusAssign(keys: Array<out K>) {
|
||||
this.keys.removeAll(keys)
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all entries from the keys of which are contained in the given [keys] sequence from this mutable map.
|
||||
*/
|
||||
@SinceKotlin("1.1")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline operator fun <K, V> MutableMap<K, V>.minusAssign(keys: Sequence<K>) {
|
||||
this.keys.removeAll(keys)
|
||||
}
|
||||
|
||||
|
||||
// do not expose for now @PublishedApi
|
||||
internal fun <K, V> Map<K, V>.optimizeReadOnlyMap() = when (size) {
|
||||
|
||||
@@ -413,6 +413,41 @@ class MapTest {
|
||||
}
|
||||
|
||||
|
||||
fun testMinus(doMinus: (Map<String, Int>) -> Map<String, Int>) {
|
||||
val original = mapOf("A" to 1, "B" to 2)
|
||||
val shortened = doMinus(original)
|
||||
assertEquals("A" to 1, shortened.entries.single().toPair())
|
||||
}
|
||||
|
||||
@Test fun minus() = testMinus { it - "B" - "C" }
|
||||
|
||||
@Test fun minusList() = testMinus { it - listOf("B", "C") }
|
||||
|
||||
@Test fun minusArray() = testMinus { it - arrayOf("B", "C") }
|
||||
|
||||
@Test fun minusSequence() = testMinus { it - sequenceOf("B", "C") }
|
||||
|
||||
@Test fun minusSet() = testMinus { it - setOf("B", "C") }
|
||||
|
||||
|
||||
|
||||
fun testMinusAssign(doMinusAssign: (MutableMap<String, Int>) -> Unit) {
|
||||
val original = hashMapOf("A" to 1, "B" to 2)
|
||||
doMinusAssign(original)
|
||||
assertEquals("A" to 1, original.entries.single().toPair())
|
||||
}
|
||||
|
||||
@Test fun minusAssign() = testMinusAssign {
|
||||
it -= "B"
|
||||
it -= "C"
|
||||
}
|
||||
|
||||
@Test fun minusAssignList() = testMinusAssign { it -= listOf("B", "C") }
|
||||
|
||||
@Test fun minusAssignArray() = testMinusAssign { it -= arrayOf("B", "C") }
|
||||
|
||||
@Test fun minusAssignSequence() = testMinusAssign { it -= sequenceOf("B", "C") }
|
||||
|
||||
|
||||
fun testIdempotent(operation: (Map<String, Int>) -> Map<String, Int>) {
|
||||
val original = mapOf("A" to 1, "B" to 2)
|
||||
|
||||
+4
@@ -1850,6 +1850,10 @@ public final class kotlin/collections/MapsKt {
|
||||
public static final fun mapValuesTo (Ljava/util/Map;Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Ljava/util/Map;
|
||||
public static final fun minBy (Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Ljava/util/Map$Entry;
|
||||
public static final fun minWith (Ljava/util/Map;Ljava/util/Comparator;)Ljava/util/Map$Entry;
|
||||
public static final fun minus (Ljava/util/Map;Ljava/lang/Iterable;)Ljava/util/Map;
|
||||
public static final fun minus (Ljava/util/Map;Ljava/lang/Object;)Ljava/util/Map;
|
||||
public static final fun minus (Ljava/util/Map;Lkotlin/sequences/Sequence;)Ljava/util/Map;
|
||||
public static final fun minus (Ljava/util/Map;[Ljava/lang/Object;)Ljava/util/Map;
|
||||
public static final fun mutableMapOf ([Lkotlin/Pair;)Ljava/util/Map;
|
||||
public static final fun none (Ljava/util/Map;)Z
|
||||
public static final fun none (Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Z
|
||||
|
||||
@@ -1677,6 +1677,10 @@ public final class kotlin/collections/MapsKt {
|
||||
public static final fun mapValuesTo (Ljava/util/Map;Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Ljava/util/Map;
|
||||
public static final fun minBy (Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Ljava/util/Map$Entry;
|
||||
public static final fun minWith (Ljava/util/Map;Ljava/util/Comparator;)Ljava/util/Map$Entry;
|
||||
public static final fun minus (Ljava/util/Map;Ljava/lang/Iterable;)Ljava/util/Map;
|
||||
public static final fun minus (Ljava/util/Map;Ljava/lang/Object;)Ljava/util/Map;
|
||||
public static final fun minus (Ljava/util/Map;Lkotlin/sequences/Sequence;)Ljava/util/Map;
|
||||
public static final fun minus (Ljava/util/Map;[Ljava/lang/Object;)Ljava/util/Map;
|
||||
public static final fun mutableMapOf ([Lkotlin/Pair;)Ljava/util/Map;
|
||||
public static final fun none (Ljava/util/Map;)Z
|
||||
public static final fun none (Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Z
|
||||
|
||||
Reference in New Issue
Block a user