Thursday, April 11, 2013

[android help] Removing common elements between two different Arraylist

java - Removing common elements between two different Arraylist - Stack Overflow



















Sorry If you are thinking that it is a repeated post. But the situation is different here. I have read several posts for it but not getting the exact thing I am looking for. I know How to develop a complex logic for this but please take a look this is for android and we can't expect too much processing on the device due to to limited resources available.


I have an ArrayList of an bean class objects consisting five fields as


Java Bean -- MyShares


  1. fileName

  2. filePath

  3. fileSize

  4. isShared

  5. Creator

I have another ArrayList of String which contains only filepaths. Now what I want is to remove all the common elements between the two arraylist means the file paths in seconds arraylist and file path in first arraylist objects are similar then I have to remove from both of the arraylist But I Don't want a new arraylist which contains the uncommon elemenst But I want to get my both arraylist only without their common elements.


Please help me.





























You can use an outer loop to scan over the Bean objects, and an inner loop to scan over the file paths.


pseudo code:



for(Bean i in beans){
for(String p in paths){
if(i.path.equals(p)){
beansToRemove.add(i);
pathsToRemove.add(p);
}
}
}
beans.removeAll(beansToRemove);
paths.removeAll(pathsToRemove);


I'm not sure if my extra arraylists to track the removed arraylists go against your question or not since the original arrays remain. If you presort both arrays on the path and keep track of the position in each area (not exhaustive search) you can improve it from n2 to nlgn























Rough Java code:



HashSet commonKeys = new HashSet();
for (Share share : shares) {
commonKeys.add(share.filePath);
}
commonKeys.retainAll(filePaths);
for (Iterator it = shares.iterator(); it.hasNext(); ) {
Share share = it.next();
if (commonKeys.contains(share.filePath)) {
it.remove();
}
}
filePaths.removeAll(commonKeys);



















lang-java






.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...