somoly.tistory.com
Published 2023. 3. 26. 15:28
Kotlin Collections overview Kotlin

Collection vs. Mutable Collection

 

먼저 Kotlin의 다양한 컬렉션 유형에 대해 살펴보겠습니다. 기본 컬렉션 유형을 초기화하는 방법을 살펴보겠습니다.
컬렉션 인터페이스는 읽기 전용 메서드를 지원하는 반면 MutableCollection은 읽기/쓰기 메서드를 지원합니다.

 

 

List

listOf() 메서드를 사용하여 간단한 읽기 전용 List를 생성하고 mutableListOf() 메서드를 사용하여 읽기-쓰기 MutableList를 생성할 수 있습니다:

val theList = listOf("one", "two", "three")    

val theMutableList = mutableListOf("one", "two", "three")

 

 

Set

마찬가지로 setOf() 메서드를 사용하여 읽기 전용 Set을 만들고 mutableSetOf() 메서드를 사용하여 읽기-쓰기 MutableSet을 만들 수 있습니다:

val theSet = setOf("one", "two", "three")  

val theMutableSet = mutableSetOf("one", "two", "three")

 

Map

메서드 mapOf()를 사용하여 읽기 전용 맵을 생성하고 mutableMapOf()를 사용하여 읽기-쓰기 MutableMap을 생성할 수도 있습니다:

val theMap = mapOf(1 to "one", 2 to "two", 3 to "three")

val theMutableMap = mutableMapOf(1 to "one", 2 to "two", 3 to "three")

 

유용한 연산자

 

Kotlin의 컬렉션 API는 Java에서 찾을 수 있는 것보다 훨씬 더 풍부하며, 오버로드된 연산자 집합이 함께 제공됩니다.

 

`in` 연산자

collection.contains(x)로 변환할 수 있는 `x in collection`이라는 표현식을 사용할 수 있습니다:

@Test
fun whenSearchForExistingItem_thenFound () {
    val theList = listOf("one", "two", "three")

    assertTrue("two" in theList)        
}

 

 

`+` 연산자

`+` 연산자를 사용하여 요소 또는 전체 컬렉션을 다른 컬렉션으로 이동할 수 있습니다:

@Test
fun whenJoinTwoCollections_thenSuccess () {
    val firstList = listOf("one", "two", "three")
    val secondList = listOf("four", "five", "six")
    val resultList = firstList + secondList

    assertEquals(6, resultList.size)   
    assertTrue(resultList.contains("two"))        
    assertTrue(resultList.contains("five"))        
}

 

`-` 연산자

마찬가지로 `-` 연산자를 사용하여 한 요소 또는 여러 요소를 제거할 수 있습니다:

@Test
fun whenExcludeItems_thenRemoved () {
    val firstList = listOf("one", "two", "three")
    val secondList = listOf("one", "three")
    val resultList = firstList - secondList

    assertEquals(1, resultList.size)        
    assertTrue(resultList.contains("two"))        
}

 

기타 방법

마지막으로 몇 가지 일반적인 수집 방법을 살펴보겠습니다. Java에서 고급 메서드를 활용하려면 Stream API를 사용해야 합니다.

Kotlin에서는 컬렉션 API에서 유사한 메서드를 사용할 수 있습니다.

 

 

Slicing

주어진 목록에서 하위 목록을 얻을 수 있습니다:

@Test
fun whenSliceCollection_thenSuccess () {
    val theList = listOf("one", "two", "three")
    val resultList = theList.slice(1..2)

    assertEquals(2, resultList.size)        
    assertTrue(resultList.contains("two"))        
}

 

Removing

리스트에서 모든 널을 쉽게 제거할 수 있습니다:

@Test
fun whenFilterNullValues_thenSuccess () {
    val theList = listOf("one", null, "two", null, "three")
    val resultList = theList.filterNotNull()

    assertEquals(3, resultList.size)        
}

 

Filtering

Java Stream API의 filter() 메서드와 유사하게 작동하는 filter()를 사용하여 컬렉션 항목을 쉽게 필터링할 수 있습니다:

@Test
fun whenFilterNonPositiveValues_thenSuccess () {
    val theList = listOf(1, 2, -3, -4, 5, -6)
    val resultList = theList.filter{ it > 0}

    assertEquals(3, resultList.size)  
    assertTrue(resultList.contains(1))
    assertFalse(resultList.contains(-4))      
}

 

Dropping

처음 N개의 아이템을 드롭할 수 있습니다:

@Test
fun whenDropFirstItems_thenRemoved () {
    val theList = listOf("one", "two", "three", "four")
    val resultList = theList.drop(2)

    assertEquals(2, resultList.size)        
    assertFalse(resultList.contains("one"))        
    assertFalse(resultList.contains("two"))        
}

 

주어진 조건을 충족하는 경우 처음 몇 개의 아이템을 드롭할 수 있습니다:

@Test
fun whenDropFirstItemsBasedOnCondition_thenRemoved () {
    val theList = listOf("one", "two", "three", "four")
    val resultList = theList.dropWhile{ it.length < 4 }

    assertEquals(2, resultList.size)        
    assertFalse(resultList.contains("one"))        
    assertFalse(resultList.contains("two"))        
}

 

Grouping

요소를 그룹화할 수 있습니다:

@Test
fun whenGroupItems_thenSuccess () {
    val theList = listOf(1, 2, 3, 4, 5, 6)
    val resultMap = theList.groupBy{ it % 3}

    assertEquals(3, resultMap.size)  
    
    assertTrue(resultMap[1]!!.contains(1))
    assertTrue(resultMap[2]!!.contains(5))      
}

 

Mapping

제공된 함수를 사용하여 모든 요소를 매핑할 수 있습니다:

@Test
fun whenApplyFunctionToAllItems_thenSuccess () {
    val theList = listOf(1, 2, 3, 4, 5, 6)
    val resultList = theList.map{ it * it }
    
    assertEquals(4, resultList[1])
    assertEquals(9, resultList[2])
}

 

flatMap()을 사용해 중첩된 컬렉션을 평평하게 만들 수 있습니다. 여기서는 문자열을 List<String>으로 변환하고 List<List<String>>로 끝나지 않도록 합니다:

@Test
fun whenApplyMultiOutputFunctionToAllItems_thenSuccess () {
    val theList = listOf("John", "Tom")
    val resultList = theList.flatMap{ it.toLowerCase().toList() }
    
    assertEquals(7, resultList.size)
}

 

Reduction

접기/축소 작업을 수행할 수 있습니다:

@Test
fun whenApplyFunctionToAllItemsWithStartingValue_thenSuccess () {
    val theList = listOf(1, 2, 3, 4, 5, 6)
    val finalResult = theList.fold(0, {acc, i -> acc + (i * i)})
    
    assertEquals(91, finalResult)
}

 

Chunking

컬렉션을 지정된 크기의 청크로 나누려면 chunked() 메서드를 사용할 수 있습니다:

 

@Test
fun whenApplyingChunked_thenShouldBreakTheCollection() {
    val theList = listOf(1, 2, 3, 4, 5)
    val chunked = theList.chunked(2)

    assertThat(chunked.size).isEqualTo(3)
    assertThat(chunked.first()).contains(1, 2)
    assertThat(chunked[1]).contains(3, 4)
    assertThat(chunked.last()).contains(5)
}

 

컬렉션에는 5개의 요소가 있으므로 chunked(2) 메서드 호출은 각각 2개의 요소가 있는 두 개의 컬렉션과 하나의 단일 요소 컬렉션을 반환합니다.

컬렉션을 분할한 후 각 청크를 다른 컬렉션에 매핑할 수도 있습니다:

@Test
fun whenApplyingChunkedWithTransformation_thenShouldBreakTheCollection() {
    val theList = listOf(1, 2, 3, 4, 5)
    val chunked = theList.chunked(3) { it.joinToString(", ") }

    assertThat(chunked.size).isEqualTo(2)
    assertThat(chunked.first()).isEqualTo("1, 2, 3")
    assertThat(chunked.last()).isEqualTo("4, 5")
}

크기 3의 청크를 생성한 후 각 청크를 쉼표로 구분된 문자열로 변환합니다.

 

 

'Kotlin' 카테고리의 다른 글

Kotlin Coroutine 개념  (0) 2023.10.05
kotlin euc-kr to utf-8 conversion  (0) 2023.09.09
Apache POI sheet.autoSizeColumn() Speed Up  (0) 2023.08.08
kotlin logger  (0) 2023.06.26
profile

somoly.tistory.com

@RxCats

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!