val numberList = listOf(1, 2, 3, 4) var ss = numberList.reduce { acc, i -> acc + i } println(ss)
//最后打印的值是:10
如果要计算一个对象里面的Int值,如果一个账单的account
账单的实体类
data class PaymentOrderEntity(var orderId: String, var account: Int)
var paymentList = mutableListOf<PaymentOrderEntity>() paymentList.add(PaymentOrderEntity("13543141241", 4)) paymentList.add(PaymentOrderEntity("32141341344", 5)) var ss = paymentList.map(PaymentOrderEntity::account).reduce { acc, i -> acc + i } println(ss)
//最后输出的值是:9
示例:
val numberList = listOf(1, 2, 3, 4) var ss = numberList.fold(1) { acc, i -> acc + i } println(ss)
//最后打印的值是:11