[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
Solved

# ArrayList Comparistion

Posted on 2006-10-25
Medium Priority
388 Views
Hi

I have two arrayalist

ArrayList a = {2,3,4, 5,6}
ArrayList b = {1,2,3}

a-b = {4,5,6}
b-a = {1}

My question is there any built in fuction that will perform minus operation of two array list.
If there is no builtin how do we perform. I think there should be a built in not sure how we can perform
Can you help we a running example.

Thanks
0
Question by:basirana
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 2

Expert Comment

ID: 17805516
public boolean removeAll(Collection c)
http://java.sun.com/j2se/1.5.0/docs/api/java/util/AbstractCollection.html#removeAll(java.util.Collection)

or iterator over one arraylist and keep using
public boolean remove(Object o)

http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html#remove(java.lang.Object)

Hope this helps.
0

LVL 14

Accepted Solution

hoomanv earned 2000 total points
ID: 17805570
0

Author Comment

ID: 17805659
Thanks for Response Have great time
0

LVL 1

Expert Comment

ID: 17805665
Here is an example of how to do that:

ArrayList<Integer> a=new ArrayList<Integer>();
ArrayList<Integer> b=new ArrayList<Integer>();

a.removeAll(b);

System.out.println(a); // will print [3,4,5,6]

An important point: Make sure the element type (Integer in this case) provides a working hashCode and equals implementation.

0

LVL 2

Expert Comment

ID: 17805695
Example:

ArrayList a = new ArrayList(), b = new ArrayList();
System.out.println(a.toString()+b.toString());
a.removeAll(b);
System.out.println(a.toString()+b.toString());

Output:
[1, 2, 3, 4, 5][2, 4]
[1, 3, 5][2, 4]
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
###### Suggested Courses
Course of the Month12 days, 17 hours left to enroll