Reputation: 12431
I am brand new to Java :)
I have 2 String lists and I was wondering what would be the most efficient way to compare the two and have a resulting array which contains strings that are not in the other. For example, I have a list called oldStrings and one called Strings. I have seen the Comparator function but don't fully understand how it works, right now I was thinking I could create a for loop, loop through each string and then save that string:
for (final String str : oldStrings) {
if(!strings.contains(str))
{
getLogger().info(str + " is not in strings list ");
}
}
There's going to be up to 200 strings in this list. Would this be the best way to go about this? Thank you!
Upvotes: 0
Views: 1423
Reputation: 92066
You should be using Google Guava's Sets
utilities.
Set<String> s = Sets.newHashSet("a", "b", "c", "d");
Set<String> t = Sets.newHashSet("f", "g", "a", "c");
Sets.SetView<String> difference = Sets.difference(s, t);
System.out.println(difference); // prints [b, d]
Upvotes: 0
Reputation: 240938
Collection firstList = new ArrayList() {{
add("str1");
add("str2");
}};
Collection secondList = new ArrayList() {{
add("str1");
add("str3");
add("str4");
}};
System.out.println("First List: " + firstList);
System.out.println("Second List: " + secondList);
// Here is main part
secondList.removeAll(firstList);
System.out.println("Result: " + secondList);
Update: More sophisticated version of code
Collection<String> firstList = new ArrayList<String>();
firstList.add("str1");
firstList.add("str2");
Collection<String> secondList = new ArrayList<String>();
secondList.add("str1");
secondList.add("str2");
secondList.add("str3");
System.out.println("First List: " + firstList);
System.out.println("Second List: " + secondList);
// Here is main part
secondList.removeAll(firstList);
Update:
To Get acctual difference between both String list go for this.
Set<String> setOne = new HashSet<String>();
Set<String> setTwo = new HashSet<String>();
setOne.add("1");
setOne.add("2");
setOne.add("5");
setTwo.add("1");
setTwo.add("3");
setTwo.add("4");
Set<String> setTwoDummy = new HashSet<String>(setTwo);
setTwo.retainAll(setOne);
setTwoDummy.addAll(setOne);
setTwoDummy.removeAll(setTwo);
System.out.println(""+setTwoDummy);
Upvotes: 9
Reputation: 3735
Compare two lists of strings and have a resulting array which contains strings that are not in the other.
The description is ambiguous because we don't we don't know if we need just non matching strings from the first list, the second list, or both. Below is pseudo code for both.
for (String str : oldStrings)
{
if(strings.contains(str))
{
intersectionList.add(str);
}
}
oldStrings.removeAll(intersectionList);
strings.removeAll(intersectionList);
result = strings.addAll(oldStrings).toArray();
Or
copyStrings = strings.clone();
strings.removeAll(oldStrings);
oldStrings.removeAll(copyStrings);
result = strings.addAll(oldStrings).toArray();
Upvotes: 1
Reputation: 17775
First, the problem with your solution is that it will only find elements that are in oldStrings
and not strings
. If you're going with this approach then you need to loop on the other list as well.
If this is not for homework then check out CollectionUtils.disjunction from Apache Commons Collections.
Upvotes: 4