List
val systemUsers: MutableList<Int> = mutableListOf(1,2,3)val sudoers: List<Int> = systemUsersfun addSystemUser(newUser: Int) {systemUsers.add(newUser)}fun getSysSudoers():List<Int> {return sudoers}fun main() {addSystemUser(4)println("Tot sudoers: ${getSysSudoers().size}")getSysSudoers().forEach {i -> println("Some useful info on user $i")}// getSysSudoers().add(5) <- Error!}
Set
val openIssues:MutableSet<String> = mutableSetOf("uniqueDescr1", "uniqueDescr2", "uniqueDescr3") fun addIssue(uniqueDesc: String): Boolean { return openIssues.add(uniqueDesc) } fun getStatusLog(isAdded: Boolean): String { return if (isAdded) "registered correctly." else "marked as duplicate and rejected." } fun main() { val aNewIssue: String = "uniqueDescr4" val anIssueAlreadyIn: String = "uniqueDescr2" println("Issue $aNewIssue ${getStatusLog(addIssue(aNewIssue))}") println("Issue &anIssueAlreadyIn ${getStatusLog(addIssue(anIssueAlreadyIn))}") }Map ```kotlin const val POINTS_X_PASS: Int = 15 val EZPassAccounts:MutableMap
= mutableMapOf(1 to 100, 2 to 100, 3 to 100) val EZPassReport: Map = EZPassAccounts
fun updataPointCredit(id:Int) { if(EZPassAccounts.containsKey(id)){ EZPassAccounts[id] = EZPassAccounts.getValue(id) + POINTS_X_PASS } else { println(“Error: Trying to update a non-existing account”) } }
fun accountReport() {
EZPassReport.forEach {
k,v -> println(“ID $k: credit $v”)
}
}
fun main() {
accountsReport() // 6
updatePointsCredit(1) // 7
updatePointsCredit(1)
updatePointsCredit(5) // 8
accountsReport() // 9
}
- ** 有关操作**
- filter
```kotlin
val numbers = listOf(1,2,3)
numbers.filter { it < 0 }
numbers.filter { x -> x>0 }
map
val numbers = listOf(1, -2, 3, -4, 5, -6) val doubled = numbers.map { x -> x*2 } val triple = numbers.map { it*3 }any,all,none
val numbers = listOf(1, -2, 3, -4, 5, -6) val anyNegative = numbers.any { it < 0 } val allEven = numbers.all { it % 2 == 0 }find,findLast
val words = listOf("Lets", "find", "something", "in", "collection", "somehow") val first = words.find {it.startsWith("some")} val last = words.findLast{it.startsWith("some")} val nothing = words.find {it.contains("nothing")}first,last,firstOrNull
val first = numbers.first() val first = numbers.last { it > 0 } val first = numbers.firstOrNull()count
val numbers = val numbers = listOf(1, -2, 3, -4, 5, -6) val totalCount = numbers.count() val evenCount = numbers.count {it%2==0}associateBy, groupBy
fun main() { data class Person(val name: String, val city: String, val phone: String) val people = listOf( Person("John", "Boston", "+1"), Person("Sarah", "Munich", "+4"), Person("Svyatoslav", "Saint-Petersburg", "+7"), Person("Vasilisa", "Saint-Petersburg", "+8")) val phoneBook = people.associateBy { it.phone } val cityBook = people.associateBy(Person::phone, Person::city) val peopleCities = people.groupBy(Person::city, Person::name) val lastPersonCity = people.associateBy(Person::city, Person::name) println("People: $people") println("Phone book: $phoneBook") println("City book: $cityBook") println("People living in each city: $peopleCities") println("Last person living in each city: $lastPersonCity") }partition
fun main() { val numbers = listOf(1, -2, 3, -4, 5, -6) val evenOdd = numbers.partition {it%2==0} val (p,n) = numbers.partition {it>0} println("${evenOdd.first}") println("$p") }flatMap
val fruitsBag = listOf("apple","orange","banana","grapes") // 1 val clothesBag = listOf("shirts","pants","jeans") val list = listOf(fruitsBag, clothesBag) val map = list.map {it} val flatMapBag = list.flatMap { it }sorted
val shuffled = listOf(5, 4, 2, 1, 3, -10) \ println(shuffled.sorted()) println(shuffled.sortedBy{-it}) println(shuffled.sortedDescending()) println(shuffled.sortedByDescending(abs(it)))取元素
val map = mapOf("key" to 42) val value1 = map["key"] val value2 = map.getValue("key") val mapWithDefault = map.withDefault {k -> k.length} val value3 = mapWithDefault.getValue("key2") try { map.getValue("anotherKey") } catch (e: NoSuchElementException) { println("Message: $e") }zip
val A = listOf("a", "b", "c") // 1 val B = listOf(1, 2, 3, 4) val resultPairs = A zip B val resultReduce = B.zip(A) {a,b -> "$a$b"}getOrElse
val list = listOf(0,10,20) println(list.getOrElse(1) {42}) println(list.getOrElse(20) {42}) //应用到map val map = mutableMapOf<String, Int?>() println(map.getOrElse("x") {1}) map["x"] =3 println(map.getOrElse("x") {1}) map["x"] = null println(map.getOrElse("x") {1})
