Kotlin

Kotlin - Collection 2

J_Bin 2022. 4. 21. 22:52
fun main(){

    // mapIndexed() : 인자 * 인덱스
    val numbers = 1..5
    //numbers.mapIndexed{idx,number -> idx * number}.forEach{ println(it)}
    // [1,2,3,4] [0,1,2,3] -> 0, 2, 6, 12



    val cities = listOf("Seoul","Tokyo","London","Jungle","Pusan")
    val things = listOf("nike","adidas","lamp","ipod")
    // take() : 앞에서부터 반환
    //cities.take(3).forEach{ println(it)}        // Seoul,Tokyo,London

    // takeLast() : 뒤에서부터 반환
    //cities.takeLast(2).forEach{ println(it)}      // Jungle,Pusan

    // takeWhile() : 앞부터 주어진 조건에 만족하는 인자들까지 반환
    // 앞에서부터 길이가 5인 도시들만 반환
    // "Pusan"도 길이가 5이지만 앞에서부터 시작했을 때 "London"의 길이가 6이므로 끊긴다
    //cities.takeWhile { city -> city.length == 5 }.forEach{ println(it)} //Seoul,Tokyo,Pusan

    // takeLastWhile() : 뒤에서부터 주어진 조건에 만족하는 인자들까지 반환
    // 뒤에서 부터 길이가 4인 것들을 반환
    // "nike"도 길이가 4이지만 뒤에서부터 봤을 때 "adidas"가 조건에 맞지 않으므로 끊긴다.
    //things.takeLastWhile { x -> x.length == 4 }.forEach{ println(it)}   // lamp,ipod

    // drop() : 앞에서부터 제외하고 반환
    //cities.drop(2).forEach{ println(it)}        // "London","Jungle","Pusan"

    // dropLast() : 뒤에서부터 제외하고 반환
    //cities.dropLast(3).forEach{ println(it)}        // "Seoul","Tokyo"



    val language = listOf("four","five","six","seven","nike","ipod")
    // dropWhile()
    // 길이가 4이하 인 것들을 앞에서 부터 빼고 반환
    //language.dropWhile { x -> x.length == 4  }.forEach{ println(it)}  /**더 공부할 것***/

    // dropLastWhile
    //language.dropLastWhile { x -> x.length == 5 }.forEach{ println(it)}  /*더 공부할 것*/



    val num = listOf("one","two","three","ten","five","six","seven")

    // first() : 컬렉션 내 첫번째 인자 반환
    //println(num.first())

    // first(조건식) : 컬렉션 내 조건에 맞는 것을 앞에서부터 반환
    //println(num.first{x -> x.length == 4})

    // firstOrNull() : 컬렉션 내 조건에 맞는 것을 앞에서부터 반환, 없으면 null 반환
    //println(num.firstOrNull(){x -> x.length > 100})


    // last() : 컬렉션 내 마지막 인자 반환
    //println(num.last())

    // last(조건식) : 컬렉션 내 조건에 맞는 것을 뒤에서부터 반환
    //println(num.last{x -> x.length == 4})

    // lastOrNull() : 컬렉션 내 조건에 맞는 것을 뒤에서부터 반환, 없으면 null
    //print(num.lastOrNull{x -> x.length > 100})


    val cities2 = listOf("Seoul","Seoul","Tokyo","Tokyo","Sydney")

    // distinct() : 중복제거
    //cities2.distinct().forEach{ println(it)}

    // distinctBy() : 조건에 맞게 중복 제거
    // 길이에 맞게 중복제거
    //cities2.distinctBy { x -> x.length }.forEach{ println(it)}


    val cityCodes = listOf("SEO","TOK","MTV","NTC")
    val cityNames = listOf("Seoul","Tokyo","Mountain View")

    // pair 형태로 출력 : zip()
    //cityCodes.zip(cityNames).forEach{ println(it)}

    // 사용자가 직접 정의 : zip() {}
    //cityCodes.zip(cityNames){code,name -> "<${code} ${name}>"}.forEach{ println(it)}


    // joinToString() : 컬렉션 내 자료를 문자열 형태로 변환함과 동시에 이를 조합하여 하나의 문자열로 생성함
    //println(cities.joinToString())
    //println(cities.joinToString(separator = " & "))

    // reduce() : 컬렉션 내 자료들을 모두 합쳐 하나의 값으로 만들어줌
    val number12 = 1..5
    val strings = listOf("a","b","c")

    //println(number12.reduce{acc,s -> acc + s})
    //println(strings.reduce{acc,s -> acc + s})

    // fold() : reduce()와 거의 동일하지만 초기값 지정할 수 있음
    //println(number12.fold(10){acc,s -> acc + s})        // 25
    //println(strings.fold("start : "){acc,s -> acc + s}) // start : abc


    // chunked() : 컬렉션을 개수 단위로 분할
    val number33 = listOf(1,2,3,4,5,6,7,8,9)
    //println(number33.chunked(3))        // [[1,2,3],[4,5,6],[7,8,9]]

    // windowed() : 컬렉션에서 위치를 1씩 증가하면서 개수만큼 분할
    val numbers44 = listOf(0,1,2,3,4,5,6,7,8,9,10)
    //println(numbers44.windowed(3))    // [[0, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7], [6, 7, 8], [7, 8, 9], [8, 9, 10]]
    //println(numbers44.windowed(3, step = 2))    // [[0, 1, 2], [2, 3, 4], [4, 5, 6], [6, 7, 8], [8, 9, 10]]


    // zipWithNext() : 인접한 두 개의 요소를 묶어서 pair 형태로 반환
    println(numbers44.zipWithNext())        // [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9), (9, 10)]

}