Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- doc2vec
- list map
- 독서
- AWS EC2
- Python
- map
- ChatGPT
- Zsh
- 시행착오
- FastAPI
- getChangePayload
- android custom view
- ListAdapter
- build with ai
- ktor client
- 안드로이드
- android exoplayer
- DiffUtil.ItemCallback
- ListAdapter DiffUtil
- kotlin collection
- exoplayer cache
- ExoPlayer
- kotlin list
- android ktor
- android
- 유튜브
- ktor api call
- 스피너
- llm
- video caching
Archives
- Today
- Total
버튼 수집상
[Kotlin] List.flatMap 리스트 고차함수 본문
class Tester(
val title : String,
val list: List<String>
)
class Category(
val cateNm: String,
val goodsList: List<Goods>
)
class Goods(
val goodsNm: String,
val tagList: List<String>
)
fun main() {
// 2차원 리스트
val list2d : List<Tester> = listOf(
Tester(title = "1", list = listOf("이것을", "지금부터")),
Tester(title = "2", list = listOf("하나의", "리스트로", "쭈우욱")),
Tester(title = "3", list = listOf("뽑을", "것입니다."))
)
val flatten : List<String> = list2d.flatMap {
// flatMap 블락 마지막 줄은 List<String>
it.list.map { it + " " }
}
flatten.forEach {
print(it)
}
println()
// 3차원 리스트
val cateList = listOf(
Category(
cateNm = "1",
goodsList = listOf(
Goods(
goodsNm = "goods1",
tagList = listOf("tag1", "tag2", "tag3")
),
Goods(
goodsNm = "goods2",
tagList = listOf("tag4", "tag5")
)
)
),
Category(
cateNm = "2",
goodsList = listOf(
Goods(
goodsNm = "goods3",
tagList = listOf("tag6", "tag7", "tag8", "tag9")
),
Goods(
goodsNm = "goods4",
tagList = listOf<String>()
)
)
),
Category(
cateNm = "3",
goodsList = listOf(
Goods(
goodsNm = "goods4",
tagList = listOf("tag10", "tag11")
)
)
)
)
val tagListMerged: List<String> =
cateList.flatMap{ cate ->
cate.goodsList.flatMap { goods ->
goods.tagList.map { it + " "}
}
}
tagListMerged.forEach { tags ->
print(tags)
}
}
728x90
'TIL - Kotlin' 카테고리의 다른 글
[Kotlin] RxJava의 mergeDelayError를 Coroutine Flow로 만들기 Coroutine Flow merge (0) | 2024.01.09 |
---|---|
[Kotlin] List.map 파헤치기 (0) | 2023.07.20 |
[Kotlin] 유용한 Collection 함수 (0) | 2023.07.04 |
[Kotlin] 코틀린 코드 스니펫 돌려보기 (0) | 2023.06.26 |
[Kotlin] 유용한 List 고차함수 (0) | 2023.04.18 |