kotlin val numbers = listOf(1, 2, 3, 4, 5) val squaredNumbers = numbers.map { it * it } val names = listOf("Alice", "Bob", "Charlie") val nameLengths = names.map { it.length } val nestedList = listOf(listOf(1, 2), listOf(3, 4), listOf(5, 6)) val flattenedList = nestedList.flatMap { it } kotlin val numbers = listOf(1, 2, 3, 4, 5) val evenNumbers = numbers.filter { it % 2 == 0 } val mixedList = listOf(1, "two", null, 3.14, "four") val nonNullStrings = mixedList.filterNotNull() val anyTypeList: List<Any> = listOf(1, "two", 3.14, 'c', listOf(4, 5)) val stringList = anyTypeList.filterIsInstance<String>() kotlin val numbers = listOf(3, 5, 1, 4, 2) val ascendingOrder = numbers.sorted() val names = listOf("Alice", "Bob", "Charlie") val nameLengthOrder = names.sortedBy { it.length } data class Person(val name: String, val age: Int) val people = listOf(Person("Alice", 25), Person("Bob", 30), Person("Charlie", 20)) val ageOrder = people.sortedWith(compareBy { it.age }) kotlin val list1 = listOf("a", "b") val list2 = listOf("b", "c") val combinedList = list1 + list2 val numbers = setOf(1, 2, 3, 4, 5) val evenNumbers = setOf(2, 4, 6, 8) val subtractedNumbers = numbers - evenNumbers val set1 = setOf(1, 2, 3) val set2 = setOf(2, 3, 4) val unionSet = set1.union(set2) groovy dependencies { implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.30' }


上一篇:
下一篇:
切换中文