Java: comparing / sorting arbitrary objects

In any case, I can determine the sequence / order for all objects in the JVM, so for any two different objects o1 or o2 there is a well-defined rule that says either o1> o2 or o2> o1 and o1 == o2 if and only if are they the same object?

identityHashCode () comparison would be a good candidate if there is a guarantee of the absence of a collision (not there).

The time of birth will also work - if I can somehow get it.

Any ideas?

Thanks!

+6
source share
5 answers

All you have to do is define an arbitrary stable ordering. (Your โ€œobjectโ€™s 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 this order 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 an order based on the base type. If you have two objects with different types, just use the type name to determine the order; one whose name is lexicographically less is less. You can implement a comparison for each type (maybe a lot of work), or you can most likely implement a general analysis of using reflexes to list field names and types (to enable type comparisons), although this can be quite slow.

Method2: Every time you call your comparator, cache any object that is not yet in the linear array. All objects matched in this way now have an index position in the array; o1 <o2 if index (o1) Index (o2). You may need a hash table to associate the assigned index positions with cached objects.

method3: If you are working with a specific subset of objects, and there is a canonical spanning tree, then specify each edge of the spanning tree so that the child 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.

+2
source

If you are able to maintain your own repository of objects, you can use WeakHashMap<Object, Long> to support your serial identifiers.

+3
source

You need to implement the Comparable<YourObject> interface and the compareTo(YourObject obj) method compareTo(YourObject obj) . The contract of the compareTo (..) method should return -1 (-ve number) when this object is less than the object passed as a parameter, 0 when they are equal, and +1 (+ ve number) when this object is larger than another object. You can implement the comparison using any fields that you like.

Using Collections.sort () or any list.sort () will use this comparator to sort your list.

Hope this helps!

+1
source

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++; } } } 
0
source

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

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

So you can compare getClass (). getName () in alphabetical order first and then hashCode ()?

0
source

Source: https://habr.com/ru/post/885632/


All Articles