本文介绍了Java中的IEqualityComparer接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更具体地说,我希望接口比较只能比较相等性的对象[例如,复数],但没有总订单数.应该有[注意,它只会返回布尔值yes/no]

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 [Note that it only returns a boolean yes/no]

boolean Equals(T object1, T object2);

和一个哈希码功能.因此,当我使用说要收集相等"对象的对象将它们分配到存储桶中时,两个相等"对象不会最终出现在两个不同的存储桶中.

and a hashcode function. So that when I distribute them into buckets using objects say to collect "equal" objects, 2 "equal" objects don't end up in two different buckets.

int getHashCode(T object);

Java有一个吗?我搜索了却找不到.

Does Java have one? I searched and couldn't find it.

我试图在Hadoop Map Reduce中使用此对象,以将相等"对象分配给相同的reduce作业,以便我可以对所有相等"对象进行操作.我只关心对象是否相等,不需要总订单.但是,如果两个对象相等,则它们应具有相同的哈希码.否则,它们将最终完成两个不同的reduce工作.

I am trying to use this in Hadoop Map Reduce to distribute "equal" objects to same reduce job so that I can operate on all "equal" objects. I only care about whether objects are equal or not and don't need total order. But if two objects are equal they should have same hash code. Otherwise they will end up in two different reduce jobs.

请注意,我了解对象的等于和哈希码.但是我想要一个外部比较器,它只依赖于对象的一部分.因此,对象的平等概念与我的不同.

Please note that I know about equals and hashcode of object. But I want an external comparator which say depends on only part of an object. So object's notion of equality differs from mine.

推荐答案

Java中没有用于此的内置类型.IMO是收藏设计中的一个漏洞".有特定于字符串的 Collat​​or 类,它是恐怕要尽可能近了.

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 :(

这篇关于Java中的IEqualityComparer接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 19:36