I'm currently trying to match 2 objects based on their values. Except, it's not a.a = a.a
, but a.a = a.b
and a.b = b.a
. This means that overriding equals
is an option but it's certainly not the right option.
While sorting these objects will make the matching time quicker, the population will be small so it is unnecessary. Also, compareTo
isn't exactly right either for the same reason given for equals
.
Do I simply make my own method in case? There will be 4 fields to match which is why I am not using an if statement up front.
public boolean isOpposite(Object other) {
return (this.a == other.b) ? true : false;
}
There is also the possibility that the object will implement/extend a base object to take on more fields and implement its own way of matching.
I'm considering using a LinkedList
because I know it to be quicker for use than ArrayList
, however I've also been considering Map
s.
Edit: better explanation of objects
public class Obj {
public String a;
public String b;
public String c;
public double d;
}
The relationships are as follows:
Obj obj1, obj2;
obj1.a == obj2.b //.equals for String of course
obj1.b == obj2.a
obj1.c == obj2.c
obj1.d == obj2.d * -1
I've done some testing and determined that the cleanest way I knew how to implement this was with using
ArrayList<Obj>
.This was my implementation:
I ran this against a sample size of 4,000,000 psuedo random
Obj
objects. This was my output: