Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Collections 在kotlin中,flatMap与map的用例是什么_Collections_Kotlin_Flatmap - Fatal编程技术网

Collections 在kotlin中,flatMap与map的用例是什么

Collections 在kotlin中,flatMap与map的用例是什么,collections,kotlin,flatmap,Collections,Kotlin,Flatmap,在 它有使用flatMap和map 似乎两者都在做相同的事情,是否有一个示例显示使用flatMap和map的区别 数据类型: data class Shop(val name: String, val customers: List<Customer>) data class Customer(val name: String, val city: City, val orders: List<Order>) { override fun toString()

它有使用
flatMap
map

似乎两者都在做相同的事情,是否有一个示例显示使用
flatMap
map
的区别

数据类型:

data class Shop(val name: String, val customers: List<Customer>)

data class Customer(val name: String, val city: City, val orders: List<Order>) {
    override fun toString() = "$name from ${city.name}"
}

data class Order(val products: List<Product>, val isDelivered: Boolean)

data class Product(val name: String, val price: Double) {
    override fun toString() = "'$name' for $price"
}

data class City(val name: String) {
    override fun toString() = name
}
数据类商店(val名称:字符串,val客户:列表)
数据类客户(val名称:字符串,val城市:城市,val订单:列表){
重写fun-toString()=“来自${city.name}的$name”
}
数据类顺序(val产品:列表,val isDelivered:布尔值)
数据类产品(val名称:字符串,val价格:双精度){
重写fun-toString()=“$price的“$name”
}
数据类城市(val名称:字符串){
重写fun toString()=名称
}
样本:

fun Shop.getCitiesCustomersAreFrom(): Set<City> =
    customers.map { it.city }.toSet()
    // would it be same with customers.flatMap { it.city }.toSet() ?

val Customer.orderedProducts: Set<Product> get() {
    return orders.flatMap { it.products }.toSet()
    // would it be same with return orders.map { it.products }.toSet()
}
fun Shop.getCitiesCustomersRefrom():设置=
customers.map{it.city}.toSet()
//customers.flatMap{it.city}.toSet()也一样吗?
val Customer.orderedProducts:Set get(){
return orders.flatMap{it.products}.toSet()
//returnorders.map{it.products}.toSet()也一样吗
}

考虑以下示例:您有一个简单的数据结构
data
,其中有一个类型为
List
的属性

class Data(val items : List<String>)

val dataObjects = listOf(
    Data(listOf("a", "b", "c")), 
    Data(listOf("1", "2", "3"))
)
另一方面,使用
map
,只会生成一个列表列表

val items2: List<List<String>> = dataObjects
    .map { it.items } //[[a, b, c], [1, 2, 3]] 

这里有三个功能map()展平(),以及前两者的组合flatMap()

考虑下面的例子 Map允许您访问{allHeroes}中的每个宇宙并(在本例中)返回 它的英雄名单。因此,输出将是一个包含两个英雄列表的列表, 每个宇宙一个。 结果是一个列表>

平面图 FlatMap允许您执行与map相同的操作,从中访问两个英雄列表 两个宇宙。但它更进一步,将返回的列表压平 进入一个单一的列表。 结果是一个列表

压平 这将产生与flatMap相同的结果。
所以flatMap是两个函数的组合,map{},然后是flatte()

flattevs
flatMap
有什么用?
flatMap(函数)
map(函数)是一样的。flatte()
与.map{f}.flatte()相比有什么性能差异吗?
val items2: List<List<String>> = dataObjects
    .map { it.items } //[[a, b, c], [1, 2, 3]] 
val nestedCollections: List<Int> = 
    listOf(listOf(1,2,3), listOf(5,4,3))
        .flatten() //[1, 2, 3, 5, 4, 3]
data class Hero (val name:String)
data class Universe (val heroes: List<Hero>)

val batman = Hero("Bruce Wayne")
val wonderWoman = Hero (name = "Diana Prince")

val mailMan = Hero("Stan Lee")
val deadPool = Hero("Wade Winston Wilson")

val marvel = Universe(listOf(mailMan, deadPool))
val dc = Universe(listOf(batman, wonderWoman))

val allHeroes: List<Universe> = listOf(marvel, dc)
allHeroes.map { it.heroes }
// output: [[Hero(name=Stan Lee), Hero(name=Wade Winston Wilson)], [Hero(name=Bruce Wayne), Hero(name=Diana Prince)]]
allHeroes.flatMap { it.heroes } 
// output: [Hero(name=Stan Lee), Hero(name=Wade Winston Wilson), Hero(name=Bruce Wayne), Hero(name=Diana Prince)]
allHeroes.map { it.heroes }.flatten() 
// output: [Hero(name=Stan Lee), Hero(name=Wade Winston Wilson), Hero(name=Bruce Wayne), Hero(name=Diana Prince)]