File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Set difference method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Set difference method" Watch "Set difference method" New topic
Author

Set difference method

Alireza Bahmanpour
Greenhorn

Joined: Oct 24, 2007
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.

Thanks in advance
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

why you're using same name variable?

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


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
Alireza Bahmanpour
Greenhorn

Joined: Oct 24, 2007
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

Joined: Oct 27, 2005
Posts: 19653
    
  18

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 ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
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

    Joined: Oct 13, 2005
    Posts: 38016
        
      22
    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

    Joined: Oct 27, 2005
    Posts: 19653
        
      18

    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

    Joined: Oct 13, 2005
    Posts: 38016
        
      22
    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

    Joined: Aug 22, 2006
    Posts: 257

    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


    "Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38016
        
      22
    The problem is not an unreachable statement, but a false assignment, writing = instead of ==. Never use "== false" or "== true."
    Anand Hariharan
    Rancher

    Joined: Aug 22, 2006
    Posts: 257

    An assignment expression within a conditional is not an error if the expression evaluates to a boolean.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38016
        
      22
    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

    Joined: Oct 27, 2005
    Posts: 19653
        
      18

    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

    Joined: Oct 13, 2005
    Posts: 38016
        
      22
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Set difference method
     
    Similar Threads
    equals method...
    unit test for java
    Question on Object and equals
    equals() and ==
    Equals and ==