参见英文答案 Use objects as keys in TObjectDictionary2个 我有以下课程: TTest = classprivate FId: Integer; FSecField: Integer; FThirdField: Integer;public constructor Create(AId, ASecField, AThirdField: Integer); // ..... end;
我有以下课程:
TTest = class private FId: Integer; FSecField: Integer; FThirdField: Integer; public constructor Create(AId, ASecField, AThirdField: Integer); // ..... end;
然后我像这样创建一个TObjectDictionary:
procedure TMainForm.btnTestClick(Sender: TObject); var TestDict: TObjectDictionary<TTest, string>; Instance: TTest; begin TestDict := TObjectDictionary<TTest, string>.Create([doOwnsKeys]); try TestDict.Add(TTest.Create(1, 1, 1), ''); if TestDict.ContainsKey(TTest.Create(1, 1, 1)) then ShowMessage('Match found') else ShowMessage('Match not found'); Instance := TTest.Create(1, 1, 1); TestDict.Add(Instance, 'str'); if TestDict.ContainsKey(Instance) then ShowMessage('Match found') else ShowMessage('Match not found'); finally TestDict.Free; end; end;
结果是:“未找到匹配”,“找到匹配”.我应该怎么做才能比较每个键的字段值而不是它们的地址?
实例引用变量的默认相等比较器比较引用而不是对象.因此,您获得对象身份而不是价值身份.因此,如果您希望强加价值标识,则需要提供自定义相等比较器.
TestDict := TObjectDictionary<TTest, string>.Create( [doOwnsKeys], TEqualityComparer<TTest>.Construct(EqualityComparison, Hasher) );
其中EqualityComparison,Hasher是比较和散列函数.它们可以像这样实现:
EqualityComparison := function(const Left, Right: TTest): Boolean begin Result := (Left.FId = Right.FId) and (Left.FSecField = Right.FSecField) and (Left.FThirdField = Right.FThirdField); end; Hasher := function(const Value: TTest): Integer begin Result := BobJenkinsHash(Value.FId, SizeOf(Value.FId), 0); Result := BobJenkinsHash(Value.FSecField, SizeOf(Value.FSecField), Result); Result := BobJenkinsHash(Value.FThirdField, SizeOf(Value.FThirdField), Result); end;