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
  • Learn & ask questions
Solved

ArrayList Comparistion

Posted on 2006-10-25
5
382 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jar file executable 12 58
Error in @AspectJ Based AOP with Spring 2 18
jboss wildfly 10.1 10 232
American Express @Work site and Java 4 27
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
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 …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

839 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