add remove set clear

    创建一个可变集合:

    向集合中添加一个元素:

    1. >>> mutableList.add(4)
    2. true
    3. >>> mutableList
    4. [1, 2, 3, 4]

    在下标为0的位置添加元素0 :

    1. >>> mutableList.add(0,0)
    2. >>> mutableList
    3. [0, 1, 2, 3, 4]

    删除元素1 :

    1. >>> mutableList.remove(1)
    2. true
    3. >>> mutableList
    4. [0, 2, 3, 4]
    5. >>> mutableList.remove(1)
    6. false

    删除下标为1的元素:

    1. >>> mutableList.removeAt(1)
    2. 2
    3. >>> mutableList
    4. [0, 3, 4]

    删除子集合:

    1. >>> mutableList.removeAll(listOf(3,4))
    2. true
    3. >>> mutableList
    4. [0]

    添加子集合:

    1. >>> mutableList.addAll(listOf(1,2,3))
    2. true
    3. >>> mutableList
    4. [1, 2, 3]

    更新设置下标0的元素值为100:

    1. >>> mutableList.set(0,100)
    2. 0
    3. >>> mutableList
    4. [100]

    清空集合:

    1. >>> mutableList.clear()
    2. []

    取两个集合交集:

    1. >>> val mlist1 = mutableListOf(1,2,3,4,5,6)
    2. >>> val mlist2 = mutableListOf(3,4,5,6,7,8,9)
    3. >>> mlist1.retainAll(mlist2)
    4. true
    5. >>> mlist1
    6. [3, 4, 5, 6]

    contains(element: T): Boolean

    判断集合中是否有指定元素,有就返回true,否则返回false 。 代码示例:

    1. >>> val list = listOf(1,2,3,4,5,6,7)
    2. >>> list.contains(1)
    3. true

    elementAt(index: Int): T

    查找下标对应的元素,如果下标越界会抛IndexOutOfBoundsException。 代码示例:

    1. >>> val list = listOf(1,2,3,4,5,6,7)
    2. >>> list.elementAt(6)
    3. 7
    4. >>> list.elementAt(7)
    5. java.lang.ArrayIndexOutOfBoundsException: 7
    6. at java.util.Arrays$ArrayList.get(Arrays.java:3841)

    另外,针对越界的处理,还有下面两个函数:

    elementAtOrElse(index: Int, defaultValue: (Int) -> T): T : 查找下标对应元素,如果越界会根据方法返回默认值。

    1. >>> list.elementAtOrElse(7,{0})
    2. 0
    3. >>> list.elementAtOrElse(7,{10})
    4. 10

    elementAtOrNull(index: Int): T? : 查找下标对应元素,如果越界就返回null

    1. >>> list.elementAtOrNull(7)
    2. null

    first()

    返回集合第1个元素,如果是空集,抛出异常NoSuchElementException。

    1. >>> val list = listOf(1,2,3)
    2. >>> list.first()
    3. 1
    4. >>> val emptyList = listOf<Int>()
    5. >>> emptyList.first()
    6. java.util.NoSuchElementException: List is empty.
    7. at kotlin.collections.CollectionsKt___CollectionsKt.first(_Collections.kt:178)

    对应的有针对异常处理的函数firstOrNull(): T? :

    1. >>> emptyList.firstOrNull()
    2. null

    first(predicate: (T) -> Boolean): T

    返回符合条件的第一个元素,没有则抛异常NoSuchElementException 。

    1. >>> val list = listOf(1,2,3)
    2. >>> list.first({it%2==0})
    3. 2
    4. >>> list.first({it>100})
    5. java.util.NoSuchElementException: Collection contains no element matching the predicate.

    indexOf(element: T): Int

    返回指定元素的下标,没有就返回-1

    1. >>> val list = listOf("a","b","c")
    2. >>> list.indexOf("c")
    3. 2
    4. >>> list.indexOf("x")
    5. -1

    indexOfFirst(predicate: (T) -> Boolean): Int

    返回第一个符合条件的元素下标,没有就返回-1 。

    1. >>> val list = listOf("abc","xyz","xjk","pqk")
    2. >>> list.indexOfFirst({it.contains("x")})
    3. 2
    4. >>> list.indexOfFirst({it.contains("e")})
    5. -1

    indexOfLast(predicate: (T) -> Boolean): Int

    返回最后一个符合条件的元素下标,没有就返回-1 。

    1. >>> val list = listOf("abc","xyz","xjk","pqk")
    2. >>> list.indexOfLast({it.contains("x")})
    3. 2
    4. >>> list.indexOfLast({it.contains("k")})
    5. 3
    6. >>> list.indexOfLast({it.contains("e")})
    7. -1

    last()

    返回集合最后一个元素,空集则抛出异常NoSuchElementException。

    1. >>> val list = listOf(1,2,3,4,7,5,6,7,8)
    2. >>> list.last()
    3. 8
    4. >>> val emptyList = listOf<Int>()
    5. >>> emptyList.last()
    6. java.util.NoSuchElementException: List is empty.
    7. at kotlin.collections.CollectionsKt___CollectionsKt.last(_Collections.kt:340)

    last(predicate: (T) -> Boolean): T

    返回符合条件的最后一个元素,没有就抛NoSuchElementException

    1. >>> val list = listOf(1,2,3,4,7,5,6,7,8)
    2. >>> list.last({it==7})
    3. 7
    4. >>> list.last({it>10})
    5. java.util.NoSuchElementException: List contains no element matching the predicate.

    对应的针对越界处理的lastOrNull函数:返回符合条件的最后一个元素,没有则返回null :

    1. >>> list.lastOrNull({it>10})
    2. null

    lastIndexOf(element: T): Int

    返回符合条件的最后一个元素,没有就返回-1

    1. >>> val list = listOf("abc","dfg","jkl","abc","bbc","wer")
    2. >>> list.lastIndexOf("abc")
    3. 3

    single(): T

    该集合如果只有1个元素,则返回该元素。否则,抛异常。

    1. >>> val list = listOf(1)
    2. >>> list.single()
    3. 1
    4. >>> val list = listOf(1,2)
    5. >>> list.single()
    6. java.lang.IllegalArgumentException: List has more than one element.
    7. at kotlin.collections.CollectionsKt___CollectionsKt.single(_Collections.kt:471)
    8. >>> val list = listOf<Int>()
    9. >>> list.single()
    10. java.util.NoSuchElementException: List is empty.
    11. at kotlin.collections.CollectionsKt___CollectionsKt.single(_Collections.kt:469)

    single(predicate: (T) -> Boolean): T

    返回符合条件的单个元素,如有没有符合的抛异常NoSuchElementException,或超过一个的抛异常IllegalArgumentException。

    1. >>> list.singleOrNull({it==7})
    2. null