aspose file tools*
The moose likes Java in General and the fly likes ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "ArrayList " Watch "ArrayList " New topic
Author

ArrayList

Arulanand Dayalan
Ranch Hand

Joined: Aug 10, 2005
Posts: 124
Hi All,

I have an ArrayList A which contains list of transfer Objects. Also i have an another ArrayList B which contains a subset of ArrayList B. My problem is to get two seperate ArrayLists, one containting the elements in B and the other containg the elements not in B which are present in A. I am able to do this by taking the first element in A and check if it is present B, if present adding to the corresponding Arraylist. I repeting the whole process. This is inefficient, can anyone suggest better way of doing this.

My TO Looks like this,

pubic class VTO{
private name;
private id;

//Corresponding set and get methods.
}

Thanks in Advance.

Regards,
Arul.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Are you required to write the code yourself?

If not, there are methods defined in the AbstractCollection interface which is implemented by ArrayList that can help you.
Arulanand Dayalan
Ranch Hand

Joined: Aug 10, 2005
Posts: 124
Thanks for your reply. Its not that i need to write the code. I am looking for the better way of doing it. Can you tell how i can use the AbstractCollection Interface to implement this.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

Did you look at the interface? It's got this interesting method "retainAll()" which you should read about.


[Jess in Action][AskingGoodQuestions]
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi all,

I cannot understand why you are not simply using the bulk operations
containsAll
retainsAll

from you array list objects?
Are your lists so huge?

Bu.


all events occur in real time
Lucas Lee
Ranch Hand

Joined: Oct 02, 2006
Posts: 53
You can use HashSet which is in JDK to achieve you goal.
Fill two HashSet with two ArrayList,then use the first HashSetA.removeAll(HashSetB),then you'll get the result of A - B.
Arulanand Dayalan
Ranch Hand

Joined: Aug 10, 2005
Posts: 124
Thanks for all your replies.

I think i need to override the equals methods of

pubic class VTO{
private name;
private id;

//Corresponding set and get methods.
}

so that the retainAll method would works properly. Is that right?

Thanks and Regards,
Arul.
 
jQuery in Action, 2nd edition
 
subject: ArrayList
 
Similar Threads
populating Action Form with Arraylists values
arraylist in java
Collections and Garbage Collection
LinkedList/ArrayList: need a hybrid
TreeSet uses compareTo