val done = HashSet<StudentProgrammeState>()
处理结束时,检查如下:
if (done.distinctBy { it.hashCode() }.size < done.size ) {
println("Duplicate states were evaluated.")
}
每次运行都会显示此消息。自从
done
是一个
HashSet
HashCode
下面是
StudentProgrammeState
:
class StudentProgrammeState(val program: Program) {
val instances = HashSet<StudentModuleInstance>()
override fun equals(other: Any?): Boolean {
if (!(other is StudentProgrammeState)) return false
return (other.program == program) &&
(other.instances.containsAll(instances) &&
instances.containsAll(other.instances))
}
override fun hashCode() = Objects.hash(program, instances)
Equals
这里不直接检查
hashCode
instances
但是这个测试应该对应于无序的集合相等。
对于学生模块仪器:
typealias StudentModuleInstance = Pair<Module, Int>
Pair<>
是内在的
data class
equals
和
hashcode
价值
program