본문 바로가기

코틀린74

[Kotlin][Collection] groupingBy with eachCount 각 요소에서 키를 추출하기 위해 지정된 keySelector 함수를 사용하여 나중에 그룹화 및 접기 작업 중 하나와 함께 사용할 컬렉션에서 그룹화 소스를 만듭니다.eachCount : 각 그룹의 키를 그룹의 요소 수와 연결하는 맵을 반환합니다.public inline fun Iterable.groupingBy(crossinline keySelector: (T) -> K): Grouping { return object : Grouping { override fun sourceIterator(): Iterator = this@groupingBy.iterator() override fun keyOf(element: T): K = keySelector(element) }}v.. 2024. 9. 7.
[Kotlin][Integer] toBinaryString 정수 인수의 문자열 표현을 기수 2의 부호 없는 정수로 반환합니다.public static String toBinaryString(int i) { return toUnsignedString0(i, 1);}private static String toUnsignedString0(int val, int shift) { // assert shift > 0 && shift  Integer (Java Platform SE 8 )Returns the value obtained by rotating the two's complement binary representation of the specified int value left by the specified number of bits. (Bits shif.. 2024. 9. 7.
[Kotlin][Map] getOrDefault 지정된 키가 매핑된 값을 반환하거나, 이 맵에 키에 대한 매핑이 포함되어 있지 않은 경우 defaultValue를 반환합니다.public inline fun Map.getOrDefault(key: K, defaultValue: V): V = (this as Map).getOrDefault(key, defaultValue) val list = listOf(1, 2, 3, 4)val map = mutableMapOf()list.forEach { map[it] = map.getOrDefault(it, 0) + 1 }print(map) // {1=1, 2=1, 3=1, 4=1} getOrDefault - Kotlin Programming Language kotlinlang.org 2024. 9. 7.
[Kotlin][Array] copyOfRange 원래 배열의 지정된 범위의 복사본인 새 배열을 반환합니다.public actual inline fun Array.copyOfRange(fromIndex: Int, toIndex: Int): Array { return if (kotlin.internal.apiVersionIsAtLeast(1, 3, 0)) { copyOfRangeImpl(fromIndex, toIndex) } else { if (toIndex > size) throw IndexOutOfBoundsException("toIndex: $toIndex, size: $size") java.util.Arrays.copyOfRange(this, fromIndex, toIndex) }}val ar.. 2024. 9. 6.
[Kotlin][Array] sliceArray 지정된 인덱스 범위의 인덱스에 있는 요소를 포함하는 배열을 반환합니다.public fun Array.sliceArray(indices: IntRange): Array { if (indices.isEmpty()) return copyOfRange(0, 0) return copyOfRange(indices.start, indices.endInclusive + 1)}public actual inline fun Array.copyOfRange(fromIndex: Int, toIndex: Int): Array { return if (kotlin.internal.apiVersionIsAtLeast(1, 3, 0)) { copyOfRangeImpl(fromIndex, toIndex) .. 2024. 9. 6.
[Kotlin][Collection] removeFirst / removeLast / removeAt 이 변경 가능한 목록에서 첫 번째 요소를 제거하고 제거된 요소를 반환하거나, 이 목록이 비어 있으면 NoSuchElementException을 발생시킵니다.public fun MutableList.removeFirst(): T = if (isEmpty()) throw NoSuchElementException("List is empty.") else removeAt(0)val list = mutableListOf(1, 2, 3, 4)print(list.removeFirst()) // 1print(list) // [2, 3, 4] 이 변경 가능한 목록에서 마지막 요소를 제거하고 제거된 요소를 반환하거나, 이 목록이 비어 있으면 NoSuchElementException을 발생시킵니다.public fun M.. 2024. 9. 6.
[Kotlin][Collection] sortedWith 지정된 비교기에 따라 정렬된 모든 요소의 목록을 반환합니다. 정렬이 안정적입니다. 이는 동일한 요소가 정렬 후에도 서로 상대적인 순서를 유지함을 의미합니다.public fun Iterable.sortedWith(comparator: Comparator): List { if (this is Collection) { if (size () as Array).apply { sortWith(comparator) }.asList() } return toMutableList().apply { sortWith(comparator) }}public actual fun MutableList.sortWith(comparator: Comparator): Unit { if (size > 1).. 2024. 9. 5.