IEqualityComparer interface in Java

后端 未结 4 1616
野性不改
野性不改 2021-02-13 12:27

More specifically I want an interface to compare objects which can be only compared for equality [e.g. complex numbers] but don\'t have total order on them. It should have [Not

4条回答
  •  渐次进展
    2021-02-13 12:36

    There's no built-in type that is used for this in Java. It's a "hole" in the collections design, IMO. There's the string-specific Collator class which is about as close as it gets, I'm afraid.

    There's no way of customizing the built-in maps to use a specific kind of equality comparison, worse luck. It's entirely reasonable to want this functionality, and a real pain that it's not already present.

    You could create your own such interface of course, and write your own map variants which use it... but having to do so sucks :(

提交回复
热议问题