1
포함의 HashMap은 내 오브젝트에서 사용자 지정 연산자를 정의하는 방법
class CharNode {
String char = "x";
String color = "#000000";
String backgroundColor = "none";
CharNode(this.char) { }
bool operator ==(CharNode other) {
return (other.char == char && other.backgroundColor == other.backgroundColor && other.color == color);
}
}
을 그리고 난이 CharNodes (예) 비교할 때 작동 : 사실 인쇄
CharNode cn1 = new CharNode("A");
CharNode cn2 = new CharNode("A");
print((cn1 == cn2).toString());
합니다.
문제의 HashMap (실시 예)과의 객체를 찾는 것이다 : FALSE 인쇄
HashMap<CharNode, int> map = new HashMap<CharNode, int>();
map[cn1] = 0;
print((map.contains(cn2)).toString());
.
질문 : HashMap에 내 사용자 정의 연산자를 사용하는 방법은 무엇입니까? forEach를 사용해야합니까?
그래서 문제는 hashCode 평등에 있습니까? hashCode를 수동으로 설정할 수 있습니까? – ViliX64
네, 그게 바로 당신이해야 할 일입니다. 자세한 내용은 http://pchalin.blogspot.ca/2014/04/defining-equality-and-hashcode-for-dart.html을 참조하십시오. –
명확히하기 위해, 당신은'hashCode'를 수동으로 설정하지 않습니다. 당신은'hashCode' getter를 오버라이드합니다. – Tonio