scala - Scala 检查 ListBuffer[MyType] 是否包含一个元素
问题描述
我有两个班级:
class Item(val description: String, val id: String)
class ItemList {
private var items : ListBuffer[Item] = ListBuffer()
}
如何检查项目是否包含一项 description=x 和 id=y 的项目?
解决方案
那将是
list.exists(item => item.description == x && item.id == y)
如果您还equals
为您的课程实现(或者甚至更好,让它case class
自动执行),您可以将其简化为
case class Item(description: String, id: String)
// automatically everything a val,
// you get equals(), hashCode(), toString(), copy() for free
// you don't need to say "new" to make instances
list.contains(Item(x,y))
推荐阅读
- javascript - google.script.run.withSuccessHandler(function) 不返回数据
- jolt - 需要 JOLT spect 将 JSON 转换为预期的输出 json
- python - 如何减少模板匹配的图像搜索区域?
- node.js - “使用严格”在 Node 版本 14 中有用吗?
- sql - 记录类型匹配问题 Oracle 列表
- python - 回调中更新的 Dash 表不会显示
- python - 在函数中打开 csv 文件时出错
- flutter - Flutter 需要在屏幕之间传递一个 List
- spring - 我可以将 KafkaTemplate 与单例范围一起使用吗?
- r - 如何过滤时间窗口内最近发生的事件?