Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: how to compare two set values

  1. #1
    Join Date
    Nov 2009
    Thanked 0 Times in 0 Posts

    Default how to compare two set values

    Hi guys,

    I need to itterate through 2 sorted sets and compare the values in the set. These sets are of type String and are called listOfNames1 and listOfNames2. If they are not the same i need these values to be added to another set called different.

    The code I have is below. This works as I expect it to when i change if (names1 != names2) to if (names1 == names2) but when im trying to get it to add the names that are not the same it just adds all the names in listOfNames1 to the sortedSet different.

     public void notTheSame() 
          SortedSet<String>different = new TreeSet<String>();
          for(String names1 : listOfNames1)
             for(String names2 : listOfNames2)
                if (names1 != names2)
             }return null;

    Hope someone can help. thankyou in advance for anyone that takes the time to try.
    Last edited by humdinger; March 13th, 2010 at 10:16 AM.

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 833 Times in 772 Posts
    Blog Entries

    Default Re: how to compare two set values

    There is an easier way to find similarities and differences between sets...look at The Set Interface (The Java™ Tutorials > Collections > Interfaces) and scroll down to the Bulk Operations where there are code listings for how to go about doing this. For example, to find the difference:
    Set<String> difference = new HashSet<String>(listOfNames1);

  3. The Following User Says Thank You to copeg For This Useful Post:

    humdinger (March 13th, 2010)

Similar Threads

  1. Get <SELECT> tag values on other jsp page
    By nehakuls in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: November 18th, 2009, 02:29 AM
  2. Eliminating duplicate values
    By Harry_ in forum Collections and Generics
    Replies: 7
    Last Post: November 9th, 2009, 06:35 AM
  3. Values of Input
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 8th, 2009, 03:46 AM
  4. Substitution of Values in Array
    By nyeung in forum Collections and Generics
    Replies: 2
    Last Post: October 26th, 2009, 08:02 PM
  5. String + Compare // Might be too easy for ya
    By Jangan in forum Java Theory & Questions
    Replies: 1
    Last Post: October 18th, 2009, 05:40 PM