我有,我想过滤列表,然后返回地图ID与量的总和:科特林:在列表中BigDecimal的总和
val totalById = list
.filter { it.status == StatusEnum.Active }
.groupBy { it.item.id }
.mapValues { it.value.sumBy { it.amount } }
“it.amount”是BigDecimal的,但看起来像sumBy仅限于Int。
对于Java 8这将会是:
Collectors.groupingBy(i-> i.getItem().getId(), Collectors.mapping(Item::getAmount, Collectors.reducing(BigDecimal.ZERO, BigDecimal::add))))
有没有办法做到在科特林?
我尝试: .mapValues {it.value.map {it.amount} .fold(BigDecimal.ZERO,BigDecimal的::添加) 但complierr抱怨 “::增加”: 无下列功能可以是使用提供的参数调用: – klc
[错误]私人打开乐趣添加(p0:BigInteger!,p1:Int,p2:BigInteger!,p3:Int):BigDecimal!定义在java.math.BigDecimal中 [错误] private open fun add(p0:Long,p1:Int,p2:BigInteger!,p3:Int):BigDecimal!定义在java.math.BigDecimal中 [错误] private open fun add(p0:Long,p1:Int,p2:Long,p3:Int):BigDecimal!定义在java.math.BigDecimal中 [错误] private open fun add(p0:Long,p1:Long):在java.math.BigDecimal中定义的长定义 [ p2:Int):BigDecimal!在java.math.BigDecimal的 – klc
@klc定义,我制作一个完整的例子,它的工作原理:http://try.kotlinlang.org/#/UserProjects/cpohvnjtfr0uvpvcjc3gt02b1t/n0j3v72oh55hdf141663os6eek,你可以自己有尝试。 – Ilya