1. 创建集合
val list = listOf ( 1 , 2 , 3 )
val arrayList = arrayListOf ( 1 , 2 , 3 )
val mutableList = mutableListOf ( 1 , 2 , 3 )
val set = setOf ( 1 , 2 , 3 )
val hashSet = hashSetOf ( 1 , 2 , 3 )
val mutableSet = mutableSetOf ( 1 , 2 , 3 )
val map = mapOf ( "a" to 1 , "b" to 2 )
val hashMap = hashMapOf ( "a" to 1 , "b" to 2 )
val mutableMap = mutableMapOf ( "a" to 1 , "b" to 2 )
2. 集合转换
var arrayList = arrayListOf ( 1 , 2 , 3 )
arrayList. add ( 4 )
arrayList. add ( 0 , 5 )
arrayList. addFirst ( 6 )
arrayList. addLast ( 7 )
arrayList. addAll ( arrayListOf ( 8 ) )
println ( arrayList)
[ 6 , 5 , 1 , 2 , 3 , 4 , 7 , 8 ]
var arrayList = arrayListOf ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 )
arrayList. remove ( 3 )
arrayList. removeAt ( 0 )
arrayList. removeFirst ( )
arrayList. removeFirstOrNull ( )
arrayList. removeLast ( )
arrayList. removeLastOrNull ( )
arrayList. removeAll ( arrayListOf ( 2 , 3 ) )
println ( arrayList)
arrayList. clear ( )
[ 5 , 6 , 7 ]
arrayList[ 0 ]
arrayList. indexOf ( 1 )
arrayList. lastIndexOf ( 1 )
arrayList. indexOfFirst { true }
arrayList. indexOfLast { true }
arrayList. firstNotNullOf { }
arrayList. firstNotNullOfOrNull { }
arrayList[ 0 ] = 1
val numbers = listOf ( 6 , 42 , 10 , 4 )
println ( "Count: ${ numbers. count ( ) } " )
println ( "Max: ${ numbers. maxOrNull ( ) } " )
println ( "Min: ${ numbers. minOrNull ( ) } " )
println ( "Average: ${ numbers. average ( ) } " )
println ( "Sum: ${ numbers. sum ( ) } " )
Count: 4
Max: 42
Min: 4
Average: 15.5
Sum: 62
val fruits = listOf ( "banana" , "avocado" , "apple" , "kiwifruit" )
fruits. filter { it. startsWith ( "a" ) }
. sortedBy { it }
. map { it. toUpperCase ( ) }
. forEach { println ( it) }
APPLE
AVOCADO
val numbersMap = mapOf ( "key1" to 1 , "key2" to 2 , "key3" to 3 , "key11" to 11 )
val filteredKeysMap = numbersMap. filterKeys { it. endsWith ( "1" ) }
val filteredValuesMap = numbersMap. filterValues { it < 10 }
println ( filteredKeysMap)
println ( filteredValuesMap)
{ key1= 1 , key11= 11 }
{ key1= 1 , key2= 2 , key3= 3 }
val numbers = listOf ( null , 1 , "two" , 3.0 , "four" )
println ( "All String elements in upper case:" )
numbers. filterIsInstance< String> ( ) . forEach {
println ( it. toUpperCase ( ) )
}
All String elements in upper case:
TWO
FOUR
val numberSets = listOf ( setOf ( 1 , 2 , 3 ) , setOf ( 4 , 5 , 6 ) , setOf ( 1 , 2 ) )
println ( numberSets. flatten ( ) )
[ 1 , 2 , 3 , 4 , 5 , 6 , 1 , 2 ]
val numbers = listOf ( "one" , "two" , "three" , "four" )
val listString = StringBuffer ( "The list of numbers: " )
numbers. joinTo ( listString)
println ( listString)
println ( numbers. joinToString ( separator = " | " , prefix = "start: " , postfix = ": end" ) )
[ one, two, three, four]
one, two, three, four
The list of numbers: one, two, three, four
start: one | two | three | four: end
val numbers = listOf ( "one" , "two" , "three" , "four" , "five" , "six" )
println ( numbers. slice ( 1 .. 3 ) )
println ( numbers. slice ( 0 .. 4 step 2 ) )
println ( numbers. slice ( setOf ( 3 , 5 , 0 ) ) )
[ two, three, four]
[ one, three, five]
[ four, six, one]
val numbers = ( 0 .. 13 ) . toList ( )
println ( numbers. chunked ( 3 ) { it. sum ( ) } )
[ 3 , 12 , 21 , 30 , 25 ]
val numbers = listOf ( "one" , "two" , "three" , "four" , "five" )
println ( numbers. groupBy { it. first ( ) . toUpperCase ( ) } )
println ( numbers. groupBy ( keySelector = { it. first ( ) } , valueTransform = { it. toUpperCase ( ) } ) )
{ O= [ one] , T= [ two, three] , F= [ four, five] }
{ o= [ ONE] , t= [ TWO, THREE] , f= [ FOUR, FIVE] }
val numbers = listOf ( "one" , "two" , "three" , "four" )
val ( match, rest) = numbers. partition { it. length > 3 }
println ( match)
println ( rest)
[ three, four]
[ one, two]
val numbers = listOf ( "one" , "two" , "three" , "four" )
println ( numbers. associateBy { it. first ( ) . toUpperCase ( ) } )
println ( numbers. associateBy ( keySelector = { it. first ( ) . toUpperCase ( ) } , valueTransform = { it. length } ) )
{ O= one, T= three, F= four}
{ O= 3 , T= 5 , F= 4 }
val colors = listOf ( "red" , "brown" , "grey" )
val animals = listOf ( "fox" , "bear" , "wolf" )
val twoAnimals = listOf ( "fox" , "bear" )
println ( colors zip animals)
println ( colors. zip ( twoAnimals) )
[ ( red, fox) , ( brown, bear) , ( grey, wolf) ]
[ ( red, fox) , ( brown, bear) ]
val numbers = listOf ( "one" , "two" , "three" , "four" )
println ( numbers. any { it. endsWith ( "e" ) } )
println ( numbers. none { it. endsWith ( "a" ) } )
println ( numbers. all { it. endsWith ( "e" ) } )
true
true
false
val numbers = setOf ( "one" , "two" , "three" , "four" )
println ( numbers union setOf ( "four" , "five" ) )
println ( setOf ( "four" , "five" ) union numbers)
println ( numbers intersect setOf ( "two" , "one" ) )
println ( numbers subtract setOf ( "three" , "four" ) )
println ( numbers subtract setOf ( "four" , "three" ) )
[ one, two, three, four, five]
[ four, five, one, two, three]
[ one, two]
[ one, two]
[ one, two]
3. 集合排序
val numbers = mutableListOf ( "one" , "two" , "three" , "four" )
numbers. sort ( )
println ( "Sort into ascending: $ numbers " )
numbers. sortDescending ( )
println ( "Sort into descending: $ numbers " )
Sort into ascending: [ four, one, three, two]
Sort into descending: [ two, three, one, four]
numbers. sortBy { it. length }
println ( "Sort into ascending by length: $ numbers " )
numbers. sortByDescending { it. last ( ) }
println ( "Sort into descending by the last letter: $ numbers " )
Sort into ascending by length: [ two, one, four, three]
Sort into descending by the last letter: [ four, two, one, three]
val lengthComparator = Comparator { str1: String, str2: String -> str1. length - str2. length }
println ( listOf ( "aaa" , "bb" , "c" ) . sortedWith ( lengthComparator) )
[ c, bb, aaa]