map flatmap

2021-01-14  本文已影响0人  我的阿福

1.map

public inline fun <T, R> Iterable<T>.map(transform: (T) -> R): List<R> {
    return mapTo(ArrayList<R>(collectionSizeOrDefault(10)), transform)
}

public inline fun <T, R, C : MutableCollection<in R>> Iterable<T>.mapTo(destination: C, transform: (T) -> R): C {
    for (item in this)
        destination.add(transform(item))
    return destination
}

2.flatmap

public inline fun <T, R> Iterable<T>.flatMap(transform: (T) -> Iterable<R>): List<R> {
    return flatMapTo(ArrayList<R>(), transform)
}

public inline fun <T, R, C : MutableCollection<in R>> Iterable<T>.flatMapTo(destination: C, transform: (T) -> Iterable<R>): C {
    for (element in this) {
        val list = transform(element)
        destination.addAll(list)
    }
    return destination
}

测试用例

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
}

fun Shop.getCity(): List<City> {

    return customers.map { it.city }
}

fun Shop.getProduct(): List<Product> {
    return customers.flatMap { it ->
        it.orders.flatMap { it.products }
    }
}

    @Test
    fun testFor() {

        val shop = Shop(
            "红旗超市", listOf(
                Customer(
                    "张三", City("北京"), listOf(
                        Order(
                            listOf(
                                Product("芹菜", 10.0),
                                Product("白菜", 1.5)
                            ), false
                        ),
                        Order(
                            listOf(
                                Product("白酒", 1.2),
                                Product("啤酒", 0.3)
                            ), false
                        )
                    )
                ), Customer(
                    "李四", City("广州"), listOf(
                        Order(
                            listOf(
                                Product("苹果", 10.0),
                                Product("梨子", 1.5)
                            ), false
                        ),
                        Order(
                            listOf(
                                Product("咖啡", 10.0),
                                Product("可乐", 1.5)
                            ), false
                        )
                    )
                )
            )
        )

        val cityList = shop.getCity()
        for (c in cityList) {
            Log.i(TAG, "city: $c")
        }

        //
        val productList = shop.getProduct()
        for (p in productList) {
            Log.i(TAG, "product: $p")
        }
    }

运行结果


image.png
上一篇 下一篇

猜你喜欢

热点阅读