Results 1 to 9 of 9
  1. #1
    JavaJ is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default Duplicates in more than two sets

    Good day...

    I have a map which is declared to hold String and TreeSet integer.
    like so :
    Name : 10 , 20 , 40
    Name2 : 12 , 33, 50
    Name3: 14, 50, 20
    Name4: 20,10,80
    ..etc

    I want to ensure that each name has unique set of integers no duplicates allowed even if the duplicate number is in another name's set..

    for two sets I can compare directly and remove duplicates but what is the best way to ensure that there is no duplicates while there are more than two sets ??

    Thank you in advance

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Have a look at the Set.removeAll( ... ) method; it returns true if the Set has changed because of the method.

    kind regards,

    Jos
    Last edited by JosAH; 12-03-2009 at 02:35 PM.

  3. #3
    JavaJ is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    JosAh

    thanks for your reply ..

    If I got it right... the problem still exists I should repeat this multiple times..
    someVar.get("Name").removeAll(someVar.get("Name1") );
    someVar.get("Name").removeAll(someVar.get("Name2") );

    ...etc

    regards

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by JavaJ View Post
    JosAh

    thanks for your reply ..

    If I got it right... the problem still exists I should repeat this multiple times..
    someVar.get("Name").removeAll(someVar.get("Name1") );
    someVar.get("Name").removeAll(someVar.get("Name2") );

    ...etc

    regards

    Yep, you have to take the 'difference' of all the sets, if any of those two sets have a non-empty intersection the first set is changed and so the removeAll method returns true.

    kind regards,

    Jos

  5. #5
    JavaJ is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    Thanks for your quick response .. one more question :
    suppose I have two tables of these
    table 1
    Name : 10 , 20 , 40
    Name2 : 12 , 33, 50
    Name3: 14, 50, 20
    Name4: 20,10,80

    table2
    ... etc

    How can I ensure that table 2 also doesn't have any duplicates from the first table integer sets ..?

    Best Regards

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Take the union of all sets in the first table and use that to removeAll( ... ) for all the sets in the second table. If something is actually removed the intersection of the two tables wasn't empty. If your data survives this test do the first test per table.

    kind regards,

    Jos

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default

    How are these things being populated?
    Wouldn't it be better to have some sort of master set. As you add numbers into the other sets, you add them into the master set first and only add them to these other sets if they didn't already exist in the master.

    Something along those lines anyway.

  8. #8
    JavaJ is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    They are populated thru a populatemethod(name[], name2[], name3[],name4[])

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default

    So you have a method that takes several arrays of numbers?

    Then I'd use my method to ensure uniqueness, as you populate whataver it is you're populating.

    That's not to say someone else won't have a better technique...or at least a more stylish one.

Similar Threads

  1. How to plot a graph for two sets of variables....
    By Megatron in forum New To Java
    Replies: 9
    Last Post: 04-20-2009, 09:20 PM
  2. Printing the union and instersection of two sets
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 07:35 PM
  3. No duplicates allowed in Sets
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-21-2008, 04:33 PM
  4. Duplicates
    By Gambit17 in forum New To Java
    Replies: 5
    Last Post: 11-08-2007, 09:56 AM
  5. duplicates in iReport
    By Heather in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 04:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •