Kotlin:lambda运行替代方案
我有userDto,包含程序,其中包含实际字段。实际程序只能是一个。我需要得到它。然后,我运行以下命令:Kotlin:lambda运行替代方案,kotlin,lambda,Kotlin,Lambda,我有userDto,包含程序,其中包含实际字段。实际程序只能是一个。我需要得到它。然后,我运行以下命令: userDto.programs.sortedBy { it.created }.findLast { it.actual }? 好的,但我想预测一下这种情况,当findLast返回null时,抛出异常(&T)。请,建议,怎么做 UPD: 你真的很接近:!你能做的是: userDto.programs.sortedBy { it.created }.findLast { it.actual
userDto.programs.sortedBy { it.created }.findLast { it.actual }?
好的,但我想预测一下这种情况,当findLast返回null时,抛出异常(&T)。请,建议,怎么做
UPD:
你真的很接近:!你能做的是:
userDto.programs.sortedBy { it.created }.findLast { it.actual } ?: throw RuntimeException()
或者,如果您试图实际避免抛出错误,但无法通过问题的提问方式真正判断,您可以执行如下错误检查:
userDto.programs.sortedBy { it.created }.findLast { it.actual }?.let{
//rest of your code goes here
}
希望这有帮助,干杯 但若我想在检查后继续处理表达式结果,比如在升级的示例中?
userDto.programs.sortedBy { it.created }.findLast { it.actual }?.let{
//rest of your code goes here
}