Introduce associateWith and associateWithTo functions
#KT-13814
This commit is contained in:
@@ -358,6 +358,18 @@ class CollectionTest {
|
||||
assertEquals(namesByTeam, mutableNamesByTeam)
|
||||
}
|
||||
|
||||
@Test fun associateWith() {
|
||||
val items = listOf("Alice", "Bob", "Carol")
|
||||
val itemsWithTheirLength = items.associateWith { it.length }
|
||||
|
||||
assertEquals(mapOf("Alice" to 5, "Bob" to 3, "Carol" to 5), itemsWithTheirLength)
|
||||
|
||||
val updatedLength =
|
||||
items.drop(1).associateWithTo(itemsWithTheirLength.toMutableMap()) { name -> name.toLowerCase().count { it in "aeuio" }}
|
||||
|
||||
assertEquals(mapOf("Alice" to 5, "Bob" to 1, "Carol" to 2), updatedLength)
|
||||
}
|
||||
|
||||
@Test fun plusRanges() {
|
||||
val range1 = 1..3
|
||||
val range2 = 4..7
|
||||
|
||||
@@ -548,6 +548,18 @@ public class SequenceTest {
|
||||
assertEquals(listOf("act", "wast", "test"), sequenceOf("act", "test", "wast").sortedWith(comparator).toList())
|
||||
}
|
||||
|
||||
@Test fun associateWith() {
|
||||
val items = sequenceOf("Alice", "Bob", "Carol")
|
||||
val itemsWithTheirLength = items.associateWith { it.length }
|
||||
|
||||
assertEquals(mapOf("Alice" to 5, "Bob" to 3, "Carol" to 5), itemsWithTheirLength)
|
||||
|
||||
val updatedLength =
|
||||
items.drop(1).associateWithTo(itemsWithTheirLength.toMutableMap()) { name -> name.toLowerCase().count { it in "aeuio" }}
|
||||
|
||||
assertEquals(mapOf("Alice" to 5, "Bob" to 1, "Carol" to 2), updatedLength)
|
||||
}
|
||||
|
||||
@Test fun orEmpty() {
|
||||
val s1: Sequence<Int>? = null
|
||||
assertEquals(emptySequence(), s1.orEmpty())
|
||||
|
||||
Reference in New Issue
Block a user