Go Back   Java Programming Forums > Java Standard Edition Programming Help > Collections and Generics


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 13-03-2010, 01:34 PM
Member
 

Join Date: Nov 2009
Posts: 30
Thanks: 27
Thanked 0 Times in 0 Posts
humdinger is on a distinguished road
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.



Java Code
 public void notTheSame() 
   {
      SortedSet<String>different = new TreeSet<String>();
      for(String names1 : listOfNames1)
      {
         for(String names2 : listOfNames2)
         {
            if (names1 != names2)
            {
               different.add(names1);
            }
         }return null;
      }
Hope someone can help. thankyou in advance for anyone that takes the time to try.




Last edited by humdinger; 13-03-2010 at 02:16 PM.
Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 13-03-2010, 03:46 PM
copeg's Avatar
Moderator
 
9 Highscores

Join Date: Oct 2009
Posts: 570
Thanks: 7
Thanked 131 Times in 125 Posts
copeg will become famous soon enoughcopeg will become famous soon enough

I'm feeling Sleepy
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:
Java Code
Set<String> difference = new HashSet<String>(listOfNames1);
difference.removeAll(listOfNames2);
Reply With Quote
The Following User Says Thank You to copeg For This Useful Post:
humdinger (13-03-2010)
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
Get <SELECT> tag values on other jsp page nehakuls JavaServer Pages: JSP & JSTL 0 18-11-2009 06:29 AM
Eliminating duplicate values Harry_ Collections and Generics 7 09-11-2009 10:35 AM
Values of Input chronoz13 What's Wrong With My Code? 10 08-11-2009 07:46 AM
Substitution of Values in Array nyeung Collections and Generics 2 27-10-2009 12:02 AM
String + Compare // Might be too easy for ya Jangan Java Theory & Questions 1 18-10-2009 10:40 PM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming practice problems java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

All times are GMT. The time now is 02:07 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.