Solved

ArrayList Comparistion

Posted on 2006-10-25
5
379 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
Comment Utility
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
Comment Utility
0
 

Author Comment

by:basirana
Comment Utility
Thanks for Response Have great time
0
 
LVL 1

Expert Comment

by:gambistics
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now