?
-
Should I adopt the first or the second way?
The first makes it faster. The second makes the code shorter.
The size to compare is not in the millions so which should i go with?
First Way:
private Comparator createXComparator() {
return new Comparator() {
public int compare(Object object1, Object object2) {
int value = 0;
A a1 = (A) object1;
A a2 = (A) object2;
if (a1.getX() < a2.getX()) {
value = -1;
} else {
value = 1;
}
return value;
}
};
}
private Comparator createYComparator() {
return new Comparator() {
public int compare(Object object1, Object object2) {
int value = 0;
A a1 = (A) object1;
A a2 = (A) object2;
if (a1.getY() < a2.getY()) {
value = -1;
} else {
value = 1;
}
return value;
}
};
}
Second Way:
private Comparator createComparator() {
return new Comparator() {
public int compare(Object object1, Object object2) {
int value = 0;
A a1 = (A) object1;
A a2 = (A) object2;
if (...) {
if (a1.getX() < a2.getX()) {
value = -1;
} else {
value = 1;
}
} else {
if (a1.getY() < a2.getY()) {
value = -1;
} else {
value = 1;
}
}
return value;
}
};
}
The first makes it faster. The second makes the code shorter.
The size to compare is not in the millions so which should i go with?
First Way:
private Comparator createXComparator() {
return new Comparator() {
public int compare(Object object1, Object object2) {
int value = 0;
A a1 = (A) object1;
A a2 = (A) object2;
if (a1.getX() < a2.getX()) {
value = -1;
} else {
value = 1;
}
return value;
}
};
}
private Comparator createYComparator() {
return new Comparator() {
public int compare(Object object1, Object object2) {
int value = 0;
A a1 = (A) object1;
A a2 = (A) object2;
if (a1.getY() < a2.getY()) {
value = -1;
} else {
value = 1;
}
return value;
}
};
}
Second Way:
private Comparator createComparator() {
return new Comparator() {
public int compare(Object object1, Object object2) {
int value = 0;
A a1 = (A) object1;
A a2 = (A) object2;
if (...) {
if (a1.getX() < a2.getX()) {
value = -1;
} else {
value = 1;
}
} else {
if (a1.getY() < a2.getY()) {
value = -1;
} else {
value = 1;
}
}
return value;
}
};
}