一组如何确定两个对象在 dart 中相等? [英] How does a set determine that two objects are equal in dart?

查看:30
本文介绍了一组如何确定两个对象在 dart 中相等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白集合如何确定两个对象何时相等.更具体地说,一个集合的add方法什么时候真正添加了一个新对象,什么时候它不是一个新对象,因为该对象已经在集合中了?

I don't understand how a set determines when two objects are equal. More specific, when does the add method of a set, really adds a new object, and when doesn't it act a new object, because the object is already in the set ?

例如,我有来自以下类的对象:

For example, I have objects from the following class:

class Action {
  final Function function;
  final String description;

  Action(this.function, this.description);

  call() => function();

  toString() => description;
}

现在我认为以下集合将包含 2 个元素,因为其中 2 个是相等的:

Now I would think that the following set would contain 2 elements, as 2 of them are equal:

void main() {
  Set<Action> actions = new Set()
    ..add(new Action(() => print("a"), "print a"))  
    ..add(new Action(() => print("a"), "print a"))
    ..add(new Action(() => print("b"), "print b"));
}

但是,这个集合包含 3 个 Action 对象.请参阅演示.如何确保相同的对象在集合中被视为相同?

But instead, this set contains 3 Action objects. See the demo. How can I make sure that equal objects are seen as equal in the set ?

推荐答案

有关 Dart 中 operator== 的综合文章,请参阅 http://work.j832.com/2014/05/equality-and-dart.html

For a comprehensive write-up about operator== in Dart see http://work.j832.com/2014/05/equality-and-dart.html

它只是检查它们是否相等 a == b 您可以覆盖 == 运算符来自定义此行为.请记住,当 == 运算符被覆盖时,hashCode 也应该被覆盖.

It just checks if they are equal a == b You can override the == operator to customize this behavior. Keep in mind that also hashCode should be overridden when the == operator is overridden.

class Action {
  @override
  bool operator==(other) {
    // Dart ensures that operator== isn't called with null
    // if(other == null) {
    //   return false;
    // }
    if(other is! Action) {
      return false;
    }
    return description == (other as Action).description;
  }

  // hashCode must never change otherwise the value can't
  // be found anymore for example when used as key 
  // in hashMaps therefore we cache it after first creation.
  // If you want to combine more values in hashCode creation
  // see http://stackoverflow.com/a/26648915/217408
  // This is just one attempt, depending on your requirements
  // different handling might be more appropriate.
  // As far as I am aware there is no correct answer for
  // objects where the members taking part of hashCode and
  // equality calculation are mutable.
  // See also http://stackoverflow.com/a/27609/217408
  int _hashCode;
  @override
  int get hashCode {
    if(_hashCode == null) {
      _hashCode = description.hashCode
    }
    return _hashCode;
  }
  // when the key (description) is immutable and the only
  // member of the key you can just use
  // int get hashCode => description.hashCode
}

试试 DartPad

这篇关于一组如何确定两个对象在 dart 中相等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆