跳到主內容

Kotlin集合運算子整理總表Iterable/Collection/List/Set/Map

出處 : https://blog.csdn.net/vitaviva/article/details/107587134

建立集合 

不可變更集合 immutable

import java.util.*
import java.text.SimpleDateFormat
/**
 * You can edit, run, and share this code.
 * play.kotlinlang.org
 */
fun main() {
    val list = listOf(1,2,3, null)
    // 不為 null List
    val notNullList = listOfNotNull(null,1,2,3,null)
    val map = mapOf("foo" to "FOO", "bar" to "BAR", "bar" to "BB")
    val set = setOf(4,5,6,6)
    println(list) 			// 	[1, 2, 3, null]
    println(notNullList) 	//	[1, 2, 3]
    println(map)			// 	{foo=FOO, bar=BB}
    println(set)			// 	[4, 5, 6]
}
  

 

可變更集合 mutable

import java.util.*
import java.text.SimpleDateFormat
/**
 * You can edit, run, and share this code.
 * play.kotlinlang.org
 */
fun main() {
  val list = mutableListOf(1,2,3,3)
  val map = mutableMapOf("foo" to "FOO", "bar" to "BAR", "bar" to "BB")
  val set = mutableSetOf(1,2,3,3)
  println(list) // [1, 2, 3, 3]
  println(map)	// {foo=FOO, bar=BB}
  println(set)	// [1, 2, 3]
}
  

 

addAll(), list 相加

import java.util.*
import java.text.SimpleDateFormat

fun main() {
	val list1 = mutableListOf(1,2,3,4)
    val list2 = mutableListOf(5,6,7,8)
  	println(list1.addAll(list2)) // true (注意當下有返回值)
    println(list1)				 // [1, 2, 3, 4, 5, 6, 7, 8
        
}

 

remove(),  list 刪除

import java.util.*
import java.text.SimpleDateFormat

fun main() {
	
    val numberList = mutableListOf(1,2,3)
    numberList.remove(2)
    
    println(numberList)		// [1, 3]
}
  

 

 索引

import java.util.*
import java.text.SimpleDateFormat

fun main() {
	val list = listOf(1,2,3)
    val indices: IntRange = list.indices
    println(indices)
    
    for(i in list.indices){
        println(list[i])
    }
    /*
     0..2
    1
    2
    3
    */
    println(list.first()) 	// 1
    println(list.last())	// 3
    println(list.lastIndex) // 2 = ( size - 1 )
    println(list.size)		// 3
}

Stream 與 kotlin 比較

image-1704942398795.png


List 相關

list.map{}

import java.util.*
import java.text.SimpleDateFormat

fun main() {
	val nameList = listOf("Sam","John",null,"Mary")
    val resultList = nameList.map{ "name:" + it }
    println(resultList) 	// [name:Sam, name:John, name:null, name:Mary]
    
    // 過濾null
    val resultList2 = nameList.mapNotNull{ it }
    println(resultList2) 	// [Sam, John, Mary]
}
  

list 保留或排除(removeAll(), retainAll())

import java.util.*
import java.text.SimpleDateFormat

fun main() {
	val peopleList = mutableListOf(People("sam",18),People("John",23),People("Mary",30))
    // 移除 age > 25
    peopleList.removeAll{ it.age > 25 } // [People(name=sam, age=18), People(name=John, age=23)]
    println(peopleList)
    
    // 保留 name == sam
    val peopleList2 = mutableListOf(People("sam",18),People("John",23),People("Mary",30))
    peopleList2.retainAll{ it.name == "sam" } // [People(name=sam, age=18)]
    println(peopleList2)
}
  

 

list 轉 map

import java.util.*
import java.text.SimpleDateFormat

fun main() {
	val peopleList = listOf(People("Sam",12),People("Mary",18), People("Sam",13))
    // 指定 key : value
    val map = peopleList.associate { it.name to it }
    // 指定 key , value => 固定為物件
    val map2 = peopleList.associateBy { "name:" + it.name }
    println(map) 	// {Sam=People(name=Sam, age=13), Mary=People(name=Mary, age=18)}
    println(map2)	// {name:Sam=People(name=Sam, age=13), name:Mary=People(name=Mary, age=18)}
    
}
  
data class People(
	val name:String,
    val age: Int
)

isEmpty()

import java.util.*
import java.text.SimpleDateFormat

fun main() {
	val list1 = listOf(1,2,3,4)
    val list2 = emptyList<Int>()
    var list3:List<Int>? = null
    
    println(list1.isEmpty())		// flase
    println(list2.isEmpty())		// true
    println(list3?.isEmpty())		// null
    
    println(list1.orEmpty())		// [1, 2, 3, 4]
    println(list2.orEmpty())		// []
    println(list3?.orEmpty())		// null
}

 

getOrElse() ,getOrNull()

import java.util.*
import java.text.SimpleDateFormat

fun main() {
	val list = listOf(1,2,3,4)
   
  	println(list.getOrElse(1,{9}))	// 1
    println(list.getOrElse(5,{9}))	// 9
    
    println(list.getOrNull(1))	// 2
    println(list.getOrNull(5))	// null
}

 


Map 相關

containsKey(),  包含 key 

import java.util.*
import java.text.SimpleDateFormat

fun main() {
	
   	val numberMap = mapOf("one" to 1,"two" to 2)
    
    println(numberMap.containsKey("one"))	// true
    
    val worldMap = mapOf("one" to "ONE","two" to "TWO")
    println(worldMap.containsValue("one"))	// false
    println(worldMap.containsValue("ONE"))	// true
}