볼 때 마다 헷갈리는 map과 flatMap을 파헤쳐 보겠다. // map 예제 val data = listOf("A", "B", "C") val newData = data.map { "$it*" } println("data: $newData") // flatMap 예제 val data2 = listOf("A", "B", "C") val newData2 = data.flatMap { listOf("$it*") } println("data2: $newData2") ---출력 결과--- data: [A*, B*, C*] data2: [A*, B*, C*] Collections 클래스에서 map과 flatMap 함수를 살펴보자 1. map 함수 /** * Returns a list containing the ..
inline 함수와 generic type에서 reified 를 사용하는 걸 종종 볼 수 있다. inline fun Fragment.viewModel(factory: ViewModelProvider.Factory, body: T.() -> Unit): T { val vm = ViewModelProviders.of(this, factory)[T::class.java] vm.body() return vm } 먼저 reified 의 단어의 뜻은 '구체화 된' 이다. (reify: 구체화 하다. 생각하다.) ex) reify a concept. (개념을 구체화하다.) (발음은 특이하게 '리어파이' 이다. 라이파이 아님 주의! ) 어원 그대로 일반 Generic Type과 다르게 더 구체화를 한다고 보면 된다. ..
Kotlin List 종류들과 Map 등의 Iterable 객체들은 filter, map 등의 함수를 사용합니다. 1. 일반 사용법 val strList = "한 두울 세에엣 네에에에엣 다서어어어엇".split(" ") val lengthList = strList .filter { println("filter: $it"); it.length > 2 } .map { println("map: ${it.length}"); it.length } .take(2) println(lengthList.toList()) // 출력 결과 filter: 한 filter: 두울 filter: 세에엣 filter: 네에에에엣 filter: 다서어어어엇 map: 3 map: 5 map: 6 [3, 5] 2. Sequence 사용..
간단하게 추상 메소드 한개를 가진 인터페이스를 뜻한다. - Single Abstract Method - lamda 식을 제공 - interface/ abstract 이어야 하고, 1개의 abstract method를 가지고 있어야 함 - setter 가 정의되어 있어야 함 주의사항 JAVA에서 만들어진 interface/ abstract 클래스는 kotlin에서 람다식으로 사용할 수 없다. interface MyOnClickListener { void onClick() } public void setOnMyOnClickListener (MyOnClickListener listener) { this.onClickListener = listener; } tv.setOnMyOnClickListener { } ..
1. 클래스에서의 사용 interface Generic { fun setItem(item: T) } class Sample : Generic { override fun setItem(item: Generics type 정의) { // 구현 } } 2. 함수에서의 사용 private fun abc(t: T) { } // Use abc("ABC") abc("ABC") Type 생략 1. 생략 가능한 경우 class Foo(val t: T) @Test fun test() { val foo1 = Foo("foo1") // 생략 가능 val foo2 = Foo("foo2") // 선언 가능 } 2. 생략이 불가능한 경우 class Foo @Test fun test() { val foo1 = Foo() // 선언 ..
1. Nested class Nested: 내포된 이라는 뜻으로, Class 안에 있는 일반 Class 가 있는 경우를 말한다. class Outer { class Nested { val a = 1 } } java 로 Decompile해보면 아래와 같다. static 처리가 되어 있어서 아래와 같이 Nested 클래스를 호출할 수 있다. Outer.Nested() ※ 주의 사항 Kotlin에서 Nested Class는 내부적으로 Static 처리를 한다는 것이다. Java에서 Nested Class를 선언하면 Static 이 아니란 걸 주의하면 된다. (안드 개발시 Kotlin과 Java를 왔다갔다 하면서 사용할 일이 많아서 둘다 알아야 한다...) 2. Inner class Java에서 Nested C..
1. Higer-Order Functions 함수를 파라메터로 전달하거나, 함수를 리턴 가능 lamda를 통해 축약 형태로 제공 - 파라메터 정의 (3가지) 1) 파라메터 없는 경우 fun print(body: () -> String) { println(body()) } @Test fun test() { print({"test"}) } 아래 함수는 위와 같다. fun print() { println(body()) } @Test fun test() { print() } fun body() = "return message" 2) 파라메터 1개인 경우 fun print(body: (String) -> String) { println(body("print message")) } @Test fun test() {..
1. Nested class 내부에 그냥 class 를 정의하면 static으로 처리한다. java에서는 기본값이 inner class 이지만, kotlin은 기본값이 nested 즉 static class 이다. class Outer { private val bar: Int = 1 class Nested { fun foo() = bar } } /* 호출 */ val demo = Outer.Nested().foo() // == 2 2. Inner class class Outer { private val bar: Int = 1 inner class Inner { fun foo() = bar } } /* 호출 */ val demo = Outer().Inner().foo() // == 1 3. Anonymou..