|
||
|
|||
|
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;
}
Last edited by humdinger; 13-03-2010 at 02:16 PM. |
|
||||
|
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); |
| The Following User Says Thank You to copeg For This Useful Post: | ||
humdinger (13-03-2010) | ||
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |