본문 바로가기

Kotlin147

[Kotlin][Map] containsKey, containsValue 맵에 지정된 키가 포함되어 있으면 true 를 반환합니다. K 유형의 키를 전달해야 하는 containKey 의 유형 안전성 제한을 극복할 수 있습니다.val map: Map = mapOf("x" to 1, "y" to 2)print(map.containsKey("x")) // trueprint(map.containsKey("y")) // trueprint(map.containsKey("z")) // false 맵이 하나 이상의 키를 지정된 값에 매핑하는 경우 true 를 반환합니다. V 유형의 값을 전달해야 하는 containValue 의 유형 안전성 제한을 극복할 수 있습니다.val map: Map = mapOf("x" to 1, "y" to 2)// member containsValue is use.. 2024. 9. 10.
[Kotlin][String] indexOf 지정된 startIndex 부터 시작하여 지정된 문자열이 처음 나타나는 이 문자 시퀀스 내의 인덱스를 반환합니다.public fun CharSequence.indexOf(string: String, startIndex: Int = 0, ignoreCase: Boolean = false): Int { return if (ignoreCase || this !is String) indexOf(string, startIndex, length, ignoreCase) else nativeIndexOf(string, startIndex)}fun matchDetails(inputString: String, whatToFind: String, startIndex: Int = 0): S.. 2024. 9. 10.
[Kotlin][Collection] getOrPut 값이 존재하고 null이 아닌 경우 지정된 키에 대한 값을 반환합니다. 그렇지 않으면 defaultValue 함수를 호출하고 해당 결과를 지정된 키 아래의 맵에 넣고 호출 결과를 반환합니다. 맵이 동시에 수정되는 경우 작업이 원자적으로 보장되지 않는다는 점에 유의하세요.public inline fun MutableMap.getOrPut(key: K, defaultValue: () -> V): V { val value = get(key) return if (value == null) { val answer = defaultValue() put(key, answer) answer } else { value }}val map = muta.. 2024. 9. 7.
[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.