In Java, you can use the Collection interface's removeAll method.
// Create a couple ArrayList objects and populate them
// with some delicious fruits.
Collection firstList = new ArrayList() {{
add("apple");
add("orange");
}};
Collection secondList = new ArrayList() {{
add("apple");
add("orange");
add("banana");
add("strawberry");
}};
// Show the "before" lists
System.out.println("First List: " + firstList);
System.out.println("Second List: " + secondList);
// Remove all elements in firstList from secondList
secondList.removeAll(firstList);
// Show the "after" list
System.out.println("Result: " + secondList);
The above code will produce the following output:
First List: [apple, orange]
Second List: [apple, orange, banana, strawberry]
Result: [banana, strawberry]
Answer from William Brendel on Stack OverflowIn Java, you can use the Collection interface's removeAll method.
// Create a couple ArrayList objects and populate them
// with some delicious fruits.
Collection firstList = new ArrayList() {{
add("apple");
add("orange");
}};
Collection secondList = new ArrayList() {{
add("apple");
add("orange");
add("banana");
add("strawberry");
}};
// Show the "before" lists
System.out.println("First List: " + firstList);
System.out.println("Second List: " + secondList);
// Remove all elements in firstList from secondList
secondList.removeAll(firstList);
// Show the "after" list
System.out.println("Result: " + secondList);
The above code will produce the following output:
First List: [apple, orange]
Second List: [apple, orange, banana, strawberry]
Result: [banana, strawberry]
You already have the right answer. And if you want to make more complicated and interesting operations between Lists (collections) use apache commons collections (CollectionUtils) It allows you to make conjuction/disjunction, find intersection, check if one collection is a subset of another and other nice things.
Use Apache Commons Collections (javadoc):
CollectionUtils.disjunction(a, b);
See also: Effective Java, 2nd edition, Item 47: Know and use the libraries (The author mentions only the JDK's built-in libraries but I think the reasoning could be true for other libraries too.)
If I understood your question correctly then following method nonOverLap in the code below should get you that:
<T> Collection<T> union(Collection<T> coll1, Collection<T> coll2) {
Set<T> union = new HashSet<>(coll1);
union.addAll(new HashSet<>(coll2));
return union;
}
<T> Collection<T> intersect(Collection<T> coll1, Collection<T> coll2) {
Set<T> intersection = new HashSet<>(coll1);
intersection.retainAll(new HashSet<>(coll2));
return intersection;
}
<T> Collection<T> nonOverLap(Collection<T> coll1, Collection<T> coll2) {
Collection<T> result = union(coll1, coll2);
result.removeAll(intersect(coll1, coll2));
return result;
}
if your requirement is absolute difference between the list's element values then
Try This
int length=firstList.size();
for(int i=0;i<length;i++){
resultList.add(Math.abs(firstList.get(i)-secondList.get(i)));
}
Assuming Size of Both the list is same !
So unless you are looking for a specifically optimized solution, you can iterate the lists, get the numbers from both input lists at a particular index and find the difference (believing that mere subtraction would do, judging which number is greater!) and add it to third list. You may need to handle situation where size of one list is less than other.
Implement equals and hashCode in your custom object and you can use the same approach as you did with Strings.
Well, the removeAll method is a generic library method which doesn't know anything about your model class. So if you think about it for a second, how is it going to know which ones are "the same"?
The short answer is that you need to override the equals() method in your Model class, as this is what the checks are based on. The implementation should return true for any pair of model instances that you wish to be considered the same - the default inherited behaviour returns true only if they're the same object in memory. (And as always, when you override equals() you must override hashCode() too).
You can convert them to Set collections, and perform a set difference operation on them.
Like this:
Set<Date> ad = new HashSet<Date>(a);
Set<Date> bd = new HashSet<Date>(b);
ad.removeAll(bd);
If you only want find missing values in b, you can do:
List toReturn = new ArrayList(a);
toReturn.removeAll(b);
return toReturn;
If you want to find out values which are present in either list you can execute upper code twice. With changed lists.
fw_diff= fw_week.clone().removeAll(sw_week)
sw_diff=sw_week.clone().removeAll(fw_week)
More efficient one:
fw_diff= fw_week.clone().removeAll(sw_week)
sw_diff=sw_week.clone().removeAll(fw_diff)
Here,fw_diff contains intersaction of both the list. So now , for sw_diff , we need to remove only fw_diff from sw_week . No need to remove all of fw_week .
We do the same thing in our application like this:-
Our scenario is we have 2 arraylist and I need to find the difference between first and second arraylist.
Following is the code snippet:-
this.lstDifference=(ArrayList<String>) ListUtils.subtract(FirstArrayList,SecondArrayList);
The type of arraylist can be changed depending upon your two arraylists.