Last active
June 30, 2023 07:35
-
-
Save senamit2708/7029963c913137ecaa2d0611ec773a71 to your computer and use it in GitHub Desktop.
map, flatmap basic learning
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package collections | |
| fun main() { | |
| mapfunOne() | |
| shoppingFun() | |
| shoppingFunComplex() | |
| } | |
| fun mapfunOne() { | |
| class OrderLine(val name: String, val price: Int) | |
| val orderLineList = listOf(OrderLine("samsung mobile", 2000), OrderLine("realme", 15000), OrderLine("oppo", 25000)) | |
| val companylist = orderLineList.map { it.name } //[samsung mobile, realme, oppo] | |
| println(companylist) | |
| } | |
| fun shoppingFun() { | |
| class ShoppingBag(val items: List<String>) | |
| val groceryBag: List<ShoppingBag> = listOf( | |
| ShoppingBag(listOf("grapes", "banana", "sugarcane")), | |
| ShoppingBag(listOf("milk", "egg", "pasta")), | |
| ShoppingBag(listOf("bread", "naan", "masala tikka")) | |
| ) | |
| val retailBag: List<ShoppingBag> = listOf( | |
| ShoppingBag(listOf("shirt", "tshirt", "trouser")), | |
| ShoppingBag(listOf("skirt", "shorts", "suit")), | |
| ShoppingBag(listOf("umbrella", "pen", "pencil")) | |
| ) | |
| val groceries = groceryBag.map { it.items } | |
| val retailItems = retailBag.flatMap { it.items } | |
| println("groceries: $groceries") //groceries: [[grapes, banana, sugarcane], [milk, egg, pasta], [bread, naan, masala tikka]] | |
| println("retailItems: $retailItems ") //retailItems: [shirt, tshirt, trouser, skirt, shorts, suit, umbrella, pen, pencil] | |
| class WorkingDay(val items: List<Int>) | |
| val workindDayList: List<WorkingDay> = listOf( | |
| WorkingDay(listOf(1, 2, 3, 4)), | |
| WorkingDay(listOf(7, 8)) | |
| ) | |
| val groceryAndRetailTogether = listOf(groceryBag, retailBag) | |
| val groceryAndRetailOne = groceryAndRetailTogether.flatMap { it.flatMap { it.items } } | |
| println("groceryAndRetail: $groceryAndRetailOne") | |
| //groceryAndRetail: [grapes, banana, sugarcane, milk, egg, pasta, bread, naan, masala tikka, shirt, tshirt, trouser, skirt, shorts, suit, umbrella, pen, pencil] | |
| val groceryAndRetailTwo = groceryAndRetailTogether.map { it.map { it.items } } | |
| println("groceryAndRetailTwo: $groceryAndRetailTwo") | |
| //groceryAndRetailTwo: [[[grapes, banana, sugarcane], [milk, egg, pasta], [bread, naan, masala tikka]], [[shirt, tshirt, trouser], [skirt, shorts, suit], [umbrella, pen, pencil]]] | |
| val groceryAndRetailThree = groceryAndRetailTogether.flatMap { it.map { it.items } } | |
| println("groceryAndRetailThree: $groceryAndRetailThree") | |
| //groceryAndRetailThree: [[grapes, banana, sugarcane], [milk, egg, pasta], [bread, naan, masala tikka], [shirt, tshirt, trouser], [skirt, shorts, suit], [umbrella, pen, pencil]] | |
| val groceryAndRetailFour = groceryAndRetailTogether.map { it.flatMap { it.items } } | |
| println("groceryAndRetailFour: $groceryAndRetailFour") | |
| //groceryAndRetailFour: [[grapes, banana, sugarcane, milk, egg, pasta, bread, naan, masala tikka], [shirt, tshirt, trouser, skirt, shorts, suit, umbrella, pen, pencil]] | |
| val retailItemAndDay = | |
| listOf(retailItems, workindDayList) //flatmap and map will not work if the list of two different classes. | |
| // val retailItemAndDayListOne = retailItemAndDay.flatMap { it.map { it.items } } //compile time error | |
| } | |
| fun shoppingFunComplex() { | |
| class CartItem(val name: String, val price: Int) | |
| class ShoppingBag(val items: List<CartItem>) | |
| val groceryBag: List<ShoppingBag> = listOf( | |
| ShoppingBag(listOf(CartItem("grapes", 20), CartItem("banana", 30), CartItem("sugarCane", 15))), | |
| ShoppingBag(listOf(CartItem("milk", 40), CartItem("egg", 15), CartItem("pasta", 80))), | |
| ShoppingBag(listOf(CartItem("bread", 16), CartItem("naan", 50), CartItem("masala-tikka", 70))) | |
| ) | |
| val retailBag: List<ShoppingBag> = listOf( | |
| ShoppingBag(listOf(CartItem("shirt", 200), CartItem("tshirt", 300), CartItem("trouser", 150))), | |
| ShoppingBag(listOf(CartItem("skirt", 400), CartItem("shorts", 150), CartItem("suit", 800))), | |
| ShoppingBag(listOf(CartItem("umbrella", 160), CartItem("pen", 500), CartItem("pencil", 700))) | |
| ) | |
| val groceries = groceryBag.map { it.items.map { it.name } } | |
| val retailItems = retailBag.flatMap { it.items.map { it.name } } | |
| // val groceriesOne = groceryBag.map { it.items.flatMap { it.name } } //compile time error -> flatmap require itterable to work on, not on single string. -> it means flatmap need list to work on | |
| println("groceries: $groceries") | |
| //groceries: [[grapes, banana, sugarCane], [milk, egg, pasta], [bread, naan, masala-tikka]] | |
| println("retailItems: $retailItems ") | |
| //retailItems: [shirt, tshirt, trouser, skirt, shorts, suit, umbrella, pen, pencil] | |
| val groceryAndRetailTogether = listOf(groceryBag, retailBag) | |
| val groceryAndRetailOne = groceryAndRetailTogether.flatMap { it.flatMap { it.items.map { it.name.uppercase() } } } | |
| println("groceryAndRetail: $groceryAndRetailOne") | |
| //groceryAndRetail: [GRAPES, BANANA, SUGARCANE, MILK, EGG, PASTA, BREAD, NAAN, MASALA-TIKKA, SHIRT, TSHIRT, TROUSER, SKIRT, SHORTS, SUIT, UMBRELLA, PEN, PENCIL] | |
| val groceryAndRetailTwo = groceryAndRetailTogether.map { it.map { it.items.map { it.name.uppercase() } } } | |
| println("groceryAndRetailTwo: $groceryAndRetailTwo") | |
| //groceryAndRetailTwo: [[[GRAPES, BANANA, SUGARCANE], [MILK, EGG, PASTA], [BREAD, NAAN, MASALA-TIKKA]], [[SHIRT, TSHIRT, TROUSER], [SKIRT, SHORTS, SUIT], [UMBRELLA, PEN, PENCIL]]] | |
| val groceryAndRetailThree = groceryAndRetailTogether.flatMap { it.map { it.items.map { it.name.uppercase() } } } | |
| println("groceryAndRetailThree: $groceryAndRetailThree") | |
| //groceryAndRetailThree: [[GRAPES, BANANA, SUGARCANE], [MILK, EGG, PASTA], [BREAD, NAAN, MASALA-TIKKA], [SHIRT, TSHIRT, TROUSER], [SKIRT, SHORTS, SUIT], [UMBRELLA, PEN, PENCIL]] | |
| val groceryAndRetailFour = groceryAndRetailTogether.map { it.flatMap { it.items.map { it.name.uppercase() } } } | |
| println("groceryAndRetailFour: $groceryAndRetailFour") | |
| //groceryAndRetailFour: [[GRAPES, BANANA, SUGARCANE, MILK, EGG, PASTA, BREAD, NAAN, MASALA-TIKKA], [SHIRT, TSHIRT, TROUSER, SKIRT, SHORTS, SUIT, UMBRELLA, PEN, PENCIL]] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Returns a list containing the results of applying the given [transform] function | |
| * to each element in the original collection. | |
| * | |
| * @sample samples.collections.Collections.Transformations.map | |
| */ | |
| public inline fun <T, R> Iterable<T>.map(transform: (T) -> R): List<R> { | |
| return mapTo(ArrayList<R>(collectionSizeOrDefault(10)), transform) | |
| } | |
| /** | |
| * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original collection. | |
| * | |
| * @sample samples.collections.Collections.Transformations.flatMap | |
| */ | |
| public inline fun <T, R> Iterable<T>.flatMap(transform: (T) -> Iterable<R>): List<R> { | |
| return flatMapTo(ArrayList<R>(), transform) | |
| } | |
| //note -> flatmap always work on list of items, thats why in code i have written compile time error when i tried to use flatmap on single item of any list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment