开发者

What's the best way to compare two JavaScriptObjects in GWT?

开发者 https://www.devze.com 2023-02-04 11:50 出处:网络
It looks like hashCode() and equals() are declared as final.So overriding the implementation is not possible.It also states that equals() returns true if the objects are JavaScript identical (triple-e

It looks like hashCode() and equals() are declared as final. So overriding the implementation is not possible. It also states that equals() returns true if the objects are JavaScript identical (triple-equals). I'm not quite sure what that means as creating two identical JavaScriptObject's in GWT and comparing them with equals() returns false. Also it looks like hashcode() uses a monotonically increasing counter to assign a hash code to the underlying JavaScript object. If I wanted to store JavaScriptObject开发者_C百科s in a Set this would complicate things. Any help would be much appreciated.


It depends on which equality criteria you want to use for your situation.

  • If you want object identity, you can use the predefined equals() and hashCode() - and put the JavaScriptObjects directly in a HashSet.
  • If you need a different equality notion, you can write your own Comparator and put the JavaScriptObjects in e.g. a TreeSet, created by new TreeSet(comparator).
  • If you need to put JavaScriptObjects in a HashSet (not a TreeSet), and still need a different equality notion, you can't put the JavaScriptObjects directly in the Set. Then you'd have to write a wrapper class, which contains the JavaScriptObject, and implements equals() and hashCode().
0

精彩评论

暂无评论...
验证码 换一张
取 消