Kotlin-Koans 学习笔记-集合操作

kotlin 扩展了很多的 to… 方法


映射函数: map{(T) -> R}
根据传入的转换函数将原有的集合转换成一个新的 List 集合

1
2
3
4
5
6
fun Shop.getCitiesCustomersAreFrom(): Set<City> {
// Return the set of cities the customers are from
return this.customers.map {
it.city
}.toSet()
}

过滤函数: filter{(T) -> Boolean}
根据传入的判断函数返回一个新的 List 集合
1
2
3
4
5
6
fun Shop.getCustomersFrom(city: City): List<Customer> {
// Return a list of the customers who live in the given city
return this.customers.filter {
it.city == city
}
}

全局判断函数:all {(T)->Boolean} 返回 boolean 全部满足才返回 true
等同于list.size == list.filter{(T)->Boolean}.size

包含判断函数:any {(T)->Boolean} 返回 boolean 只要满足lambda即返回 true
等同于:list.fliter{(T)->Boolean}.size>0

判断计数函数 count{(T)->Boolean} 返回满足条件的子集数量
等同于list.fliter{(T)->Boolean}.size

判断首项函数:firstOrNull{(T)->Boolean} 返回子集中第一个满足条件的项( T 或者 null)
等同于
1
2
var r = list.filter{}
return if (r.size>0) r.get(0) else null

铺平函数:flatMap

如果我们的传入参数是一个用户,但是我们最终想要的结果是用户全部订单(List)中的全部商品的集合,这时我们就需要flatMap 铺平。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
val Customer.orderedProducts: Set<Product> get() {
// Return all products this customer has ordered
//返回用户全部订单中所有的商品种类
return orders.flatMap {
it.products
}.toSet()
}

val Shop.allOrderedProducts: Set<Product> get() {
// Return all products that were ordered by at least one customer
//返回商店中全部的至少被一位用户下单过的商品
customers.flatMap {
it.orders
}.flatMap {
it.products
}.toSet()

return customers.flatMap {
it.orderedProducts
}.toSet()
}

取最大值函数:max()maxBy{} 根据传入的选择器找出集合中最大的成员
1
2
3
4
5
6
7
8
fun Customer.getMostExpensiveOrderedProduct(): Product? {
// Return the most expensive product which has been ordered
return orders.flatMap {
it.products //订单铺平成商品
}.maxBy {
it.price //取出商品中价格最大的
}
}

排序函数:sortedBy{} 根据传入的选择器升序排列
1
2
3
4
5
6
fun Shop.getCustomersSortedByNumberOfOrders(): List<Customer> {
// Return a list of customers, sorted by the ascending number of orders they made
return this.customers.sortedBy {
it.orders.size //按照客户的订单数升序排列
}
}

求和函数:sum()返回Int、 sumBy{}返回 Int、 sumByDouble{} 返回 Double
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
fun Customer.getTotalOrderPrice(): Double {
// Return the sum of prices of all products that a customer has ordered.
// Note: a customer may order the same product for several times.
orders.flatMap {
it.products
}.sumByDouble {
it.price
}

orders.flatMap { it.products }.fold(0.0,{acc, product ->
acc+product.price
})
var result = 0.0
orders.flatMap { it.products }.forEach {
result+=it.price
}
return result
}

分组函数 groupBy{} 按照传入的选择器将集合对象分组返回Map<K, List<T>>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
fun Shop.groupCustomersByCity(): Map<City, List<Customer>> {
// fun Shop.groupCustomersByCity(): Map<City, List<Customer>> {
// Return a map of the customers living in each city
//不使用groupBy函数的写法
val map = mutableMapOf<City,List<Customer>>()
val citys = this.customers.map {
it.city
}.toSet().map {
map.put(it, this.getCustomersFrom(it))
}
// return map //效果等同

return this.customers.groupBy { it.city }
}

折叠函数:Iterable<T>.fold{initial:R, operation: (R,T) -> R}
折叠函数

将一个 T集合装换成一个 R ,所以这个方法被称为折叠。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
fun Shop.getSetOfProductsOrderedByEachCustomer(): Set<Product> {
// Return the set of products that were ordered by each of the customers
//找到商店中每一个用户都下单的商品
allOrderedProducts.filter { p ->
customers.all {
it.orders.flatMap {
it.products
}.any {
it == p
}
}
}.toSet()

return customers.fold(allOrderedProducts, { orderedByAll, customer ->
//第一次调用该函数时传入值为fold函数的第一个参数 全部被下单的产品集合
orderedByAll.intersect( //交集运算
customer.orders.flatMap {
it.products
}.toSet() //传入的用户的全部产品的集合
)
})
}