본문 바로가기

전체 글235

[Kotlin][Bitwise Operator] shl, shr, ushr, and, or, xor, inv 이 값을 bitCount 비트 수만큼 왼쪽으로 이동합니다. bitCount 의 최하위 5개 비트만 이동 거리로 사용됩니다. 따라서 실제로 사용되는 변속 거리는 항상 0..31 범위에 있습니다.print(1 shl 3) // 8 이 값을 bitCount 비트 수만큼 오른쪽으로 이동하여 가장 왼쪽 비트를 부호 비트의 복사본으로 채웁니다. bitCount 의 최하위 5개 비트만 이동 거리로 사용됩니다. 따라서 실제로 사용되는 변속 거리는 항상 0..31 범위에 있습니다.print(9 shr 3) // 1 이 값을 bitCount 비트 수만큼 오른쪽으로 이동하여 가장 왼쪽 비트를 0으로 채웁니다. bitCount 의 최하위 5개 비트만 이동 거리로 사용됩니다. 따라서 실제로 사용되는 변속 거리는 항상 0..31.. 2024. 9. 7.
[Kotlin][Set] union, subtract, intersect (합집합, 차집합, 교집합) 두 컬렉션의 모든 개별 요소를 포함하는 세트를 반환합니다.public infix fun Iterable.union(other: Iterable): Set { val set = this.toMutableSet() set.addAll(other) return set}val set1 = setOf(1, 2, 3, 4)val set2 = setOf(2, 3, 4, 5)print(set1.union(set2)) // [1, 2, 3, 4, 5] 이 컬렉션에 포함되어 있고 지정된 컬렉션에 포함되지 않은 모든 요소를 포함하는 집합을 반환합니다.public infix fun Iterable.subtract(other: Iterable): Set { val set = this.toMutableS.. 2024. 9. 7.
[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.