aspose file tools*
The moose likes Beginning Java and the fly likes How to subtract two arrayLists? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to subtract two arrayLists?" Watch "How to subtract two arrayLists?" New topic
Author

How to subtract two arrayLists?

majid nakit
Ranch Hand

Joined: Jun 26, 2001
Posts: 165
Hi,

I have two ArrayList A and B

how to do : A - B
in other word take off all the objects in B from A.

Thanks
[ October 27, 2008: Message edited by: Bear Bibeault ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
a.removeAll(b);


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jackie Gleason
Greenhorn

Joined: Aug 12, 2008
Posts: 8
As far as I know you would have to iterate through set a and remove all instances of set b
List<String> a = new ArrayList<String>();
a.add("Hello");
a.add("World");
a.add("Leaders");
List<String> b = new ArrayList<String>();
b.add("Goodbye");
b.add("World");
b.add("Leaders");
for(String bs:b){
a.remove(bs);
}
for(String as:a){
println(as);
}

This would only print out Hello
[ October 27, 2008: Message edited by: Jackie Gleason ]
Jackie Gleason
Greenhorn

Joined: Aug 12, 2008
Posts: 8
Originally posted by Ilja Preuss:
a.removeAll(b);


As far as I know the function he gives you here does not exist(at least not according to the Java API) There is a removeAll function in the List class but it removes all the items in the list

void removeActionListener(ActionListener l)
Removes the specified action listener so that it no longer receives action events from this list.
void removeAll()
Removes all items from this list.
void removeItemListener(ItemListener l)
Removes the specified item listener so that it no longer receives item events from this list.

Update: Ok so now I am baffled I tried this and it DID work. I am not sure why considering according to the 1.6 doc there is not a function. Anyone have an explination for this? Am I looking at an old API or something?

http://java.sun.com/javase/6/docs/api/
[ October 27, 2008: Message edited by: Jackie Gleason ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Yes, there is a removeAll method, but you will only find it if you look in List, rather than List.








There are two classes/interfaces called List!
Jackie Gleason
Greenhorn

Joined: Aug 12, 2008
Posts: 8
Ahhhhh oops haha
majid nakit
Ranch Hand

Joined: Jun 26, 2001
Posts: 165
Here is what I did but I have an IndexOutOfBoundsException :

public ArrayList substacrtArrays(ArrayList A, ArrayList B)
{
int sizeAbefore = A.size();
try {
for(int k=0; k < B.size();k++)

{
for(int i=0;i<A.size();i++)
{
Role roleA = (Role)A.get(k);
Role roleB = (Role)B.get(i);
if((roleA.getValue()).equals(roleB.getValue()))
A.remove(roleA);

}
}
int sizeAafter = A.size();
}catch(Exception e){
System.out.println(e);
}

return A;


}

your help is appreciated.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You need to go through those for loops carefully counting how many elements you have. Imagine list 1 contains a b c d e and list 2 contains b d f h. Go through them with pencil and paper and see how many iterations you get, and whether you go beyond the size of either list.

In your code, you have two classcasts which should be unnecessary; you should override the equals() method in your Role class, and use equals to check whether the two values are "the same." Then you don't need your casts.
You also have sizeAbefore and sizeAafter local variables which you don't appear to use; you should delete them both.
It is probably better to use an Iterator and its remove method to go through the lists.
Also check carefully which list you are getting the i and k variables from.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
And please use the code button and correct indentation; your code is difficult to read.
Vlad Jigounov
Greenhorn

Joined: Jul 23, 2010
Posts: 1
list1.removeAll(list2); will do the job, but it cost you N1*N2 operations (very slow).
If you work with big arrays you can improve performance like this:


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Welcome to the Ranch Agree that using Sets will improve performance for that sort of thing.

Please use the code button; sinc eyou are new I have edited your post and you can see how much better it looks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to subtract two arrayLists?