Kotlin集合扩展函数 - 创建集合
Kotlin标准包提供了一系列的扩展函数用来创建集合,包括Array,List,Set,Map以及可变集合。
这里需要指出示例里的intList为
<span class="hljs">val</span> intList: List<Int> = listOf(<span class="hljs">1</span>, <span class="hljs">2</span>, <span class="hljs">3</span>)
Array
创建数组基本方式
<span class="hljs">val</span> intArray: Array<<span class="hljs">Int</span>> = arrayOf(<span class="hljs">1</span>, <span class="hljs">2</span>, <span class="hljs">3</span>)
创建基本类型数组
<span class="hljs">val</span> primitiveShortArray: ShortArray = shortArrayOf(<span class="hljs">1</span>,<span class="hljs">2</span>,<span class="hljs">3</span>)
<span class="hljs">val</span> primitiveIntArray: IntArray = intArrayOf(<span class="hljs">1</span>, <span class="hljs">2</span>, <span class="hljs">3</span>)
<span class="hljs">val</span> primitiveLongArray: LongArray = longArrayOf(<span class="hljs">1</span>,<span class="hljs">2</span>,<span class="hljs">3</span>)
<span class="hljs">val</span> primitiveFloatArray: FloatArray = floatArrayOf(<span class="hljs">1</span>,<span class="hljs">2</span>,<span class="hljs">3</span>)
<span class="hljs">val</span> primitiveDoubleArray: DoubleArray = doubleArrayOf(<span class="hljs">1</span>,<span class="hljs">2</span>,<span class="hljs">3</span>)
<span class="hljs">val</span> primitiveBooleanArray: BooleanArray = booleanArrayOf(<span class="hljs">true</span>,<span class="hljs">false</span>,<span class="hljs">true</span>)
<span class="hljs">val</span> primitiveCharArray: CharArray = charArrayOf(<span class="hljs">'a'</span>,<span class="hljs">'b'</span>,<span class="hljs">'c'</span>)
空数组
<span class="hljs">val</span> emptyArray: Array<Int> = emptyArray()
可空类型数组
<span class="hljs">val</span> nullableArray: Array<<span class="hljs">Int</span>?> = arrayOfNulls(<span class="hljs">10</span>)
复制数组
<span class="hljs">val</span> copyOfArray: Array<<span class="hljs">Int</span>> = intArray.copyOf()
部分复制数组
<span class="hljs">val</span> partialCopyOfArray: Array<<span class="hljs">Int</span>> = intArray.copyOfRange(<span class="hljs">0</span>, <span class="hljs">2</span>)
List
创建列表基本方式
<span class="hljs">val</span> intList: List<Int> = listOf(<span class="hljs">1</span>, <span class="hljs">2</span>, <span class="hljs">3</span>)
<span class="hljs">//或</span>
<span class="hljs">val</span> intArrayList: List<Int> = arrayListOf(<span class="hljs">1</span>, <span class="hljs">2</span>, <span class="hljs">3</span>)
空列表
<span class="hljs">val</span> emptyList: List<Int> = emptyList()
<span class="hljs">//或</span>
<span class="hljs">val</span> eptyList1: List<<span class="hljs">Int</span>> = listOf()
去掉null元素的列表
<span class="hljs">val</span> listWithNonNullElements: List<Int> = listOfNotNull(<span class="hljs">1</span>, <span class="hljs">null</span>, <span class="hljs">3</span>)
<span class="hljs">//等同于</span>
List(<span class="hljs">1</span>,<span class="hljs">3</span>)
Set
创建集合的基本方式
<span class="hljs">val</span> aSet: Set<Int> = setOf(<span class="hljs">1</span>)
<span class="hljs">//或</span>
<span class="hljs">val</span> hasSet: Set<<span class="hljs">Int</span>> = hashSetOf(<span class="hljs">1</span>)
<span class="hljs">//或</span>
<span class="hljs">val</span> linkedSet: Set<<span class="hljs">Int</span>>linkedSerOf(<span class="hljs">1</span>)
空集合
<span class="hljs">val</span> emptySet: Set<Int> = emptySet()
<span class="hljs">//或</span>
<span class="hljs">val</span> emptySet1: Set<<span class="hljs">Int</span>> =setOf()
<span class="hljs">//或</span>
<span class="hljs">val</span> emptySet2: Set<<span class="hljs">Int</span>> =hashSetOf()
<span class="hljs">//或</span>
<span class="hljs">val</span> emptySet3: Set<<span class="hljs">Int</span>> =linkedSetOf()
Map
创建Map的基本方式
<span class="hljs">val</span> aMap: Map<String, <span class="hljs">Int</span>> = mapOf(<span class="hljs">"hi"</span> to <span class="hljs">1</span>, <span class="hljs">"hello"</span> to <span class="hljs">2</span>)
空Map
<span class="hljs">val</span> emptyMap: Map<String, <span class="hljs">Int</span>> = emptyMap()
可变集合
可变List
<span class="hljs">val</span> mutableList: MutableList<<span class="hljs">Int</span>> = mutableListOf(<span class="hljs">1</span>, <span class="hljs">2</span>, <span class="hljs">3</span>)
可变Set
<span class="hljs">val</span> mutableSet: MutableSet<Int> = mutableSetOf(<span class="hljs">1</span>)
可变Map
<span class="hljs">var</span> mutableMap: MutableMap<String, <span class="hljs">Int</span>> = mutableMapOf(<span class="hljs">"hi"</span> to <span class="hljs">1</span>, <span class="hljs">"hello"</span> to <span class="hljs">2</span>)