Win a copy of Penetration Testing Basics this week in the Security forum!

# Set difference method

Alireza Bahmanpour
Greenhorn
Posts: 23
Hi
I am supposed to right a code which finds the difference between two number sets. The difference between the first set, 'this', and the second set 'mySet' should go into the new set 'result'
(eg. this = {1,2,3}, mySet = {2,3,4} -> result = {1,4})
Here is my code:

Boolean 'same' works as a flag which determines whether the current number exists in both sets or not.

Eclipse gives me an error: 'Local Variable same is never read', and I'm not sure why. I don't even know if this is the best approach to the problem.

Ranch Hand
Posts: 1324
why you're using same name variable?

and I hope your logic is concern with variable value instead of the variable name.

Alireza Bahmanpour
Greenhorn
Posts: 23
I repeat: Boolean 'same' works as a flag which determines whether the current number exists in both sets or not. If after the second for loop, the variable 'same' remains false, then the number goes into the 'result' set, otherwise it won't.

I really don't know if this is the right thing to do. I appreciate if you correct me.

Rob Spoor
Sheriff
Posts: 20706
68
Originally posted by Alireza Bahmanpour:
if(same=false)

That's not reading same - that's setting same to false, then reading it. The result will be false and the body of this if-statement will never be executed.

Another hint: Collection has a method called contains which does exactly what your inner loops do. You can replace them with this:

And of course vice versa for the second inner loop.
[ October 12, 2008: Message edited by: Rob Prime ]

Campbell Ritchie
Sheriff
Posts: 50617
82
That isn't actually the set difference; the set difference would be {1, 2, 3}-{2, 3, 4} = {1}, and {2, 3, 4}-{1, 2, 3} = {4}. Some people call set difference complement and write {1, 2, 3}\{2, 3, 4}.

What you are actually looking for is some sort of disjunction, which you would have to work out by taking the intersection {1, 2, 3}∩{2, 3, 4} = {2, 3} and the union {1, 2, 3}∪{2, 3, 4} = {1, 2, 3, 4} and then subtracting them {1, 2, 3, 4}-{2, 3} = {1, 4} or {2, 3}-{1, 2, 3, 4} = ∅ (empty set).

As you see, you can do this in three stages, work out the union, work out the intersection and subtract the intersection from the union. At least if you use the methods of the java.util.Set interface, which contains methods which represent union difference and intersection.

Have you been told to write your own set implementation?

Please use ctrl-c ctrl-v to post code; I am sure "if (same=false)" didn't appear in your code.

Your code is over-complicated; for example if (same == false) should be changed to if (!same). And the break; statements are potentially confusing, which is why they are discouraged in this Style Guide.

Running a linear search in one object through the contents of another object is not good design.

Suggest:
• Create a public boolean contains(Object obj) method in the set class. This may require a linear search.
• Create three set objects, two full one empty.
• In another class, iterate through set1; if set2 doesn't contain an element, add it to set3
• Iterate through set2; if set1 doesn't contain an element, add it to set3
•
Campbell Ritchie
Sheriff
Posts: 50617
82
Surely if(same=false) is a misprint, Rob, otherwise Eclipse would say more than "not used;" it would be a compiler error.
[ October 12, 2008: Message edited by: Campbell Ritchie ]

Rob Spoor
Sheriff
Posts: 20706
68
Of course it's not a compiler error. b is a boolean, so b = false first assigns false to b, then evaluates b - to false. My Eclipse would issue a warning, but the syntax surely allows it.

Campbell Ritchie
Sheriff
Posts: 50617
82
No, you are right Rob, it will compile. I was mistaken.

In which case it is a major logic error . . . It should be changed to if (!same) . . .

Eclipse gives a yellow mark and a warning about "Possible accidental assignment . . ."

Anand Hariharan
Rancher
Posts: 272
It is not a compiler error because the JLS makes an exception for the if condition w.r.t reachable statements:

http://java.sun.com/docs/books/jls/third_edition/html/statements.html#236419

(BTW, I looked the HTML source to get the anchor within the page, so if it is generated on the fly, I may be off.)

- Anand

Campbell Ritchie
Sheriff
Posts: 50617
82
The problem is not an unreachable statement, but a false assignment, writing = instead of ==. Never use "== false" or "== true."

Anand Hariharan
Rancher
Posts: 272
An assignment expression within a conditional is not an error if the expression evaluates to a boolean.

Campbell Ritchie
Sheriff
Posts: 50617
82
It is a major logic error if you think you are using the value you assigned to the boolean in the previous statement . . .

Rob Spoor
Sheriff
Posts: 20706
68
I agree with you that you shouldn't do it. I never use == false or == true, and I've set up my Eclipse to warn me about those boolean assignments in case I type something like this:

I believe that, should I want this, I need double brackets:

Campbell Ritchie
Sheriff
Posts: 50617
82
This is serious, Rob and me in complete agreement.

I have also got Eclipse set up with a yellow warning for that sort of thing, but I had thought it would be like when you write = instead of == for numbers-a compiler error.