Hi, I'm trying to learn some Java for fun and got a question about comparing integers. If I wish to compare int a to int b and check for equality that's pretty straightforward and easy, but if I want a method to return equal for different values I get a problem. Simplified version of my problem: Lets say I have 12 possible values for a and b where there are six pairs of ints I want to compare to equal. So for example if a and b both are 1 or 2 they are equal, but also if a is 1 and b is 2 I would like that to compare to equal as well. I've managed to create a method that works(for my purposes at least), but it's such horrible code and doesn't scale well at all(in my program there are more than just 2 values that should return an equality). Bruteforce solution:
private boolean checkEquality(int a, int b) {
if (a == b) return true;
if (a == 1 && b == 2) || (a == 2 && b == 1) return true;
if (a == 3 && b == 4) || (a == 4 && b == 3) return true;
//and so on for the different pairs
return false;
}
Any advice on how to improve on this code? I'm fairly certain there's a way to at least ignore the ordering of a and b, but can't seem to figure it out.
English is my secondary language, so if I was unclear in my question I apologize and will clear up any confusion in the comments.