-1

i am trying to create all subsets of a set and group them without duplicates. For example, in a set like {1,2,3,4,5,6}, {(1,2), (3,4), (5,6)} this tuple can be valid on the other hand {(1,2), (2,4), (5,6)} this tuple shouldn't be valid since it has two instance of number 2 although the set has only one instance of number 2.

To guarantee this non-duplicate form, i have a small method that checks the given subset, if it has the number that is passed to the method as parameter, it returns true.

The problem with that, when there is duplicate in the set like {1,2,3,1,5,6} it shouldn't say that the second "1" is equal to first "1". To make this happen, i used "==" operator to compare the object references and i hoped that this will make difference between first "1" and the second "1", but it didn't. Apparently, Eclipse creates same object reference for same type of objects which is created in compile-time and have same value. How can i force Eclipse to create different object references ? If there is a way to flag objects with unique number, that helps also.

Here is the method:

private boolean doesTuplesHave(Integer number, ArrayList<ArrayList<Integer>> tuples){

    for(ArrayList<Integer> list : tuples){
        for(Integer numinTuple : list){
            if( numinTuple == number){
                return true;
            }
        }
    }

    return false;
}
  • Eclipse is the development environment; it is not the compiler (`javac`) or the runtime (`jre`, `java`) -- it is the Java Virtual Machine that is creating the instances of `Integer`. I can force your for-loop `for(Integer numinTuple : list)` to create new separate instances of the `Integer`, but then numinTuple will _never_ be `==` to number. – Stephen P Mar 23 '17 at 21:30
  • Beside the answer below, you understood me, thank you. To run and test the method `doesTuplesHav` i created 6 `Integer` object and add them to 2 `ArrayList` by grouping them by 3 to mimic the `tuples` parameter. In debugging i saw that all Integers in tuples became equal to `number` regardless of their values. Hopefully, i changed the design so i don't need any reference control anymore. – Ozan Polatbilek Mar 25 '17 at 21:28

1 Answers1

2

a) Eclipse has nothing to do with it, this is standard behavior for all Java compilers and JVMs.

b) use .equals()

if(numinTuple.equals(number))

or, if it's possible that one of the sides can be null, use Objects.equals():

if(Objects.equals(numinTuple, number))

Explanation: in Java, the == operator checks reference equality, not semantic equality (i.e. is this the same object vs do these objects have the same value)

See: What is the difference between == vs equals() in Java?


Also, in Java 8, your method can be rewritten in a more concise way as:

return tuples.stream().anyMatch(l->l.contains(number));

(which also fixes your bug)

Community
  • 1
  • 1
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • You got me wrong. I want to check the references between objects not their values. I want no same object in a tuple. With a set like this {1,2,3,4} (1,1) connot be valid but in a set like {1,2,3,1}, (1,1) tuple is valid. With controlling value of objects you can't have (1,1) tuple when the given set is {1,2,3,1}. The problem came from this situation. I want to check if it is same object. But reference of all objects that have same value are same, so it is impossible to check the reference equality. – Ozan Polatbilek Mar 23 '17 at 18:10