Java:比较/排序任意对象
无论如何,我可以为 JVM 中的所有对象定义一个序列/顺序,以便对于任何两个不同的对象 o1 或 o2,有一个明确定义的规则,即 o1 > > 。 o2或o2> o1 和 o1 == o2 当且仅当它们是同一个对象?
如果有无冲突保证(没有),则 IdentityHashCode() 比较将是一个不错的选择。
出生时间也可以——如果我能以某种方式获得的话。
有什么想法吗?
谢谢你!
Is there anyway I can define a sequence/order for all objects in a JVM so that for any two distinct objects o1 or o2, there's a well defined rule that says either o1 > o2 or o2 > o1 and o1 == o2 if and only if they are the same object?
identityHashCode() comparison would be a good candidate, if there's a no-collision guarantee (there isn't).
Birth time would work too - if I can somehow obtain that.
Any ideas?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您能够维护自己的对象存储库,则可以使用
WeakHashMap
来维护您自己的序列 ID。If you are in a position to maintain your own repository of objects, you could use a
WeakHashMap<Object, Long>
to maintain your own serial IDs.您需要做的就是定义任意稳定的排序。 (您的“对象诞生时间”是
一个这样的想法,但我不认为它被存储)。
方法一:
对于任何两个完全相同类型的对象,您可以通过比较来定义这样的顺序
他们各自的领域。如果所有字段都相同,则对象相等;如果不,
某些字段 f 不同,您可以根据基础类型定义排序。
如果您有两个不同类型的对象,只需使用类型名称来定义顺序即可;这
名字按字典顺序较小的词是“小于”。您可以实现按类型比较
(可能需要大量工作)或者您可以实现一个通用的比较使用反射
枚举字段名称和类型(以启用特定于类型的比较),尽管这可能
非常慢。
方法二:
任何时候调用比较器时,都会缓存线性数组中尚未遇到的任何对象。
这样比较的任何对象现在在数组中都有一个索引位置; 01 < o2 如果索引(o1) <索引(o2)。
您可能需要一个哈希表将分配的索引位置与缓存的对象关联起来。
方法三:
如果您正在使用对象的特定子集,并且有一个规范
生成树,然后对生成树的每个边进行编号,使得子节点成为弧形
有唯一的号码。那么o1 < o2 如果从生成树的根到 o1 的路径,
小于到 o2 的路径。
All you need to do is define an arbitrary stable ordering. (Your "object birth time" is
one such idea, but I don't think it is stored).
Method1:
For any two objects of the same exact type, you can define such an ordering by comparing
their individual fields. If all fields are identical, the objects are equal; if not,
some field f is different and you can define the ordering based on the underlying type.
If you have two objects with different types, simply use the type name to define the order; the
one whose name is lexicographically smaller is "less than". You can implement a compare-per-type
(might be a lot of work) or you can likely implement a generic compare the uses reflection
to enumerate field names and types (to enable type-specific compares), although this might
be pretty slow.
Method2:
Any time you call your comparator, cache any object not yet encountered in a linear array.
Any objects thus compared now have a index position in the array; o1 < o2 if the index(o1) < index(o2).
You might need a hash table to associate assigned index positions with cached objects.
Method3:
If you are working with a specific subset of the objects, and there's a canonical
spanning tree, then number each edge of the spanning tree such that children arcs
have unique numbers. Then o1 < o2 if the path to o1 from the root of the spanning tree,
is less than the path to o2.
您需要实现
Comparable
接口和compareTo(YourObject obj)
方法。 CompareTo(..) 方法的约定是,当该对象小于作为参数传递的对象时返回 -1(-ve 数字),当它们相等时返回 0,当该对象大于时返回 +1(+ve 数字)比其他物体。您可以使用您喜欢的任何字段来实现比较。使用 Collections.sort() 或任何 list.sort() 将使用此比较器对列表进行排序。
希望这有帮助!
You need to implement
Comparable<YourObject>
interface and thecompareTo(YourObject obj)
method. The contract of the compareTo(..) method is to return -1(-ve number) when this object is smaller than the object passed as parameter, 0 when they are equal and +1 (+ve number) when this object is greater than the other object. You can implement the compare to using any fields that you like.Using Collections.sort() or any list.sort() would using this comparator to sort your list.
Hope this helps!
如果您的对象属于同一类型,您可以在构造函数中记住它们的创建编号:
If your objects are of the same type, you can remember their creation number in the constructor:
Object.toString()
方法应该针对格式不同的对象返回不同的值:那么您可以先按字母顺序比较 getClass().getName(),然后再比较 hashCode() 吗?
The
Object.toString()
method should return different values for different objects in format:So could you compare the getClass().getName() alphabetically first, then the hashCode()?