关于集合操作中异常:Collection contains no element matching the predicate.

982 阅读1分钟

出错的代码是这样的:

val temp = it.discountList.first { it1 -> it1.discountType == 1 }

一般集合中去匹配按照一定条件进行筛选 比如last first 这些,都需要判空,如果不对null进行处理就会报这样的异常。解决方案就是:

val temp = it.discountList.firstOrNull { it1 -> it1.discountType == 1 }

真坑爹啊