Solved

ArrayList Comparistion

Posted on 2006-10-25
5
383 Views
Last Modified: 2008-02-01
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
Comment
Question by:basirana
5 Comments
 
LVL 2

Expert Comment

by:avsrivastava
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

by:
hoomanv earned 500 total points
ID: 17805570
0
 

Author Comment

by:basirana
ID: 17805659
Thanks for Response Have great time
0
 
LVL 1

Expert Comment

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

ArrayList<Integer> a=new ArrayList<Integer>();
ArrayList<Integer> b=new ArrayList<Integer>();
      
Collections.addAll(a,1,2,3,4,5,6);
Collections.addAll(b,1,2);
            
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

by:avsrivastava
ID: 17805695
Example:

   ArrayList a = new ArrayList(), b = new ArrayList();
   a.add(new Integer(1));
   a.add(new Integer(2));
   a.add(new Integer(3));
   a.add(new Integer(4));
   a.add(new Integer(5));
   b.add(new Integer(2));
   b.add(new Integer(4));
   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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question