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.

what you could do is create a class ValidCombo, which has two int instance variables.
then, create an array of 'valid' combinations. for instance:

...
// instance var
ValidCombo[] valid = new ValidCombo[3];
...
// in your main
valid[0] = new ValidCombo(1, 1);
valid[1] = new ValidCombo(1, 2);
valid[2] = new ValidCombo(2, 1);
...
public boolean isValid(int a, int b){
  for ( ValidCombo combo : valid ){
    if ( combo.getA() == a && combo.getB() == b )
      return true;
  }
  return false;
}

what you could do is create a class ValidCombo, which has two int instance variables.
then, create an array of 'valid' combinations. for instance:

...
// instance var
ValidCombo[] valid = new ValidCombo[3];
...
// in your main
valid[0] = new ValidCombo(1, 1);
valid[1] = new ValidCombo(1, 2);
valid[2] = new ValidCombo(2, 1);
...
public boolean isValid(int a, int b){
  for ( ValidCombo combo : valid ){
    if ( combo.getA() == a && combo.getB() == b )
      return true;
  }
  return false;
}

Seems like a better solution than I have, but I do notice you add separate lines for (2,1) and (1,2) which in this example isn't so bad, but when there are say 4 different values that should be considered equal that's quite a few lines as it increases exponentially ((x-1)squared): (a==1 && b == 2) || (a==1 && b == 3) || (a==1 && b == 4) //repeat for a == 2,3,4. I guess I could take the array part of your idea but use arraylists and use .contains(a) to eliminate the difference between (1,2) and (2,1) but not sure if that's the right way to go.

yes, you could do that, but that would land you just a few lines less. you would still need the same amount of ValidCombo objects in your arraylist.

your code would be something like this:

...
// instance var
ArrayList<ValidCombo> valid = new ArrayList<ValidCombo>();
...
// in your main
valid.add(new ValidCombo(1, 1));
valid.add(new ValidCombo(1, 2));
valid.add(new ValidCombo(2, 1));
...
public boolean isValid(int a, int b){
  return valid.contains(new ValidCombo(a, b);
}

ADDED

if on the other hand you know that for each int x and int y, if (x, y) is a valid combo, then (y, x) is also a valid combo.
in this case, you could write it like:

...
// instance var
ArrayList<ValidCombo> valid = new ArrayList<ValidCombo>();
...
// in your main
valid.add(new ValidCombo(1, 1));
valid.add(new ValidCombo(1, 2));
...
public boolean isValid(int a, int b){
  if ( valid.contains(new ValidCombo(a, b))
    return true;
  return valid.contains(new ValidCombo(b, a));
}

Edited 4 Years Ago by stultuske: n/a

As I understand it you have a closed range of ints, and you want arbitrary subsets of that range to compare equal. You need some kind of data structure to encode which subsets are equal.
Let's number the subsets. Values 1,2 should compare equal, let''s put them in subset 1. Values ,3,4,5,6 should compare equal, let's put them are in subset 2 (etc).
We can hold that as a trivial array

int[] subset = {1,1,2,2,2,2 ...

and the compare is

boolean compare(int a, int b) {
   return subset[a] == subset[b];
}

If the range is very large, then instead of an array use a map<Integer, Integer> that maps values to their subset.

Edited 4 Years Ago by JamesCherrill: n/a

This article has been dead for over six months. Start a new discussion instead.