开发者

Java: Compare/sort arbitrary objects

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(开发者_如何学C) 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!


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.


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.


You need to implement Comparable<YourObject> interface and the compareTo(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:

class A {
    private static long count = 0;
    private long objNumber;
    public A() {
        synchronized(A.class) {
            objNumber = count;
            count++;
        }
    }
}


The Object.toString() method should return different values for different objects in format:

getClass().getName() + '@' + Integer.toHexString(hashCode())

So could you compare the getClass().getName() alphabetically first, then the hashCode()?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜